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


20.5.21 Struct Attributes

The following attributes are defined for struct values.

size

Gives an offset with the storage occupied by the complete struct. Example:

(poke) type Packet = struct { byte s; byte[s] d; }
(poke) (Packet { s = 3 })'size
32UL#b
length

Gives the number of fields stored in the struct. Note that, due to absent fields, this doesn’t always corresponds to the number of fields that appear in the definition of a struct type.

For example, for the following struct type:

type Packet =
  struct
  {
    byte magic: magic in [0xffUB,0xfeUB];
    byte control if magic == 0xfe;
    byte[128] payload;
  };

We get the following results:

(poke) (Packet { magic = 0xff })'length
2UL
(poke) (Packet { magic = 0xfe })'length
3UL
mapped

Boolean indicating whether the struct is mapped.

strict

Boolean indicating whether the struct is mapped in strict mode.

elem (idx)

Element occupying the position idx in the array. Note that this attribute evaluates to a value of type any.

eoffset (idx)

Offset of the field identified by the index idx in the struct. Examples:

(poke) type Foo = struct { int i; long l; }
(poke) Foo{}'eoffset (1)
32#b

If the provided index is out of bounds then E_out_of_bounds is raised.

esize (idx)

Size of the field identified by the index idx in the struct. Examples:

(poke) type Foo = struct { int i; long l; }
(poke) Foo{}'esize (1)
64#b

If the provided index is out of bounds then E_out_of_bounds is raised.

ename (idx)

Name of the field identified by the index idx in the struct. If the field is anonymous then the empty string.

Examples:

(poke) type Foo = struct { int foo; long bar; }
(poke) Foo{}'ename (0)
"foo"

If the provided index is out of bounds then E_out_of_bounds is raised.


Previous: Computed Fields, Up: Structs   [Contents][Index]