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();
s1.foo = "hello";
var s2 = s1;
s2.foo = "world";	// does not affect s1
assert(s1.foo != "world");

var c1 = new SomeClass();
c1.foo = "hello";
var c2 = c1;
c2.foo = "world";	// affects c1
assert(c1.foo == "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
}