4.1. The supertype of all other types: type `val`¶
`val` is the supertype of all other types. Every value is an instance of `val` type.
`val` type has only one method: `repr`. It means that every value of Kink has `repr` method.
4.1.1. V.repr¶
`repr` returns a str which describes the receiver value `V` for debugging purpose.
The format of the result differs for each type:
:BIN.require_from('kink/')
stdout.print_line(new_val.repr) # => (val val_id=123)
stdout.print_line([1 2 3].repr) # => [1 2 3]
stdout.print_line(BIN.of(1 2 3).repr) # => (bin 0x01 0x02 0x03)
stdout.print_line("hello world\n".repr) # => "hello world\n"
For example, the result of `repr` can be used to construct an error message:
:NUM.require_from('kink/')
:to_int <- {(:N)
NUM.is?(N) || raise(
'to_int(N): N must be a num, but was {}'.format(N.repr))
N // 1
}
to_int('foo')
# Output:
# -- main exception
# ...
# {(stdin) L4 C17 raise} NUM.is?(N) || -->raise(
# to_int(N): N must be a num, but was "foo"
The runtime also uses the result of `repr` to construct an error message:
'foo'.bar
# Output:
# -- main exception
# ...
# [(stdin) L1 C7] 'foo'.-->bar
# no such var: bar not found in "foo"
Apart from error messages, `repr` might be used to construct log messages.
There are some requirements for implementations of `repr`.
`repr` must return a str value
As described above, `repr` is used to construct error messages, and callers expect that `repr` returns a str value. If `repr` returns a non-str value, hence it breaks the expectation, things can get really messy.
`repr` must return a str also for recursive data
If you implement a container type, probably your `repr` implementation calls `repr` method of elements. Make sure that `repr` properly returns a str also for recursive data:
:Y <- [4 5 6]
:X <- [1 2 3 Y]
Y.push_back(X)
stdout.print_line(X.repr) # => [1 2 3 [4 5 6 [,,,]]]
Recursion check can be done using DYN. Check the implementation of kink/container/FLAT_SET for example.
Keep the result short enough
If the result of `repr` is too long, it can mess up the debug output. So make sure to keep it short enough.