Skip to content

Instantly share code, notes, and snippets.

@ellemenno
Last active December 22, 2015 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ellemenno/6477448 to your computer and use it in GitHub Desktop.
Save ellemenno/6477448 to your computer and use it in GitHub Desktop.
Comparing type operator results in Loom on function args vs. literal values

Testing type ops in Loom

is, as, instanceof return expected results on literal values, but not when the same values are passed into a function and the type ops are applied to the function argument variables. NOTE: when testing with trace() be aware that Sprint 30 (and earlier?) contained a bug that caused trace to print all Booleans as true. This is resolved in SDK v1.1.2738 (and Sprint 31).

(results below)

TypeChecker.ls

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

    public class TypeOps 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('');
            testTypeOps();
        }

        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);
        }

        private function echoTypeOps(value:Object, type:Type):void
        {
            trace('value:', '<' +value +'>', 'type:', '<' +type.getFullName() +'>');
            trace('value.getFullTypeName() == type.getFullName()             ', '-->', (value.getFullTypeName() == type.getFullName()));
            trace('value instanceof type                                     ', '-->', (value instanceof type));
            trace('value is type                                             ', '-->', (value is type));
            trace('(value as type) != null                                   ', '-->', ((value as type) != null));
        }

        private function testTypeOps():void
        {
            trace('Boolean');
            trace('- literal');
            trace('true.getFullTypeName() == Boolean.getFullName()           ', '-->', (true.getFullTypeName() == (Boolean as Type).getFullName()) );
            trace('true instanceof Boolean                                   ', '-->', (true instanceof Boolean) );
            trace('true is Boolean                                           ', '-->', (true is Boolean) );
            trace('(true as Boolean) != null                                 ', '-->', ((true as Boolean) != null) );
            trace('- func args');
            echoTypeOps( (true), (Boolean) );
            trace('');

            trace('Number');
            trace('- literal');
            trace('123.getFullTypeName() == Number.getFullName()             ', '-->', ((123 as Number).getFullTypeName() == (Number as Type).getFullName()) );
            trace('123 instanceof Number                                     ', '-->', (123 instanceof Number) );
            trace('123 is Number                                             ', '-->', (123 is Number) );
            trace('(123 as Number) != null                                   ', '-->', ((123 as Number) != null) );
            trace('- func args');
            echoTypeOps( (123), (Number) );
            trace('');

            trace('Number as String');
            trace('- literal');
            trace('123.getFullTypeName() == String.getFullName()             ', '-->', ((123 as Object).getFullTypeName() == (String as Type).getFullName()) );
            trace('123 instanceof String                                     ', '-->', 'error' );//(123 instanceof String) );
            trace('123 is String                                             ', '-->', (123 is String) );
            trace('(123 as String) != null                                   ', '-->', ((123 as String) != null) );
            trace('- func args');
            echoTypeOps( (123), (String) );
            trace('');

            trace('String');
            trace('- literal');
            trace('"abc".getFullTypeName() == String.getFullName()           ', '-->', ("abc".getFullTypeName() == (String as Type).getFullName()) );
            trace('"abc" instanceof String                                   ', '-->', ("abc" instanceof String) );
            trace('"abc" is String                                           ', '-->', ("abc" is String) );
            trace('("abc" as String) != null                                 ', '-->', (("abc" as String) != null) );
            trace('- func args');
            echoTypeOps( ("abc"), (String) );
            trace('');

            trace('Function');
            trace('- literal');
            trace('(function(){}).getFullTypeName() == Function.getFullName()', '-->', ((function(){}).getFullTypeName() == (Function as Type).getFullName()) );
            trace('(function(){}) instanceof Function                        ', '-->', ((function(){}) instanceof Function) );
            trace('(function(){}) is Function                                ', '-->', ((function(){}) is Function) );
            trace('((function(){}) as Function) != null                      ', '-->', (((function(){}) as Function) != null) );
            trace('- func args');
            echoTypeOps( (function(){}), (Function) );
            trace('');

            trace('Vector');
            trace('- literal');
            trace('[].getFullTypeName() == Vector.getFullName()              ', '-->', ([].getFullTypeName() == (Vector as Type).getFullName()) );
            trace('[] instanceof Vector                                      ', '-->', ([] instanceof Vector) );
            trace('[] is Vector                                              ', '-->', ([] is Vector) );
            trace('([] as Vector) != null                                    ', '-->', (([] as Vector) != null) );
            trace('- func args');
            echoTypeOps( ([]), (Vector) );
            trace('');

            trace('Dictionary');
            trace('- literal');
            trace('{}.getFullTypeName() == Dictionary.getFullName()          ', '-->', ({}.getFullTypeName() == (Dictionary as Type).getFullName()) );
            trace('{} instanceof Dictionary                                  ', '-->', ({} instanceof Dictionary) );
            trace('{} is Dictionary                                          ', '-->', ({} is Dictionary) );
            trace('({} as Dictionary) != null                                ', '-->', (({} as Dictionary) != null) );
            trace('- func args');
            echoTypeOps( ({}), (Dictionary) );
            trace('');

            trace('Class as self');
            trace('- literal');
            trace('label.getFullTypeName() == SimpleLabel.getFullName()      ', '-->', (label.getFullTypeName() == (SimpleLabel as Type).getFullName()) );
            trace('label instanceof SimpleLabel                              ', '-->', (label instanceof SimpleLabel) );
            trace('label is SimpleLabel                                      ', '-->', (label is SimpleLabel) );
            trace('(label as SimpleLabel) != null                            ', '-->', ((label as SimpleLabel) != null) );
            trace('- func args');
            echoTypeOps( (label), (SimpleLabel) );
            trace('');

            trace('Class as Parent');
            trace('- literal');
            trace('this.getFullTypeName() == Application.getFullName()       ', '-->', (this.getFullTypeName() == (Application as Type).getFullName()) );
            trace('this instanceof Application                               ', '-->', (this instanceof Application) );
            trace('this is Application                                       ', '-->', (this is Application) );
            trace('(this as Application) != null                             ', '-->', ((this as Application) != null) );
            trace('- func args');
            echoTypeOps( (this), (Application) );
            trace('');

            trace('Class as grandparent');
            trace('- literal');
            trace('label.getFullTypeName() == DisplayObject.getFullName()    ', '-->', (label.getFullTypeName() == (DisplayObject as Type).getFullName()) );
            trace('label instanceof DisplayObject                            ', '-->', (label instanceof DisplayObject) );
            trace('label is DisplayObject                                    ', '-->', (label is DisplayObject) );
            trace('(label as DisplayObject) != null                          ', '-->', ((label as DisplayObject) != null) );
            trace('- func args');
            echoTypeOps( (label), (DisplayObject) );
            trace('');

            trace('Class as Object');
            trace('- literal');
            trace('this.getFullTypeName() == Object.getFullName()            ', '-->', (this.getFullTypeName() == (Object as Type).getFullName()) );
            trace('this instanceof Object                                    ', '-->', (this instanceof Object) );
            trace('this is Object                                            ', '-->', (this is Object) );
            trace('(this as Object) != null                                  ', '-->', ((this as Object) != null) );
            trace('- func args');
            echoTypeOps( (this), (Object) );
            trace('');
        }
    }
}

