This is one of the hackiest way to retrieve the name of a random type.
var dummy = new GetName[Sequence[nullable Comparable]]
assert dummy.to_s == "Sequence[nullable Comparable]"
This also works to resolve formal types
class G[T]
type V: Collection[T]
var name_of_v: String = (new GetName[V]).to_s is lazy
end
var g = new G[Bool]
assert g.name_of_v == "Collection[Bool]"
Warning: this does not work if --erasure is used.
meta :: GetName :: defaultinit
core :: Object :: class_factory
Implementation used byget_class
to create the specific class.
meta :: GetName :: defaultinit
core :: Object :: defaultinit
core :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
core :: Object :: output_class_name
Display class name on stdout (debug only).
# Helper to get the name of a type
#
# This is one of the hackiest way to retrieve the name of a random type.
#
# ~~~
# var dummy = new GetName[Sequence[nullable Comparable]]
# assert dummy.to_s == "Sequence[nullable Comparable]"
# ~~~
#
# This also works to resolve formal types
#
# ~~~
# class G[T]
# type V: Collection[T]
#
# var name_of_v: String = (new GetName[V]).to_s is lazy
# end
#
# var g = new G[Bool]
# assert g.name_of_v == "Collection[Bool]"
# ~~~
#
# Warning: this does not work if --erasure is used.
class GetName[E]
redef fun to_s do
var name = class_name
if name.length < 9 then return ""
return name.substring(8, name.length-9)
end
end
lib/meta/meta.nit:101,1--130,3