-
-
Notifications
You must be signed in to change notification settings - Fork 9
Casts
IsaacShelton edited this page Mar 21, 2022
·
1 revision
There are two types of casts:
value as Type
and
cast Type value
Both do the same thing, they just have different syntax.
All primitive types can be casted to and from each other.
10 as float
3.1415f as uint
sizeof bool as double
Pointers can be casted to and from each other, regardless of what type they point to.
value1 ptr = null
value2 *int = value1 as *int
value3 *double = cast *double value3
value4 *<float> List = value3 as *<float> List
Implicit and/or explicit user-defined casts can be defined to allowed as
and cast
to work on non-standard types.
import basics
struct Vector3f (x, y, z float)
func vector3f(x, y, z float) Vector3f {
v Vector3f
v.x = x
v.y = y
v.z = z
return v
}
func __as__(v Vector3f) float {
// When casting Vector3f to float, return length of vector
return sqrtf(v.x * v.x + v.y * v.y + v.z * v.z)
}
func main {
vector Vector3f = vector3f(1.0f, 3.0f, 5.0f)
printf("%hf\n", cast float vector)
}
See user-defined casts for more information