Results

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

Boolean
- literal
true.getFullTypeName() == Boolean.getFullName()            --> true
true instanceof Boolean                                    --> true
true is Boolean                                            --> true
(true as Boolean) != null                                  --> true
- func args
value: <true> type: <system.Boolean>
value.getFullTypeName() == type.getFullName()              --> true
value instanceof type                                      --> false
value is type                                              --> false
(value as type) != null                                    --> false

Number
- literal
123.getFullTypeName() == Number.getFullName()              --> true
123 instanceof Number                                      --> true
123 is Number                                              --> true
(123 as Number) != null                                    --> true
- func args
value: <123> type: <system.Number>
value.getFullTypeName() == type.getFullName()              --> true
value instanceof type                                      --> false
value is type                                              --> false
(value as type) != null                                    --> false

Number as String
- literal
123.getFullTypeName() == String.getFullName()              --> false
123 instanceof String                                      --> error
123 is String                                              --> true
(123 as String) != null                                    --> true
- func args
value: <123> type: <system.String>
value.getFullTypeName() == type.getFullName()              --> false
value instanceof type                                      --> false
value is type                                              --> false
(value as type) != null                                    --> false

String
- literal
"abc".getFullTypeName() == String.getFullName()            --> true
"abc" instanceof String                                    --> true
"abc" is String                                            --> true
("abc" as String) != null                                  --> true
- func args
value: <abc> type: <system.String>
value.getFullTypeName() == type.getFullName()              --> true
value instanceof type                                      --> false
value is type                                              --> false
(value as type) != null                                    --> false

