Structs & Classes

Perhaps rather unique among scripting languages, Coyote has a pass-by-value record type: the struct. It also provides a pass-by-reference type via class.

struct SomeStruct
    string foo;
    int bar;

    // implemented as an anonymous trait (see `Traits` below)
    string toString() { return format("%s %d", foo, bar); }
class SomeClass
    string foo;
    int bar;

var s1 = SomeStruct(); = "hello";
var s2 = s1; = "world";	// does not affect s1
assert( != "world");

var c1 = new SomeClass(); = "hello";
var c2 = c1; = "world";	// affects c1
assert( == "world");

Note that Coyote provides explicit nullability, and neither struct nor class is nullable by default:

SomeClass x = null;		// ERROR: `x` is not nullable
SomeClass? y = null;	// ok, `y` is nullable
x = y!;	// if(y is null) error(); else x = y;

int? z = null;	// also works on POD base types
int?? w = null;	// feature TBD (assigns the "innermost" null, so that `w!` works, but `w!!` does not)

Bitfields are supported. Unlike in C, the representation is well-defined (but out of scope of this document).

struct Foo
    uint a:8;
    uint b:10;
    uint c:4;
    uint :0;	// force padding to next unit
    uint x:3;
    uint :1;	// padding
    uint y:18;
    // implicit padding of 10 bits
    string s;	// can be mixed with non-bitfields