Grav v1.14.0

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))}")
}
Output
Point Point int
{"x": 1, "y": 2}
true true true false
  • equals compares numbers by value (3 equals 3.0), strings by text, and objects by identity; hash agrees with it.
  • toMap and fieldNames need the class's reflection (a decorator on it, or reflect(...) 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 — How print shows it.
  • static fn equals(a: any, b: any) -> bool — Whether a and b hold 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 — Whether a and b are 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 — Whether v is null.
  • static fn requireNonNull(v: any, message: string) -> any — v; ValueError with message when 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 class info or 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).

Edit this page on GitHub