Skip to content

Instantly share code, notes, and snippets.

@ellemenno
Last active December 21, 2015 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ellemenno/6362197 to your computer and use it in GitHub Desktop.
Save ellemenno/6362197 to your computer and use it in GitHub Desktop.
trying to understand `as`, `is`, `typeof`, `instanceof` in Loomscript

Testing type reflection in Loom

(results below)

TypeChecker.ls

package
{
    import loom.Application;
    import loom2d.display.StageScaleMode;
    import loom2d.ui.SimpleLabel;

    public interface I {}
    public class C {}
    public class B extends C implements I {} //      B => C, B -> I
    public class A extends B {}              // A => B => C


    public class TypeChecker extends Application
    {
        private var label:SimpleLabel;

        override public function run():void
        {
            stage.scaleMode = StageScaleMode.LETTERBOX;

            label = stage.addChild(new SimpleLabel("assets/Curse-hd.fnt")) as SimpleLabel;
            centeredMessage(label, this.getFullTypeName());

            trace('');
            check();
        }

        private function centeredMessage(label:SimpleLabel, msg:String):void
        {
            label.text = msg;
            label.center();
            label.x = stage.stageWidth / 2;
            label.y = (stage.stageHeight / 2) - (label.height / 2);
        }

        public function check():void
        {
            echoType(Boolean, true, false);
            echoType(Number, 1, 0);
            echoType(String, 'a', 'b');
            echoType(Function, function() {}, function() {});

            echoType(Vector, ['a', 'b'], [0, 1]);
            echoType(Dictionary, { a: 'a'}, { b: 'b'});
            echoType(C, new A(), new B());
            echoType(I, new A(), new B());
        }

        private function echoType(T:Type, a:Object, b:Object):void
        {
            var s:Vector.<String> = [];
            var w:Vector.<Number> = [];

            trace('');
            trace('<T> = ' +T.getName());

            w = [4, 10, 16, 25];
            s = ['____',  'getTypeName' ,  'getFullTypeName' ,  'toString' ]; trace(row(s, w));
            s = [' a' ,  a.getTypeName(), a.getFullTypeName(), "'" +a.toString() +"'"]; trace(row(s, w));
            s = [' b' ,  b.getTypeName(), b.getFullTypeName(), "'" +b.toString() +"'"]; trace(row(s, w));

            trace('---- ');
            w = [4, 9, 9, 9, 9, 9];
            s = ['(as)', 'a', 'b', 'T', 'a.getType', 'b.getType' ]; trace(row(s, w));
            s = [' a', (a as a).getTypeName(), (a as b).getTypeName(), (a as T).getTypeName(), (a as b.getType()).getTypeName(), (a as b.getType()).getTypeName() ]; trace(row(s, w));
            s = [' b', (b as a).getTypeName(), (b as b).getTypeName(), (b as T).getTypeName(), (b as b.getType()).getTypeName(), (b as b.getType()).getTypeName() ]; trace(row(s, w));

            trace('---- ');
            w = [4, 5, 5, 5, 8, 8];
            s = ['(is)', 'a', 'b', 'T', 'a.getType', 'b.getType' ]; trace(row(s, w));
            s = [' a', (a is a), (a is b), (a is T), (a is a.getType()), (a is b.getType()) ]; trace(row(s, w));
            s = [' b', (b is a), (b is b), (b is T), (b is a.getType()), (b is b.getType()) ]; trace(row(s, w));

            trace('---- ');
            w = [4, 5, 5, 5, 8, 8];
            s = ['(of)', 'a', 'b', 'T', 'a.getType', 'b.getType' ]; trace(row(s, w));
            s = [' a', (a instanceof a), (a instanceof b), (a instanceof T), (a instanceof a.getType()), (a instanceof b.getType()) ]; trace(row(s, w));
            s = [' b', (b instanceof a), (b instanceof b), (b instanceof T), (b instanceof a.getType()), (b instanceof b.getType()) ]; trace(row(s, w));
        }

        private function row(s:Vector.<String>, w:Vector.<Number>):String
        {
            var r:String = '';
            var n:Number = s.length;
            for (var i:Number = 0; i < n; i++)
            {
                r = r + pad(s[i], w[i]);
                r = r + ((i+1 < n) ? ' | ' : '');
            }
            return r;
        }

        private function pad(s:String, n:Number=8, c:String=' '):String
        {
            while (s.length <= n) s = s +c;
            return s;
        }
    }

}

Results

loom CLI 1.0.808
SDK: 1.1.2738 (release candidate for sprint 31)

<T> = Boolean
____  | getTypeName | getFullTypeName   | toString                  
 a    | Boolean     | system.Boolean    | 'true'                    
 b    | Boolean     | system.Boolean    | 'false'                   
---- 
(as)  | a          | b          | T          | a.getType  | b.getType 
 a    | Null       | Null       | Null       | Null       | Null      
 b    | Null       | Null       | Null       | Null       | Null      
---- 
(is)  | a      | b      | T      | a.getType | b.getType
 a    | false  | false  | false  | false     | false    
 b    | false  | false  | false  | false     | false    
