The Object
class is the root of all classes. It provides default methods like to_s
for all objects.
Static Methods
field(obj, index)
Return the value of the field in obj
at index
. obj
must be an instance of a native Agate class. The fields are ordered like their definition in the class. Use this function with caution.
class Vec {
construct new() {
@x = 42.0
@y = 69.0
}
}
def v = Vec.new()
IO.println(Object.field(v, 0)) #> 42
IO.println(Object.field(v, 1)) #> 69
IO.println(Object.field(v, 2)) #> nil
See also: field(obj, index, value).
field(obj, index, value)
Change the value of the field in obj
at index
to value
. obj
must be an instance of a native Agate class. The fields are ordered like their definition in the class. Use this function with caution.
class Vec {
construct new() {
@x = 42.0
@y = 69.0
}
x { @x }
y { @y }
}
def v = Vec.new()
Object.field(v, 0, 13.0)
Object.field(v, 1, 21.0)
IO.println(v.x) #> 13
IO.println(v.y) #> 21
See also: field(obj, index).
has_method(obj, signature)
Return true if obj
has a method with the given signature
.
IO.println(Object.has_method(1, "to_f")) #> true
IO.println(Object.has_method(1, "to_a")) #> false
IO.println(Object.has_method(1, "to_x")) #> false
IO.println(Object.has_method(Object, "has_method(_,_)")) #> true
indirect_call(obj, signature, args)
Call the method signature
on obj
with the arguments in the array args
.
IO.println(Object.indirect_call("the cake is a lie", "find(_)", ["cake"])) #> 4
See also: Missing Method Operator.
same(obj1, obj2)
Return true
if obj1
and obj2
are the same. Values are compared by value (they must have the same state), and objects are compared by reference (they must be the exact same object). This method can be used to test the equality of two objects even if the ==
operator has been overriden.
IO.println(Object.same(42, 42)) #> true
IO.println(Object.same(42, 69)) #> false
IO.println(Object.same(42, 42.0)) #> false
IO.println(Object.same(42, nil)) #> false
def a = []
IO.println(Object.same(a, a)) #> true
IO.println(Object.same([], [])) #> false
See also: ==(other) and !=(other) operators
Methods
to_s
Transform the object to a String
.
class Saucisse {
construct new() { }
}
def s = Saucisse.new()
IO.println(s.to_s) #> instance of Saucisse
type
Returns the Class
of the object.
class Saucisse {
construct new() { }
}
def s = Saucisse.new()
IO.println(s.type) #> Saucisse
IO.println(s.type.type) #> Saucisse metaclass
IO.println(s.type.type.type) #> Class
Operators
! operator
Returns false
as most objects evaluates to true
except false
and nil
.
class Saucisse {
construct new() { }
}
def s = Saucisse.new()
IO.println(!s) #> false
==(other) and !=(other) operators
By default, two objects are considered equal if they have the same value for value types, or if they are the exact same object otherwise.
IO.println(42 == 42) #> true
IO.println(42 == 69) #> false
IO.println(42 == 42.0) #> false
IO.println(42 == nil) #> false
def a = []
IO.println(a == a) #> true
IO.println([] == []) #> false
See also: same(obj1, obj2)
is(other)
Return true
if the object’s class or one of its superclass is other
. other
must be a Class
.
IO.println(Object is Class) #> true
IO.println(42 is Int) #> true
IO.println(42 is Float) #> false
IO.println(nil is String) #> false
IO.println([] is Array) #> true