Package aliasing

Is there a way to alias the public namespace of a package? Asking in case someone made a tool…

package library.name {
    public function util(): void {}
}

import ns = library.name.*
ns::util()

The following was used in ES4:

use namespace 'flash.display'

In AS3 it creates another URI namespace.

namespace ns = 'library.name'
ns::util()  // Undefined

namespaces are not packages, those are 2 different things

but

from a Redtamarin example

/**
 * @exampleText Get a class definition using Namespace.
 */

// using a namespace also allow to resolve a class without using the 'import' keyword
var ns:Namespace = new Namespace( "flash.system" );

// yep namespaces can be dynamic and that powerful
trace( "vmVersion = " + ns::["System"].vmVersion );

this will not work
namespace ns = 'library.name'
because the string will not cast automatically to a namespace

instead you have to use the constructor
var ns:Namespace = new Namespace('library.name');
this will inherit the library into the ns

after that you should be able to
ns::util()
or
ns::["util"]()

Yeah, just tried to use this in AIR. Rant amxmlc -warn-no-type-decl=false main.as with:

main.as

package {
    import flash.display.*
    import semanticmodel.*

    public final class main extends Sprite {
        public function main() {
            semanticmodel.Foo
            var sem = new Namespace('semanticmodel')
            throw new Error((new sem::Foo).produce())
        }
    }
}

semanticmodel/Foo.as

package semanticmodel {
    public final class Foo {
        public function produce() : String {
            return 'P'
        }
    }
}

But, still, namespace ns = 'uri' is valid, except package name is not recognised.

Reading the AVM2 overview on namespace_info, there are constants Constant_PackageNamespace and Constant_PackageInternalNs, so I believe ASC 2 creates symbols for both namespaces public and internal for every single package. It’d make sense to support a compile-time way to alias it too.

What are you trying to do exactly?

Now, I am in the phase of refinishing a verifier (which is also a symbol solver). I have various times written parsers in the same lookahead strategy, sometimes in JavaScript, or sometimes in ActionScript, but I ended up with my final parser in JavaScript.

Some names from AST and semantic model would conflict, or confuse with each other, even in different packages, so it’d be useful to use the public namespace of various packages as another name, like sem, ast and bc

An example is when symbols are mapped to nodes without clustering the node structure. In this case, the node of filter operator (from AS3) in my dialect would need a symbol with similiar name: ast::FilterExpression and sem::FilterOperator.

… However, if package name is short, we can also write a fully-qualified package name instead, like, i18n.SomeClass, but it is not always the case…

so hydroper then …

under Redtamarin test suite

/*
  test
*/

package semanticmodel {
    public final class Foo {
        public function produce() : String {
            return 'P'
        }
    }
}

package {
    import flash.display.*
    import semanticmodel.*

    public final class main extends Sprite {
        public function main() {
            semanticmodel.Foo
            var sem = new Namespace('semanticmodel')
            throw new Error((new sem::Foo).produce())
        }
    }
}


trace( "hello world" );

var tmp = new main();

when the code is compiled to bytecode the result is

test_foobar.abc, 916 bytes written
----------------------------------------------------------------
hello world
Error: P
        at main()[test_foobar.as:21]
        at global$init()[test_foobar.as:29]
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
exit code = 1
----------------------------------------------------------------

as expected

the same source run as evaluated script give an error

SyntaxError: test_foobar.as:17: Syntax error: Base class not found

I’m not sure how you test, but my guess is you are not compiling to bytecode

Yes, in the repl there are some limitations. Used redbean to run ASC, rant the ABC with redshell and all has gone fine