It's currently possible to import the members of types into a namespace scope with use, similar to importing the members of a namespace.
This works correctly more or less by accident for some kinds of symbols, but for other kinds of symbols the results are not usable.
It works correctly for:
static members of classes and structs imported from another assembly
enum members
union variants
It doesn't work for
instance members
members of or any class, trait or struct member defined in the current project source, rather than imported from another assembly
And because it's an unintended side effect of the way use works, no checking is done to determine whether it's safe or meaningful for a given type member to be accessed without a qualifying instance. This leads to confusing error reports or even no errors and incorrect code generation if instance members are mistakenly imported.
It's not practical, and wouldn't be particularly useful, to make the broken scenarios work:
it makes no sense to support importing instance members because they have no meaning without a qualifying instance
supporting importing class, trait or struct members into global scope would require deferring the handling that use until those symbols exist, but use is processed before members are fully defined, because used types may be referenced by member definitions.
ghūl supports global symbols directly anyway
Instead we should restrict use to symbols that can be safely imported into global scope. When applied to entire types reflected from another assembly, use should only implicitly import static symbols. An error should be reported if the user attempts to explicitly import non-static member symbols from a reflected assembly. Attempting to import anything other than a union variant from a type defined in the current project source code should result in an error.
class TEST is
test_static() static is si
test_instance() is si
si
use TEST; // imports only test_static() into global scope
use TEST.test_static; // OK - is importing a static symbol into global scope
use TEST.test_instance; // report error - attempting to import an instance symbol into global scope
It's currently possible to import the members of types into a namespace scope with
use
, similar to importing the members of a namespace.This works correctly more or less by accident for some kinds of symbols, but for other kinds of symbols the results are not usable.
It works correctly for:
It doesn't work for
And because it's an unintended side effect of the way
use
works, no checking is done to determine whether it's safe or meaningful for a given type member to be accessed without a qualifying instance. This leads to confusing error reports or even no errors and incorrect code generation if instance members are mistakenly imported.It's not practical, and wouldn't be particularly useful, to make the broken scenarios work:
use
until those symbols exist, butuse
is processed before members are fully defined, becauseuse
d types may be referenced by member definitions.Instead we should restrict
use
to symbols that can be safely imported into global scope. When applied to entire types reflected from another assembly,use
should only implicitly import static symbols. An error should be reported if the user attempts to explicitly import non-static member symbols from a reflected assembly. Attempting to import anything other than a union variant from a type defined in the current project source code should result in an error.