Skip to content
jappy edited this page Oct 15, 2011 · 1 revision

Previous - Tea Functions | Table of Contents | Next - Grammar Summary

Although variables in Tea are implicitly typed, Tea is still strongly typed and type conversion operations are performed automatically.

The exception to this are variables that are declaratively typed using define or as. In this case, when an assignment occurs along with a type cast, the compiler will verify that this cast is valid and report an error if not.

Conversions that are automatically applied are usually guaranteed to always succeed. For example, an automatic conversion from Integer to String is preferred over String to Integer because Strings can represent the value of any Integer, but the opposite is not always true. Other kinds of conversions are applied to convert primitive data types to objects and vice versa. When performing addition on an int and an Integer, the Integer is converted to an int in order for the addition to be applied. This conversion could fail, however, if the Integer type being converted references null. In this case, a NullPointerException is thrown.

If the compiler cannot perform a conversion for any reason, the type checker generates an error and the template doesn’t compile. This is generally caused by a true error on the part of the template writer, for example trying to use a boolean where an int should go.

6.1 - Method Binding

Since Java methods can be overloaded to accept parameters of different types, the Tea compiler needs to decide on which method to bind to based on which types fit best. In Java, a cast operation can be applied which forces the binding if there is an ambiguity. Since Tea has no cast operation, the compiler binds to a method by calculating a conversion cost to all the parameters.

A conversion from an int to an Integer is deemed to cost higher than converting an int to a long. If a method is overloaded to accept an Integer or a long as a parameter and the template has an int, the method that takes the long is preferred and is called instead.

6.2 - Primitive Types

Tea treats primitive types as though they were objects, except it will use primitive values wherever possible for performance. If a template passes an int to a method that accepts an Integer, a Number or just any Object, the int is converted to an Integer. If the template passes a Number to a method that accepts an int, the intValue method is called to get at the int value.

Conversion can also be applied between primitives of differing precision. If given a choice, the compiler attempts to apply the conversion with the least loss of precision. A conversion of an int to a double is preferred over the reverse, for example. Also, if given a choice, the compiler will choose to apply a conversion that does not create a new object. Tea only performs conversions between primitive data types that represent numbers. For example, a number cannot be used where a boolean is accepted. Even though Java treats char as an unsigned 16-bit number, Tea treats it as a character, and it often gets converted to a String.

A numerical conversion from an integer to a double or float can always be forced by adding 0.0 or 0.0f. This is useful for displaying the result of a division without losing the fractional part.

6.3 - Conversion to String

Any object and primitive type in Java can be converted to a string. If given the choice, however, the Tea compiler avoids doing this as it is considered a last resort. Binding an int to an Object parameter is preferred over a String parameter because it will convert it to an Integer, which is a more precise representation.

String conversion may cause a formatting operation to be applied. If the object being converted is null, the current null format is applied (see 5.2 Function List). If a date or a number, the current date or number format is applied.

The string concatenation operation, '&', always forces its operands to be converted to strings, and the resulting expression is (of course) a string. Concatenating with "" is a convenient way of forcing an expression to be converted to a string.

6.4 - Variable Promotion

A variable assignment in the scope of a block can be promoted to appear after that scope. This applies to if statements, foreach statements and calls that accept blocks. Consider the following code:

w = "Monday" // w is a String
if (value == "something") {
    x = "Hello" // x is a String
    y = 76.6    // y is a double
    z = "ZZZ"   // z is a String
}
else {
    w = currentDate() // w is a Date
    x = 56            // x is an int
    y = 89            // y is an int
}

After the if statement, the following variables will be available for use: w, x and y with the respective types: Object, Object and double. The z variable is unavailable since it was only assigned in the then part of the if statement, and therefore there is no guarantee that will contain a legal value.

When promoting variables from an inner scope, the Tea compiler chooses a compatible type that represents the merged type as specifically as possible. For objects, it does this by analyzing the inheritance graph. The common type produced for java.util.Vector and java.util.Set is java.util.Collection.

Previous - Tea Functions | Table of Contents | Next - Grammar Summary