Next: Declarations in Structs, Previous: Optional Fields, Up: Structs [Contents][Index]
It is possible to cast a struct of some particular type into another struct type (but not to an union.) Examples:
(poke) type Foo = struct { int i; int j; }; (poke) type Bar = struct { int k; int j; }; (poke) Bar {j=2} as Foo {} Foo {i=0,j=2}
The semantics of the cast are exactly the same than constructing a struct of the target type using the struct provided as an expression to the cast.
This means that some fields in the original struct may be “lost” when the struct is converted to the new type. Consider the following two definitions:
type Foo = struct { int i; long j; }; type Bar = struct { int i; }
Let’s cast some values and see what we obtain:
(poke) Bar {} Bar { i=0x0 } (poke) Bar {} as Foo Foo { i=0x0, j=0x0L } (poke) Foo {} as Bar Bar { i=0x0 }
It is possible to cast an integral struct into an integral type. The value to cast is extracted from the struct fields, and then it is converted to the target type.
Given the following integral struct type:
type Foo = struct int<64> { int<32> f1; uint<32> f2; };
We can operate:
(poke) Foo { f1 = 1, f2 = 1 } as uint<64> + 2 0x100000003UL
Conversely, it is possible to cast an integral value to an integral struct:
(poke) 0x100000003UL as Foo Foo { f1=0x1, f2=0x3U }