- Installing
- Hello World
- Spaces
- Identation
- Basics
- Comments
- Output
- ++ method
- Assignments and types
- Operator Precedence
- String interpolation
- Input
- Simple arithmetic
- Multiline strings
- Assert
- Arrays and literals arrays
- Literal intervals
- If, for, while and repeat-until
- Message sends
- Conversion between basic type values
- Anonymous functions
- Evaluating an anonymous function:
- Functions with method foreach:
- Prototypes
- Packages
- Gradual typing
- Generic prototypes
- Important metaobjects
- The CyanInterpreter prototype
Conversion between basic types
In Cyan, there is no automatic conversion between types.
var Byte byte0;
byte0 = 0;// error, 0 is Int
byte0 = 0;// error, 0 is Int
Basic types can be have your values converted into another basic type through special keywords such as:
func asByte -> Byte func asShort -> Short func asLong -> Long func asFloat -> Float func asDouble -> Double func asChar -> Char func asBoolean -> Boolean func asInt -> Int func asString -> String
And then we have the possibility to perform a conversion between any of the basic types.
var Double d = 1.0; var Int n = d asInt; var String s = n asString;
The methods of the String prototype return an union of the type with Nil, as in
func asInt -> Int|Nil
Hence, they should be used with the cast or type-case statements:
cast n = "100" asInt { (n*n - 2*n) println }
A line read as a string can be converted into an Int as shown in the next example.
cast n = (In readLine) asInt { (n*n - 2*n) println }