clarification on namespaces

zwetan zwetan at gmail.com
Mon Jun 11 01:15:37 PDT 2007


Hi,

making some tests in Tamarin and Flash
I realize that having a "dynamic way" to use namespace could be usefull

as I see it now namespaces need to be explicitely opened
> use namespace xyz;

but what about the cases where someone would want to autoselect one
among a bunch of namespaces, in short to be able to do
> use namespace eval("choice")

my use case is the following:
the need to autoselect a namespace depending on the operating system,
if the Flash player run on Windows use the win32 namespace, if the tamarin VM
run on Macintosh use the mac namespace, etc.

concrete example (tested in Flash and a modified Tamarin)
--------------------
package system
    {

    /* Class: Reflection
       Provide basic reflection mecanisms on the language.
     */
    public class Reflection
        {
        public namespace notimplemented;
        public namespace flash;
        public namespace redtamarin;

        private static var _current:Namespace = notimplemented;

        private static function init():void
            {
            switch( Environment.host )
                {
                case HostID.Flash:
                case HostID.Apollo:
                _current = flash;
                break;

                case HostID.RedTamarin:
                _current = redtamarin;
                break;

                case HostID.Tamarin:
                case HostID.Unknown:
                default:
                _current = notimplemented;
                }
            }

        notimplemented static function getClassName( o:*, path:Boolean
= false ):String
            {
            return "";
            }

        flash static function getClassName( o:*, path:Boolean = false ):String
            {
            import flash.utils.getQualifiedClassName;
            var str:String = getQualifiedClassName( o );

            if( !path && (str.indexOf( "::" ) > -1) )
                {
                str = str.split( "::" )[1];
                }

            return str;
            }

        redtamarin static function getClassName( o:*, path:Boolean =
false ):String
            {
            import avmplus.Reflection;

            if( !path )
                {
                return Reflection.findClassName( o );
                }

            return Reflection.findQualifiedClassName( o );
            }

        public static function getClassName( o:*, path:Boolean = false ):String
            {
            return _current::getClassName( o, path );
            }

        init();
        }

    }
--------------------

the situation is that Flash
declare native function for "reflection" in flash.utils.*
and RedTamarin ( a small project where I try to add some native
function to Tamarin)
declare native function for "reflection" in the avmplus.Reflection class

the goals is to have a public reflection API in the system.Reflection class
if the host support it you have reflection either in the "flash" or
"redtamarin" namespace,
if not it default to stub functions in the "notimplemented" namespace.

the hackish workaround is working, but imho it's not ideal
main problems are:
- compiling for Flash, you need to define a "fake" avmplus package
  because of the the import in the redtamarin namespace
- you can explicit declare a namespace before compiling
  ex: use namespace flash
  but if you do that you'll need to change this one line
  depending on the host you compile for
- I didn't really find another way to select dynamically at runtime a namespace
  other than declaring namespaces in the class directly, namespace at
  package level can not be switched dynamically apparently.


ok this was before I read some notes in the M0 release :)

so here my questions:

in es4/buildins/Namespace.es
"
// FIXME: we might need a static make-a-user-namespace
// function here for dynamic namespaces. Not a ctor!
"

I do agree, being able to select a namespace dynamically would be of great help
for some particular cases, cf my use case.

in es4/doc/JD_NOTES
"
BUGS
- importing a non-existing package is not an error
"

indeed, this can cause problems when the underlying native API is not the same
for different hosts, to compile you need to define fake/stubs packages
and/or classes,
not ideal...

note that my goal is to be able to provide a set of packages and
classes that can target
different hosts depending on their native capacity, but for this to be
elegant somehow
I would like to be able to select dynamically which namespace gonna be
used at runtime
is something is planed in that sens with the final ES4 spec ?

maybe I missed a simpler way to do it, but I wish I could use a
namespace like that

package
   {
   public namespace A;
   }

package
   {
   public namespace B;
   }

package test
   {

   public class Something
       {
       A static function hello():String
          {
          //...
          }

       B static function hello():String
          {
          //...
          }
   }

//main entry point
package
    {
    var ns:Namespace = new Namespace( "A" ); //this can not be done afaik
    use namespace ns; //this can not be done afaik
    import test.Something;
    trace( Something.hello() ); //namespace A used
    }


cheers,
zwetan



More information about the Es4-discuss mailing list