F.E.L.T.

Functionally Equivalent Language Translation

CAST

(cast int var-name)         ; convert var-name to an integer
(cast string expression)    ; convert expression to a String
(defvar x 3.14)
(cast int x)
(cast string x)

See also AS

This performs type coercion for your chosen output language. This instruction represents the act of "casting" if you come from C/C++/C# backgrounds, otherwise described as "type coercion" in other languages. It can result in loss of accuracy or a total wipeout according to your target language.


Type Coercion / Casting

The instruction is composed of the word at or cast followed a string that makes sense for the target platform. Yes, this is shaky ground because apart from a few very common types like int or String for example, you stand a very good chance of not being able to generate language neutral code when type casting.

Type casting can be fairly dangerous unless you know exactly what context your code is operating withing anyway so FELT is not too worried. If you absolutely must do these sorts of things in your program, and sometimes you just have to, then FELT would prefer it if you wrapped it in a specific type-wrapping function and then wrapped that with an #F:WHEN instruction, thus keeping everything cleanly separated.

With that in mind, the following examples are shown in PHP only just so that you can see how it works.

(defvar f 3.14)
(emit "Started with: " f "\n")
(emit " as in int: " (as int f) "\n")
(emit " as an array: " (print-r (cast Array f) #t) "\n")

And the rendered output is:

$f = 3.14;
echo "Started with: ", $f, "\n";
echo " as in int: ", (int)$f, "\n";
echo " as an array: ", print_r((Array)$f, TRUE), "\n";

And the output from piping through PHP command line is:

Started with: 3.14
 as in int: 3
 as an array: Array
(
    [0] => 3.14
)

Use with extreme caution!