KINDS
Record
5 min
a record is an immutable datatype that contains a collection of named fields it should be directly compared to a structure docid\ jeae9scytiv75 ywjzlcw and used when you need immutability the fields of a record must be a record datatype themselves a record is a type you can declare a new instance of it and such can have local defined constructors docid 8cur6rtdh4zhi8fdh6vho declaration https //docs shiftlang com/structure#ns declaration to declare a new record define a top level construct using the keyword record followed by the desired identifier the name must be unique within the corresponding namespace namespace a structure contains any number of named fields field all fields must be record datatypes they are immutable and cannot have their value changed after execution of the constructor they can also be initialized to initial values //physically located in records record csharpboundfields { int a; double b = 0 07; bool c = false; } structure iamnotarecord { int honkhonk; } record examplerecord { string name = "bobby buckets"; csharpboundfields mahfields; iamnotarecord failhard;//this line causes a compile error this examplerecord(int b) { mahfields b = b; } } remarks shift records are similiar to c# records but with the addition guarentee of only containing other records with init setters a record can only inherit from other records all c# primitives are exposed as records in shift c# equivalent all fields in a shift record are like init only properties in a c# record furthermore the compiler enforces that any type used as a property conforms to the same constraints public record csharpboundfields { public int a { get; init set; } = new int(); public double b { get; init set; } = new double(); public bool c { get; init set; } = new bool(); } public record examplerecord { public string name { get; init set; } = "bobby buckets"; public csharpboundfields mahfields { get; init set; } = new csharpboundfields(); public examplerecord(int b) { mahfields b = b; } } usage all of a record's fields can be accessed when you have an instance of a type records are a immutable datatype so their fields cannot be assigned a new value at runtime var boundfields = new csharpboundfields{ b = 7 3848, c = true }; boundfields a = 3;//compile error because csharpboundfields is a record type var example = new examplerecord();//default constructors always exist c#'s with syntax is supported in shift to allow non destructible mutation of records shift //using the code from the above example var saferecordmutation = boundfields with { a = 3 }; it's important to note that a record within a structure is still immutable nothing about nesting them in structures change how they behave