---- 
(of)  | a      | b      | T      | a.getType | b.getType
 a    | false  | false  | false  | false     | false    
 b    | false  | false  | false  | false     | false    

<T> = Number
____  | getTypeName | getFullTypeName   | toString                  
 a    | Number      | system.Number     | '1'                       
 b    | Number      | system.Number     | '0'                       
---- 
(as)  | a          | b          | T          | a.getType  | b.getType 
 a    | Null       | Null       | Null       | Null       | Null      
 b    | Null       | Null       | Null       | Null       | Null      
---- 
(is)  | a      | b      | T      | a.getType | b.getType
 a    | false  | false  | false  | false     | false    
 b    | false  | false  | false  | false     | false    
---- 
(of)  | a      | b      | T      | a.getType | b.getType
 a    | false  | false  | false  | false     | false    
 b    | false  | false  | false  | false     | false    

<T> = String
____  | getTypeName | getFullTypeName   | toString                  
 a    | String      | system.String     | 'a'                       
 b    | String      | system.String     | 'b'                       
---- 
(as)  | a          | b          | T          | a.getType  | b.getType 
 a    | Null       | Null       | Null       | Null       | Null      
 b    | Null       | Null       | Null       | Null       | Null      
---- 
(is)  | a      | b      | T      | a.getType | b.getType
 a    | false  | false  | false  | false     | false    
 b    | false  | false  | false  | false     | false    
---- 
(of)  | a      | b      | T      | a.getType | b.getType
 a    | false  | false  | false  | false     | false    
 b    | false  | false  | false  | false     | false    

<T> = Function
____  | getTypeName | getFullTypeName   | toString                  
 a    | Function    | system.Function   | 'system.Function'         
 b    | Function    | system.Function   | 'system.Function'         
---- 
(as)  | a          | b          | T          | a.getType  | b.getType 
 a    | Null       | Null       | Null       | Null       | Null      
 b    | Null       | Null       | Null       | Null       | Null      
---- 
(is)  | a      | b      | T      | a.getType | b.getType
 a    | false  | false  | false  | false     | false    
 b    | false  | false  | false  | false     | false    
---- 
(of)  | a      | b      | T      | a.getType | b.getType
 a    | false  | false  | false  | false     | false    
 b    | false  | false  | false  | false     | false    

<T> = Vector
____  | getTypeName | getFullTypeName   | toString                  
 a    | Vector      | system.Vector     | 'a,b'                     
 b    | Vector      | system.Vector     | '0,1'                     
---- 
(as)  | a          | b          | T          | a.getType  | b.getType 
 a    | Vector     | Vector     | Null       | Null       | Null      
 b    | Vector     | Vector     | Null       | Null       | Null      
---- 
(is)  | a      | b      | T      | a.getType | b.getType
 a    | true   | true   | false  | false     | false    
 b    | true   | true   | false  | false     | false    
---- 
(of)  | a      | b      | T      | a.getType | b.getType
 a    | true   | true   | false  | false     | false    
 b    | true   | true   | false  | false     | false    

<T> = Dictionary
____  | getTypeName | getFullTypeName   | toString                  
 a    | Dictionary  | system.Dictionary | 'Object:system.Dictionary'
 b    | Dictionary  | system.Dictionary | 'Object:system.Dictionary'
---- 
(as)  | a          | b          | T          | a.getType  | b.getType 
 a    | Dictionary | Dictionary | Null       | Null       | Null      
 b    | Dictionary | Dictionary | Null       | Null       | Null      
---- 
(is)  | a      | b      | T      | a.getType | b.getType
 a    | true   | true   | false  | false     | false    
 b    | true   | true   | false  | false     | false    
---- 
(of)  | a      | b      | T      | a.getType | b.getType
 a    | true   | true   | false  | false     | false    
 b    | true   | true   | false  | false     | false    

<T> = C
____  | getTypeName | getFullTypeName   | toString                  
 a    | A           | .A                | 'Object:.A'               
 b    | B           | .B                | 'Object:.B'               
---- 
(as)  | a          | b          | T          | a.getType  | b.getType 
 a    | A          | A          | Null       | Null       | Null      
 b    | B          | B          | Null       | Null       | Null      
---- 
(is)  | a      | b      | T      | a.getType | b.getType
 a    | true   | true   | false  | false     | false    
 b    | true   | true   | false  | false     | false    
---- 
(of)  | a      | b      | T      | a.getType | b.getType
 a    | true   | true   | false  | false     | false    
 b    | true   | true   | false  | false     | false    

<T> = I
____  | getTypeName | getFullTypeName   | toString                  
 a    | A           | .A                | 'Object:.A'               
 b    | B           | .B                | 'Object:.B'               
---- 
(as)  | a          | b          | T          | a.getType  | b.getType 
 a    | A          | A          | Null       | Null       | Null      
 b    | B          | B          | Null       | Null       | Null      
---- 
(is)  | a      | b      | T      | a.getType | b.getType
 a    | true   | true   | false  | false     | false    
 b    | true   | true   | false  | false     | false    
---- 
(of)  | a      | b      | T      | a.getType | b.getType
 a    | true   | true   | false  | false     | false    
 b    | true   | true   | false  | false     | false  
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment