BoxLang: Our new JVM Dynamic Language made by Ortus! Check it out: https://www.boxlang.io

Add final assignment modifier for variables

Description

Add final assignment modifier for assignments

final foo = "bar"

this, of course, can be used with other modifiers

final static foo = "bar" final var foo = "baz"

The only 2 modifiers that can’t be used together are var and static since they represent different scopes.

When a variable is declared as final, the scope it is being set into will track a list of keys which are designated as final, and will prevent those keys from being modified in the future.

This is ONLY a feature of scopes. Structs and other struct-like container will not have a final concept.

Note, using the final modifier on a UDF

final function foo() {}

is basically the same as

final foo = ()->{}

since both register the foo variable as “final” in the variables scope.

The following operations check the final keys

  • declaring a UDF

  • setting a variable

  • calling scope.put(), scope.assign(), scope.remove() or any BIF that delegates to these such as scope.delete() or structDelete( scope, "foo" )


Notes:

  • unlike const these are not compiler constants and provide no bytecode-level optimization. This is due to the fact nearly all symbol resolution happens at runtime

  • unlike Java`s final, there is no bytecode level optimization for the same reason

  • The only real purpose of final is to ensure the variable reference cannot be reassigned

  • A final var reference cannot be re-assigned, HOWEVER, complex values like structs or arrays can still be mutated unless they are immutable.

The following example

final lockDown = [ 1, 2, 3 ].toImmutable()

cannot be mutated OR re-assigned.


You can see the Set of final keys for a scope via the meta object

variables.$bx.meta.finalKeySet

You can also remove keys from the set to make a variable no longer final.

final foo = "bar" variables.$bx.meta.finalKeySet.clear() // Nothing is final in this scope now foo = "baz" // no error

Activity

Show:
Fixed
Pinned fields
Click on the next to a field label to start pinning.

Details

Assignee

Reporter

Fix versions

Priority

Sentry

Created August 20, 2024 at 3:55 AM
Updated August 20, 2024 at 4:00 AM
Resolved August 20, 2024 at 3:56 AM

Flag notifications