Skip to content

reflect

Runtime type introspection utilities inspired by Go's reflect.

Reflect exposes function-style APIs to inspect values and types at runtime. It represents a Type and a Value as Harneet maps, so you can pass them around and inspect their content.

Overview

  • Values are Harneet core.Objects.
  • Type is a map with at least keys: name (string), kind (string). Struct types also include fields (array of field names).
  • Value is a map with keys: value (any), type (Type).

Functions

  • reflect.TypeOf(value any) Type Returns a Type map describing the value.

  • reflect.ValueOf(value any) Value Returns a Value map wrapping the value with its Type.

  • reflect.KindOf(value any) string Returns the basic kind of a value: int, float, string, bool, array, map, struct, tuple, nil, error, object.

  • reflect.TypeName(t Type) string Returns t's name.

  • reflect.TypeKind(t Type) string Returns t's kind.

  • reflect.TypeString(t Type) string Returns a readable type string (same as name).

  • reflect.ValueType(v Value) Type Returns the Type contained in v.

  • reflect.ValueInterface(v Value) any Returns the underlying value from v.

  • reflect.ValueKind(v Value) string Returns the kind of the underlying value from v.

  • reflect.ValueIsNil(v Value) bool True if the underlying value is None.

  • reflect.ValueIsValid(v Value) bool True if the underlying value is not None.

Kinds

Possible results from KindOf/TypeKind: - int, float, string, bool - array, map, struct, tuple - nil, error, object

Note: - For custom stdlib objects (e.g., UUID), TypeOf(value)["name"] and reflect.TypeName(TypeOf(value)) return the specific type name (like UUID), while the kind is the generic category object.

Struct Field Metadata

For struct values, TypeOf(structValue) includes a fields entry with all field names.

Example:

Struct TypeOf
1
2
3
4
5
6
import reflect

type Person struct { name string; age int }
var p = Person{name: "Bob", age: 28}
var pt = reflect.TypeOf(p)
// pt["fields"] might be ["name", "age"]

Examples

See examples/reflect/basic_test.ha for a runnable demo:

Reflect Examples
import fmt
import reflect
import uuid

fmt.Println("=== reflect: Basic Demo ===")

var n = 42
fmt.Printf("KindOf(n): %s\n", reflect.KindOf(n))
var nt = reflect.TypeOf(n)
fmt.Printf("TypeName(n): %s\n", reflect.TypeName(nt))

var nv = reflect.ValueOf(n)
fmt.Printf("ValueKind(n): %s\n", reflect.ValueKind(nv))
fmt.Printf("ValueIsValid(n): %v\n", reflect.ValueIsValid(nv))
fmt.Printf("ValueInterface(n): %v\n", reflect.ValueInterface(nv))

// Custom stdlib object (UUID)
var u, err = uuid.v4()
if err == None {
    var ut = reflect.TypeOf(u)
    fmt.Printf("UUID type name: %s, kind: %s\n", reflect.TypeName(ut), reflect.TypeKind(ut))
}