Object
import "std/types" (included in import "std") · namespace std.types
Object works on any value: its type, its text, equality and a hash, and its fields. Its methods are also methods of every any, and of every object whose class has no method of that name itself (a class's own toString wins).
grav
import "std"
@Entity
class Point {
public x: int
public y: int
constructor(x: int, y: int) { self.x = x; self.y = y }
}
fn main() {
let p = new Point(1, 2)
let v: any = 3
print(p.typeName() + " " + p.className() + " " + v.typeName())
print(p.toMap())
print("${Object.equals(3, 3.0)} ${Object.equals("a", "a")} ${p.identical(p)} ${Object.equals(p, new Point(1, 2))}")
}Point Point int
{"x": 1, "y": 2}
true true true falseequalscompares numbers by value (3equals3.0), strings by text, and objects by identity;hashagrees with it.toMapandfieldNamesneed the class's reflection (a decorator on it, orreflect(...)naming it); for other classes they are empty.
API
class Object
Any value (see the top of this file).
Static members
static fn typeName(v: any) -> string— Its type's full name ("app.User", "int", "string"; "null" for null).static fn className(v: any) -> string— Its class's name without the namespace ("User"; the type name for other values).static fn toString(v: any) -> string— Howprintshows it.static fn equals(a: any, b: any) -> bool— Whetheraandbhold equal values: numbers by value (1 equals 1.0), strings by text, bools, and objects only when they are the same object.static fn identical(a: any, b: any) -> bool— Whetheraandbare the very same object (or equal non-objects).static fn hash(v: any) -> long— A hash that agrees with equals (objects by identity).static fn isNull(v: any) -> bool— Whethervis null.static fn requireNonNull(v: any, message: string) -> any—v; ValueError withmessagewhen it is null.static fn classInfo(v: any) -> ClassInfo— Its class's ClassInfo (null for values that are not objects).static fn isInstanceOf(v: any, info: ClassInfo) -> bool— Whether it is an object of classinfoor a subclass.static fn toMap(v: any) -> HashMap<string, any>— Its fields by name, base class first (for a class with reflection: one that has a decorator, or that reflect(...) names; others give an empty map).static fn fieldNames(v: any) -> Array<string>— Its fields' names (as toMap).