Jump to: navigation, search

VJET/Casting using VJETDoc

VJET Doc uses two less than signs or greater than signs to indicate casting or force type. 

//<< are valid cast starting comment segments.

As with the previous VJETDoc introducers, there must not be any whitespace between the entire comment introducer.  Thus //>  > is an invalid VJETDoc vs. //>> which is a valid Vjet cast declaration.

Here are some examples:


// Assignment casting
var person2 = PFactory.create('MrP', 30) //<< Person

If the type of the left-hand side of the assignment is known, a shorthand cast can be used. For example,


var x ; //< X
x = create() ; //<< ; cast create() to type X

If the variable type is not known, the cast operation can do double duty by typing and casting.


var d = something() ;
//<< Date ; this doubles as declaration and cast of something()

Inline expressions, arguments, and return values sometimes must be cast. The syntax for doing this is similar to what we have seen so far but embedded inline. Some examples should clarify.


// Example where we are casting a passed in argument
 
doit: function(date) {  //< void doit(Date) ; Declare doit to accept Date
    ...
}
 
var d2 ; //< Object
d2 = new Date() ; // ok since Date is an Object
 
doit(d2 /*<<*/) ;
doit(/*>>*/d2) ;

Note that we had to use the multiline comment syntactic form because the //> or //< comment forms would have consumed the rest of the current line as a comment. In this example, we are doing a shorthand cast to the type expected by the function.

Casting in an expression is similar. In this example, the inline expression cast is highlighted.


vjo.ctype('B')
.protos({
    hello: function() { //< public String
        return 'Hello!' ;
    }
}).endType() ;
 
 
vjo.ctype('A')
.needs('B')
.protos({
    getB: function() { //< public Object
        return new B() ;
    }
 
var a = new A() ; //< A
var greeting ;    //< String
greeting = (/*>> B */ a.getB()).hello() ;
 
// return casting
getIt: function() {
    return /*> String*/ expr;
}

As in Java, casting may require an extra set of parentheses to properly do the cast.