3.20. kink/VARREF

Companion mod for varref vals.

3.20.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.

Varref.owner

Varref.owner returns the Owner of the varref.

Example:

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

Varref.sym

Varref.sym returns the Sym of the varref.

Example:

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

Varref.op_store(Val)

Varref.op_store assigns the variable to the Val.

Example:

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

Varref.load

Varref.load returns the target of the variable.

Precondition:

• the variable must be nonempty

Example:

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

Varref.empty?

Varref.empty? returns whether the 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

Varref.nonempty?

Varref.nonempty? returns whether the Varref is nonempty.

Example:

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

Varref.require_from(Prefix)

Varref.require_from loads a mod then stores the mod to Varref.

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

Example:

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

The program above is equivalent to the following.

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

Varref.rest

Varref.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.

Varref.opt

Varref.opt makes a opt_param backed by the variable.

pass_arg method of the result opt_param assigns the variable to a vec containing the argument of pass_arg.

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

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.

Varref.repr

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

3.20.2. VARREF.varref?(Val)

VARREF.varref? returns whether the Val is a varref.

3.20.3. VARREF.new(Owner Sym)

VARREF.new makes a varref of the Owner and the 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