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

#dynamic type usage

Stability: 2 - Unstable

required

  • Modules
  • [type safety](type safety)

use types across promiseLand Modules.

##type variables

a type exists parallel as a compilation rule and as a type variable.

  class type SomeType{};
  var myType = SomeType;  // refers to the type variable.
  var a = new SomeType(); // generates a hard coded runtime exception and a compiler warning.

####achievement use type variables to communicate about types.

transfer a type variable to another module

this is a example of a module that defines a type (lets name it "SomeType.pland"):

  class type SomeType{
    some: "member"
  };
  return SomeType;

this module requires the type. note that we can not yet use the type in this example.

  var SomeType = require "SomeType";
  
  SomeType someVar = new SomeType(); // generates a compiler error

the new Operator on a type variable results in undefined behavior.

template syntax

promiseLand defines a new object notation syntax.

  <{
    member: expression
    ...
  }>

the < > brackets were inspired by c++ templates. but the similarities end here.

use a template object to tell the compiler you want to use a type within a function.

  <{
    types: {
      TypeName: typeVariable
    }
  }>function (){
    TypeName a = new TypeName();
  };

the function is compiled at runtime.
keep in mind, generating the function is slow, calling it is fast.

you can now use types in all your promiseLand modules.

  var SomeTypeVariable = require "SomeType";
  return <{
    types: { SomeType: SomeTypeVariable }
  }>(){
    // you see, the type usage is pretty much a new module header
    SomeType a = new SomeType();
  }();

####achievement have a type available in all your modules

compile time error vs runtime error

type mismatch are runtime errors. there are two reasons for this.

  1. consistency
    dynamic type errors will only be known during runtime.
  2. freedom
    you may need to build a function that contains type errors that are never executed. a compile time error would restrain you to much in such scenarios.
    you can also check for incompatibilities by catching runtime type errors.

compiler flags

Stability: 0 - Idea

there may be a compiler flag in the future to indicate weather or not you want to have compile time errors.

the template object could easily contain this information.

Clone this wiki locally