The trick is just to encapsulate the “real” value.
nitc $ PrimitiveInstance :: SELF
Type of this instance, automatically specialized in every classnitc $ PrimitiveInstance :: is_true
Returntrue
if the instance is the true
value.
nitc $ PrimitiveInstance :: to_b
Return the integer value if the instance is a byte.nitc $ PrimitiveInstance :: to_f
Return the integer value if the instance is a float.nitc $ PrimitiveInstance :: to_i
Return the integer value if the instance is an integer.nitc $ PrimitiveInstance :: to_i16
Return the integer value if the instance is a int16.nitc $ PrimitiveInstance :: to_i32
Return the integer value if the instance is a int32.nitc $ PrimitiveInstance :: to_i8
Return the integer value if the instance is a int8.nitc $ PrimitiveInstance :: to_u16
Return the integer value if the instance is a uint16.nitc $ PrimitiveInstance :: to_u32
Return the integer value if the instance is a uint32.core :: Object :: class_factory
Implementation used byget_class
to create the specific class.
nitc :: Instance :: defaultinit
nitc :: PrimitiveInstance :: 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 :: native_class_name
The class name of the object in CString format.core :: Object :: output_class_name
Display class name on stdout (debug only).
# Special instance to handle primitives values (int, bool, etc.)
# The trick is just to encapsulate the “real” value.
class PrimitiveInstance[E]
super Instance
# The real value encapsulated
redef var val: E
redef fun is_true
do
if val == true then return true
if val == false then return false
abort
end
redef fun ==(o)
do
if not o isa PrimitiveInstance[nullable Object] then return false
return self.val == o.val
end
redef fun eq_is(o)
do
if not o isa PrimitiveInstance[nullable Object] then return false
return self.val.is_same_instance(o.val)
end
redef fun to_s do return "{mtype}#{val.object_id}({val or else "null"})"
redef fun to_i do return val.as(Int)
redef fun to_f do return val.as(Float)
redef fun to_b do return val.as(Byte)
redef fun to_i8 do return val.as(Int8)
redef fun to_i16 do return val.as(Int16)
redef fun to_u16 do return val.as(UInt16)
redef fun to_i32 do return val.as(Int32)
redef fun to_u32 do return val.as(UInt32)
end
src/interpreter/naive_interpreter.nit:799,1--843,3