Skip to content
soliton4 edited this page Dec 9, 2014 · 4 revisions

#type safety

Stability: 3 - Stable

requires

*promise

the biggest feature in terms of functionality.

type

add the keyword type to your class definition to create a new type.

  class type MyClass {
    constructor: function(){},
    member: "default value"
  };

the same scope rules as for functions and variables apply to classes.

class variable

a class variable is returned by the class type expression.

  class type MyClass {
    constructor: function(){},
    member: "default value"
  };
  
  var theClassVariable = MyClass;

you can use it to address your created type within classical JavaScript. however it can not be used to create instances. it's not a constructor.

var is a type

simply assume there exists a type named var in JavaScript / promiseLand.

var variable;

declares a variable of type var.

now replace the var with the type we created above.

MyClass typedVariable;

the typedVariable is now of type MyClass. it can only be assigned instances of MyClass. it can only be used where instances of MyClass are expected.

####type description for parameters and members

for typesafety of parameters put the name of the type in front of the parameter name.

  function myFun(MyClass parameter1){
    MyClass variable = paramter1;
    var some = parameter1; // will trigger a compile warning as well as a runtime error
  };

the function itself will be of a function type, containing the result type of a function and the types of parameters.

for class members do it in the same manner

  class type AnotherType {
    MyClass member: new MyClass(),
    regularMember: "default",
    var alsoRegularMember: "you can use var like a type"
  };

####type description for return types

put the Class name in front of the function name.

  function MyClass myFun(var parameter){
    MyClass variable = new MyClass();
    return variable;
  };

####achivement
use typesafety within your program

implementation

instances of type classes are arrays.
accessing a array is considerably faster than accessing members by name.
since every member access is translated, it's a good opportunity to increase the runtime behavior.

####achivement
program superfast code

connect

read about the connect feature.

modifiers

typesafety provides the foundation for a lot of features.

  • track
    keep track of instances / have a destructor
  • sync
    sync instances over several frames
  • unique
    address your instances by id / make a singleton
  • savable
    an adapter to load and save a instance

dynamic type usage

types can not leave promiseLand. but they can be used across modules.
read more about [dynamic type usage](dynamic type usage).

Clone this wiki locally