4.23. kink/VARREF

Companion mod for varref vals.

4.23.1. type varref

A varref, or a variable reference, is a val which represents a variable.

Operations such as assignment are done via varref vals.

A varref consists of (Owner, Sym), where the `Owner` is the owner of the variable, and the `Sym` is the symbol of the variable.

A varref is a subtype of plain_param. See kink/param/PLAIN_PARAM mod.

4.23.1.1. Varref.owner

`owner` returns the `Owner` of `Varref`.

Example:

:Owner <- 42
:Varref <- Owner:Foo
stdout.print_line(Varref.owner.repr) # => 42

4.23.1.2. Varref.sym

`sym` returns the `Sym` of `Varref`.

Example:

:Owner <- 42
:Varref <- Owner:Foo
stdout.print_line(Varref.sym.repr) # => "Foo"

4.23.1.3. Varref.op_store(Content)

`op_store` does 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

4.23.1.4. Varref.load

`load` does variable load operation for the variable represented by `Varref`, and returns the content of the variable.

Precondition:

• the content of the variable must not be empty

Example:

:V <- 42
stdout.print_line(:V.load.repr) # => 42

4.23.1.5. Varref.empty?

`empty?` returns whether the content of the variable represented by `Varref` is empty.

Example:

:Owner <- new_val('Foo' 42)
stdout.print_line(Owner:Foo.empty?.repr)  # => false
stdout.print_line(Owner:Bar.empty?.repr)  # => true

4.23.1.6. Varref.require_from(Prefix)

`require_from` loads a mod then stores the mod to the variable.

Calling this method is equivalent to calling “Varref <- MOD.require(Prefix + Varref.sym)”.

Example:

:NUM.require_from('kink/')
stdout.print_line(NUM.is?(42).repr)  # => true

The program above is equivalent to the following.

:MOD.require_from('kink/')
:NUM <- MOD.require('kink/NUM')
stdout.print_line(NUM.is?(42).repr)  # => true

4.23.1.7. Varref.rest

`rest` makes a rest_param backed by the variable.

`pass_rest` method of the result rest_param assigns the variable to the args.

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"]

See also kink/param/REST_PARAM.

4.23.1.8. Varref.opt

`opt` makes an opt_param backed by the variable.

`pass_arg` method of the result opt_param stores a singleton vec of the argument of `pass_arg` to the variable.

`pass_nothing` method of the result opt_param stores an empty vec to the variable.

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"]

See also kink/param/OPT_PARAM.

4.23.1.9. Varref.repr

`repr` returns a str representation of the Varref, such as ":Foo".

4.23.2. VARREF.is?(Val)

`is?` returns whether `Val` is a varref.

4.23.3. VARREF.new(Owner Sym)

`new` makes 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