-
-
Notifications
You must be signed in to change notification settings - Fork 9
Typeinfo Expression
IsaacShelton edited this page Mar 21, 2022
·
1 revision
The runtime-type information of a type can be obtained at compile-time by using the typeinfo
keyword followed by a type
typeinfo int
typeinfo <String> List
typeinfo *ubyte
The resulting value of the expression is a *AnyType
which points to the runtime type information. Multiple typeinfo
expressions given the same type are guaranteed to return the same pointer value. Therefore, the following is always true:
typeinfo $T == typeinfo $T
where $T
is any valid type
import basics
struct IntOrFloat (type *AnyType, data IntOrFloatData)
union IntOrFloatData (int_value int, float_value float)
func main {
my_value IntOrFloat = 10.0f
my_value.print()
my_other_value IntOrFloat = 10
my_other_value.print()
}
func print(this *IntOrFloat) {
if this.type == typeinfo int {
printf("%d\n", this.data.int_value)
} else if this.type == typeinfo float {
printf("%hf\n", this.data.float_value)
}
}
implicit func __as__(value int) IntOrFloat {
res IntOrFloat
res.type = typeinfo int
res.data.int_value = value
return res
}
implicit func __as__(value long) IntOrFloat {
res IntOrFloat
res.type = typeinfo int
res.data.int_value = value as int
return res
}
implicit func __as__(value float) IntOrFloat {
res IntOrFloat
res.type = typeinfo float
res.data.float_value = value
return res
}