On this page:
#%dynamism
use_  static
use_  dynamic
dynamic
8.12

6.21 Static and Dynamic Lookup🔗ℹ

expression

#%dynamism

Initially indicates dynamic mode, but intended to be redefined by or use_dynamic.

This binding is not exported by rhombus/static, and a binding to indicate static mode is exported, instead.

definition

use_static

(Re-)defines #%dynamism to require certain static information and consistency with static information:

For each of these forms, static mode is detected by checking the binding of #%dynamism using the scopes of form’s operator or identifier.

Other forms not provided by rhombus might also be sensitive to dynamic mode versus static mode. Macro implementations can use syntax_meta.is_static to determine a mode.

See also Rhombus Static by Default.

> class Posn(x, y)

> fun ok_lookup(ps :~ List.of(Posn)):

    use_static

    ps[0].x

> fun bad_lookup(ps):

    use_static

    ps[0].x

[]: specialization not known (based on static information)

> fun still_bad_lookup(ps :~ List):

    use_static

    ps[0].x

x: no such field or method (based on static information)

> block:

    use_static

    Posn(1, 2, 3)

Posn: wrong number of arguments in function call (based on static information)

definition

use_dynamic

(Re-)defines #%dynamism to not require certain static information and consistency with static information. See use_static.

> class Posn(x, y)

> fun (ps):

    use_dynamic

    ps[0].x

#<function:fun>

function

fun dynamic(v :: Any) :: Any

The identity function.

A call to dynamic has no static information, even if its argument has static information, so wrapping a call to dynamic around an expression has the effect of discarding any static information available for the expression.

> class Posn(x, y)

> fun bad_lookup(ps :~ List.of(Posn)):

    use_static

    dynamic(ps)[0].x

[]: specialization not known (based on static information)

> fun still_bad_lookup(ps :~ List.of(Posn)):

    use_static

    dynamic(ps[0]).x

x: no such field or method (based on static information)

> fun (ps :~ List.of(Posn)):

    dynamic(dynamic(ps)[0]).x

#<function:fun>