The point of this interface is to allow objects to give a basic representation of themselves within a simple key-value dictionary. The specific semantic of each key and value is let unspecified.
Moreover the class annotation auto_derive
will automatically implements the
interface with the attributes locally defined in the class.
class A
auto_derive
var an_int: Int
var a_string: String
end
var a = new A(5, "five")
var map = a.derive_to_map
assert map.length == 2
assert map["an_int"] == 5
assert map["a_string"] == "five"
deriving :: Derivable :: defaultinit
deriving :: Derivable :: derive_to_map
Returns a map that loosely represents the objectself
.
core :: Object :: class_factory
Implementation used byget_class
to create the specific class.
deriving :: Derivable :: defaultinit
core :: Object :: defaultinit
deriving :: Derivable :: derive_to_map
Returns a map that loosely represents the objectself
.
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).
# Interface of objects that expose some kind of internal representation in a very unreliable way.
#
# The point of this interface is to allow objects to give a basic representation of
# themselves within a simple key-value dictionary.
# The specific semantic of each key and value is let unspecified.
#
# Moreover the class annotation `auto_derive` will automatically implements the
# interface with the attributes locally defined in the class.
#
# ~~~
# class A
# auto_derive
# var an_int: Int
# var a_string: String
# end
#
# var a = new A(5, "five")
# var map = a.derive_to_map
# assert map.length == 2
# assert map["an_int"] == 5
# assert map["a_string"] == "five"
# ~~~
interface Derivable
# Returns a map that loosely represents the object `self`.
#
# Warning: by default the method returns an empty Map.
# It is done this way so that subclasses can just call `super` and add their own attributes.
#
# Forgetting to redefine `derive_to_map` will broke the expectation of the user of the class
# Since an empty map is not POLA.
#
# Note that the semantic of keys and values is let unspecified.
# Moreover, there is no specification nor mechanism to avoid key collision.
fun derive_to_map: Map[String, nullable Object]
do
return new HashMap[String, nullable Object]
end
end
lib/deriving/deriving.nit:24,1--61,3