Jump to: navigation, search

VJET/Automatic or inferred typing

Vjet supports an automatic style of typing that infers the type based on a usage expression.  This style of typing does not require any comment syntax for support.  A separate document describes all the ways that Vjet knows or infers what type you mean based on usage.

The other place where casting and declarations are comingled is shown in the following examples.  We call this inferred type declaration since the actual declaration type is inferred from the expression versus being explicitly stated. 

In actuality we are saying that via inference, we can determine the right hand type and thus can force the left hand side to be of that type.

Simple literals or other know expressions (such as references to other Vjet know types members or functions) work as well.


var ok = true ;       //<< ; same as typing as boolean
var name = 'worker' ; //<< ; same as typing as String
var count = 12 ;      //<< ; same as typing as int
var ids=['abc','xyz'];//<< ; same as typing as String[]
var re = /1\2/ ;      //<< ; same as typing as RegExp
var isOk = function(){//<< ; same as typing boolean isOk()
    return true ;
"
var sec = Date.UTC(1981, 3, 8, 0) ; //< same as typing as Number


VJETDoc and VJET VJO has internally typed the entire native JavaScript constructs (globals functions/object etc...).  Because of this, it can infer that the return type from Date.UTC is actually Number.  This same inference intelligence can work the same way with types that you or someone else has created.

// here can infer from native JavaScript types


var date = Date ;      //<< ; same as typing as type::Date
var today = new date ; //<< ; we know date holds type Date, so we know to type today as Date

In the previous example, we see how we inferred the type of date to be native JS type reference to Date (type::Date).  This then gave us the information to know that the result of new date is actually Date.

We can infer types from Vjet types themselves.  We can also use the type inferencing for typing functions.


vjo.ctype('Person')
.globals({
      RetirementAge: 65 //< int
})
.props({
    canCollectSocSec: function(age) { //< boolean f(int)
        return age >= RetirementAge ;
    }
})
.protos({
    name: undefined,//< String
    age: 0,       //< int
      constructs: function(name, age) { //< constructs(String, int)
            this.name = name ;
            this.age = age;
      }
})
.endType()


The following code derives all its types and type declarations using inferences from the previously defined ctype, Person.


var Per = Person ;      //<< ; inferred as type::Person
var obj = new Per('MrP' , 33) ;
var p = obj ;           //<< ; inferred as Person from obj
 
var name = p.name ; //<< ; inferred as String
var age = p.age ;   //<< ; inferred as int
 
 
//>> ; inferred result of canCollectSocSec(...) is boolean
var ss = bar.Person.canCollectSocSec(age) ;
 
var old = RetirementAge ; //<< ; inferred from global
 
//>> ; inferred function signature
var canRetire = bar.Person.canCollectSocSec ;
 
//>> ; inferred return type from inferred function
var b = canRetire(age) ;


There are many cases that we can infer typing from.  Everything from inline functions to nested object literals.