Cast: Difference between revisions

From The K Language Wiki
Content added Content deleted
(add cast page)
 
No edit summary
Line 40: Line 40:
`$"zzz" /convert string to symbol
`$"zzz" /convert string to symbol
`zzz
`zzz
</pre>
```
= References =
= References =

Revision as of 04:22, 20 July 2021

Cast
x$y

cast, also known as dollar, casts a value to a different datatype based on a given value. The required casting value for x varies between dialects.

The cast primitive is fully atomic.

K6

Uses symbols to indicate cast target.

Caption text
Symbol Type
`i Integer
`b Boolean
`f Float
`c Character
 `i$"Hello."
72 101 108 108 111 46
 `c$72 101 108 108 111 46
"Hello."
 `f`i`b$31
31 31 1

K3[1]

  0$"123"  /parse as int
123
  0.0$"123.45678"  /parse as float
123.45678
  0.0$"123.456789999999"  /parse as float (note that default precision (\p) is set to 7)
123.4568
  `$"zzz"  /convert string to symbol
`zzz

References