6.26. kink/VARREF¶
6.26.1. type varref¶
`varref` is a variable reference, which is a value that represents a variable.
A `varref` is a tuple of (Owner, Sym), where `Owner` is the owner value of the variable, and `Sym` is the symbol of the variable.
`varref` is a subtype of `plain_param`. See kink/param/PLAIN_PARAM.
6.26.1.1. Varref.owner¶
`owner` returns `Owner` of `Varref`.
Example
:Owner <- 42
:Varref <- Owner:Foo
stdout.print_line(Varref.owner.repr) # => 42
6.26.1.2. Varref.sym¶
`sym` returns a `str` value of `Sym` of `Varref`.
Example
:Owner <- 42
:Varref <- Owner:Foo
stdout.print_line(Varref.sym.repr) # => "Foo"
6.26.1.3. Varref <- Content¶
`<-` operator, or `op_store` method, performs variable-store operation for the variable represented by `Varref`, with `Content` as the content of the variable.
Example
:V <- 42
stdout.print_line(V.repr) # => 42
6.26.1.4. Varref.load¶
`load` performs variable-load operation for the variable represented by `Varref`, and returns the content value of the variable.
Precondition
The content of the variable must not be empty.
Example
:V <- 42
stdout.print_line(:V.load.repr) # => 42
6.26.1.5. Varref.any?¶
`any?` performs variable-load operation for the variable represented by `Varref`, and returns whether the content is not empty.
Example
:Owner <- new_val('Foo' 42)
stdout.print_line(Owner:Foo.any?.repr) # => true
stdout.print_line(Owner:Bar.any?.repr) # => false
6.26.1.6. Varref.require_from(Prefix)¶
`require_from` loads a module then stores the module to the variable.
Calling this method is equivalent to calling `Varref <- MOD.require(Prefix + Varref.sym)`.
Precondition
`Prefix` must be a `str`.
Example
:NUM.require_from('kink/')
stdout.print_line(NUM.is?(42).repr) # => true
6.26.1.7. Varref.rest¶
`rest` returns a `rest_param` backed by the variable.
`pass_rest(Rest_args)` method of the result `rest_param` stores `Rest_args` to the variable.
See also kink/param/REST_PARAM
Example
:take_args <- {(:X :Y :Z.rest)
stdout.print_line(X.repr)
stdout.print_line(Y.repr)
stdout.print_line(Z.repr)
}
take_args('foo' 'bar')
# Output:
# "foo"
# "bar"
# []
take_args('foo' 'bar' 'baz')
# Output:
# "foo"
# "bar"
# ["baz"]
take_args('foo' 'bar' 'baz' 'qux')
# Output:
# "foo"
# "bar"
# ["baz" "qux"]
6.26.1.8. Varref.opt¶
`opt` makes an `opt_param` backed by the variable.
`pass_arg(Arg)` method of the result `opt_param` stores a singleton vector `[Arg]` to the variable.
`pass_nothing` method of the result opt_param stores an empty vector to the variable.
See also kink/param/OPT_PARAM
Example
:take_args <- {(:X :Y :Z.opt :W.opt)
stdout.print_line(X.repr)
stdout.print_line(Y.repr)
stdout.print_line(Z.repr)
stdout.print_line(W.repr)
}
take_args('foo' 'bar')
# Output:
# "foo"
# "bar"
# []
# []
take_args('foo' 'bar' 'baz')
# Output:
# "foo"
# "bar"
# ["baz"]
# []
take_args('foo' 'bar' 'baz' 'qux')
# Output:
# "foo"
# "bar"
# ["baz"]
# ["qux"]
6.26.2. VARREF.new(Owner Sym)¶
`new` returns a `varref` (Owner, Sym).
Precondition
`Sym` must be a `str`.
Example
:VARREF.require_from('kink/')
:Owner <- new_val('Foo' 42)
:Varref <- VARREF.new(Owner 'Foo')
stdout.print_line(Varref.load.repr) # => 42
6.26.3. VARREF.is?(Val)¶
`is?` returns whether `Val` is a `varref`.