Function
- literal
(function(){}).getFullTypeName() == Function.getFullName() --> true
(function(){}) instanceof Function                         --> true
(function(){}) is Function                                 --> true
((function(){}) as Function) != null                       --> true
- func args
value: <system.Function> type: <system.Function>
value.getFullTypeName() == type.getFullName()              --> true
value instanceof type                                      --> false
value is type                                              --> false
(value as type) != null                                    --> false

Vector
- literal
[].getFullTypeName() == Vector.getFullName()               --> true
[] instanceof Vector                                       --> true
[] is Vector                                               --> true
([] as Vector) != null                                     --> true
- func args
value: <> type: <system.Vector>
value.getFullTypeName() == type.getFullName()              --> true
value instanceof type                                      --> false
value is type                                              --> false
(value as type) != null                                    --> false

Dictionary
- literal
{}.getFullTypeName() == Dictionary.getFullName()           --> true
{} instanceof Dictionary                                   --> true
{} is Dictionary                                           --> true
({} as Dictionary) != null                                 --> true
- func args
value: <Object:system.Dictionary> type: <system.Dictionary>
value.getFullTypeName() == type.getFullName()              --> true
value instanceof type                                      --> false
value is type                                              --> false
(value as type) != null                                    --> false

Class as self
- literal
label.getFullTypeName() == SimpleLabel.getFullName()       --> true
label instanceof SimpleLabel                               --> true
label is SimpleLabel                                       --> true
(label as SimpleLabel) != null                             --> true
- func args
value: <Object:loom2d.ui.SimpleLabel> type: <loom2d.ui.SimpleLabel>
value.getFullTypeName() == type.getFullName()              --> true
value instanceof type                                      --> false
value is type                                              --> false
(value as type) != null                                    --> false

Class as Parent
- literal
this.getFullTypeName() == Application.getFullName()        --> false
this instanceof Application                                --> true
this is Application                                        --> true
(this as Application) != null                              --> true
- func args
value: <Object:.TypeOps> type: <loom.Application>
value.getFullTypeName() == type.getFullName()              --> false
value instanceof type                                      --> false
value is type                                              --> false
(value as type) != null                                    --> false

Class as grandparent
- literal
label.getFullTypeName() == DisplayObject.getFullName()     --> false
label instanceof DisplayObject                             --> true
label is DisplayObject                                     --> true
(label as DisplayObject) != null                           --> true
- func args
value: <Object:loom2d.ui.SimpleLabel> type: <loom2d.display.DisplayObject>
value.getFullTypeName() == type.getFullName()              --> false
value instanceof type                                      --> false
value is type                                              --> false
(value as type) != null                                    --> false

Class as Object
- literal
this.getFullTypeName() == Object.getFullName()             --> false
this instanceof Object                                     --> true
this is Object                                             --> true
(this as Object) != null                                   --> true
- func args
value: <Object:.TypeOps> type: <system.Object>
value.getFullTypeName() == type.getFullName()              --> false
value instanceof type                                      --> false
value is type                                              --> false
(value as type) != null                                    --> false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment