Next: , Previous: , Up: Structs   [Contents][Index]

14.5.15 Casting Structs

It is possible to cast a struct of some particular type into another struct type. 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 {
(poke) Bar {} as Foo
Foo {
(poke) Foo {} as Bar
Bar {

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