.\" Made by website/plain/build.mjs from website/words/union.md; change that file. .TH SG-UNION 7 2026-11-28 "SG" "SG reference" .SH NAME union \- a type whose fields all lie in the same bytes; it holds one of them at a time, read inside unsafe .SH SYNOPSIS .TP \fBunion Bits { uint word / float number }\fR a union: one field to a line, as a struct is written .TP \fBBits { number = 1.5 }\fR a value, built with one field; the bytes it does not cover are zero .TP \fBbits.word = 3\fR a field written whole, with no unsafe .TP \fBunsafe bits.word\fR a field read as its own type, whichever field was written last .TP \fBunion Address : Copyable, Formattable { ... }\fR copied instead of moved, and printed by a format of its own .TP \fBAddress.fromBytes(bytes)\fR a union of numbers read out of exactly as many bytes as it takes .TP \fBMemory.sizeOf(), Memory.alignOf()\fR the size and the alignment C gives it .SH DESCRIPTION A union is a type whose fields all start where it starts, so a value of it holds one field at a time. As C lays a union out, its alignment is that of its most aligned field, and its size that of its largest field, rounded up to a multiple of the alignment: a \fBbyte[3]\fR and a \fBushort\fR take four bytes. It is written as a struct is, one field to a line, and has methods, \fBpublic\fR and \fB: Copyable\fR as a struct has. It holds plain data only: numbers, \fBbool\fR, \fBchar\fR, raw pointers, \fBCFunc\fR, and arrays, structs and unions of them. A field has no default value, and a union has no bit fields. .PP A union is built with exactly one field, and the bytes that field does not cover are zero. Writing a field needs no \fBunsafe\fR: it writes over the bytes of that field and leaves the rest as they were. Reading one is inside \fBunsafe\fR, since the union does not know which field it holds: the bytes are read as the field's own type, whichever field was written last. A union with a tag beside it, in a struct, is how C keeps one of several values; an [\fBenum\fR](enum.md) with values keeps the tag itself and its \fBmatch\fR checks it, so a union is for a layout that C, a file or a device gives. .PP A field is written whole. Changing what lies inside it (\fBword.halves.low = 1\fR, \fBbits.bytes[0] = 1\fR) or changing it in place (\fBbits.word += 1\fR) would read it first, so the field is read, changed as a copy and written back. A field is never lent, as a place in raw memory is not: \fBread bits.word\fR is an error whose hint copies it out. A method of the union itself is lent the whole value as usual. .PP A union is not compared, hashed or ordered by its fields: \fB: Equatable\fR, \fB: Hashable\fR and \fB: Comparable\fR are errors, and so is \fB: Formattable\fR without a \fBformat\fR of its own. There is no \fBmatch\fR on a union. Each of these reads the field it holds inside \fBunsafe\fR\&. .PP \fBBits.fromBytes(bytes)\fR reads a union out of exactly as many bytes as it takes, as they lie in memory, with no \fBunsafe\fR, when every pattern of bits is a value of each field: numbers, and arrays, structs and unions of them. A \fBbool\fR, a \fBchar\fR or a raw pointer in it is an error there. For the bits of a \fBfloat\fR or a \fBdouble\fR, \fBtoBits()\fR and \fBfloat.fromBits(bits)\fR need neither a union nor \fBunsafe\fR\&. .PP In the interpreter a union is its bytes, as in C. The interpreter stops a \fBchar\fR read from bits that are no character; compiled code reads what lies there. .SH EXAMPLES .SS "One field at a time" .RS 4 .nf union Bits { uint word float number byte[4] bytes } void main() { var bits = Bits { number = 1.0 } print($"{unsafe bits.word:x8}") bits.word = 0x40490fdb print(unsafe bits.number) print(Memory.sizeOf()) print(Memory.alignOf()) } .fi .RE .PP .RS 4 .nf 3f800000 3.1415927 4 4 .fi .RE .PP The three fields share four bytes. Writing \fBword\fR needs no \fBunsafe\fR; reading \fBnumber\fR after it reads those bytes as a \fBfloat\fR\&. .SS "The bytes a field does not cover" .RS 4 .nf union Wide { byte low long whole byte[8] bytes } void main() { var w = Wide { low = 0xff } print(unsafe w.bytes) w.whole = -1 w.low = 0 print(unsafe w.bytes) } .fi .RE .PP .RS 4 .nf [255, 0, 0, 0, 0, 0, 0, 0] [0, 255, 255, 255, 255, 255, 255, 255] .fi .RE .PP Built with \fBlow\fR, the seven bytes after it are zero. Writing \fBlow\fR later writes its one byte and leaves the other seven as \fBwhole\fR left them. .SS "Methods, a format, and fromBytes" .RS 4 .nf union Address : Copyable, Formattable { uint whole byte[4] parts Address create(byte a, byte b, byte c, byte d) { return Address { parts = byte[4] { a, b, c, d } } } bool isPrivate(read this) { var p = unsafe this.parts return p[0] == 10 || (p[0] == 192 && p[1] == 168) } bool sameAs(read this, Address other) { return unsafe (this.whole == other.whole) } void format(read this, ref Formatter f) { var p = unsafe this.parts f.write($"{p[0]}.{p[1]}.{p[2]}.{p[3]}") } } void main() { var home = Address(192, 168, 1, 20) print($"{home} {home.isPrivate()}") var packet = List { 8, 8, 4, 4 } var dns = Address.fromBytes(read packet) print($"{dns} {dns.isPrivate()}") print(dns.sameAs(Address(8, 8, 4, 4))) print(dns.sameAs(home)) } .fi .RE .PP .RS 4 .nf 192.168.1.20 true 8.8.4.4 false true false .fi .RE .PP An address is four bytes, or one \fBuint\fR to compare in one step. \fBfromBytes\fR takes the four bytes of a packet with no \fBunsafe\fR, since every pattern of bits is a value of both fields. \fBunsafe\fR in front of a value marks one expression, so the parentheses put both reads of \fBsameAs\fR inside it. .SS "A tag beside it" .RS 4 .nf enum Kind { Integer Real Spot } struct Point : Copyable { int x int y } union Payload { long integer double real Point spot } struct Value { Kind kind Payload data void moveRight(ref this) { if this.kind is Spot { var p = unsafe this.data.spot p.x += 1 this.data.spot = p } } string describe(read this) { return match read this.kind { Integer => $"integer {unsafe this.data.integer}".toString() Real => $"real {unsafe this.data.real}".toString() Spot => $"spot {unsafe this.data.spot.x}, {unsafe this.data.spot.y}".toString() } } } void main() { var values = List { Value { kind = Kind.Integer, data = Payload { integer = 9000000000 } }, Value { kind = Kind.Real, data = Payload { real = 0.25 } }, Value { kind = Kind.Spot, data = Payload { spot = Point { x = 3, y = 4 } } }, } values[2].moveRight() for value in read values { print(value.describe()) } print(Memory.sizeOf()) } .fi .RE .PP .RS 4 .nf integer 9000000000 real 0.25 spot 4, 4 8 .fi .RE .PP The struct keeps the tag, and each read of the union trusts it. \fBmoveRight\fR cannot write \fBthis.data.spot.x\fR alone: it reads the point, changes the copy and writes it back whole. .SS "In raw memory" .RS 4 .nf union Cell { long integer double real } void main() { var cells = Memory.allocate(3) unsafe { cells[0].integer = 40 cells[0].integer = cells[0].integer + 2 cells[1].real = 2.5 cells[2] = Cell { real = cells[1].real * 4.0 } } var view = unsafe Memory.view(cells, 3) print(unsafe view[0].integer) print(unsafe view[2].real) print(Memory.sizeOf() * view.length()) unsafe Memory.free(cells) } .fi .RE .PP .RS 4 .nf 42 10 24 .fi .RE .PP A union is plain data, so it lies in raw memory as any item does. Inside the \fBunsafe\fR block its fields are read and written where they lie; through the view each read is still marked. .SH "WHAT THE COMPILER REFUSES" .SS "A field read without unsafe" .RS 4 .nf union Bits { uint word float number } void main() { var bits = Bits { number = 1.0 } print(bits.word) } .fi .RE .PP .RS 4 .nf error: a field of a union is read as its own type, whichever field was written last, so it is written inside `unsafe` --> example.sg:8:11 8 | print(bits.word) | ^^^^^^^^^ hint: `unsafe bits.word`, or `unsafe { ... }` around several statements .fi .RE .SS "A field changed in place" .RS 4 .nf union Bits { uint word float number } void main() { var bits = Bits { word = 1 } bits.word += 1 print(unsafe bits.word) } .fi .RE .PP .RS 4 .nf error: a field of a union is read inside `unsafe`, so it is not changed in place --> example.sg:8:5 8 | bits.word += 1 | ^^^^^^^^^ hint: `bits.word = unsafe bits.word + ...` .fi .RE .SS "A part of a field written" .RS 4 .nf union Bits { uint word byte[4] bytes } void main() { var bits = Bits { word = 0 } bits.bytes[0] = 1 print(unsafe bits.word) } .fi .RE .PP .RS 4 .nf error: a field of a union is written whole: what lies inside it is not written on its own --> example.sg:8:5 8 | bits.bytes[0] = 1 | ^^^^^^^^^^ hint: read it, change the copy and write it back: `var part = unsafe bits.bytes`, then `bits.bytes = part` .fi .RE .SS "A field lent" .RS 4 .nf union Bits { uint word float number } void show(read uint word) { print(word) } void main() { var bits = Bits { number = 1.0 } unsafe { show(read bits.word) } } .fi .RE .PP .RS 4 .nf error: a field of a union is not lent: the checker does not know that the union holds it --> example.sg:13:19 13 | show(read bits.word) | ^^^^^^^^^ hint: copy it out: `var part = unsafe bits.word` .fi .RE .SS "Built with two fields" .RS 4 .nf union Bits { uint word float number } void main() { var bits = Bits { word = 1, number = 2.0 } print(unsafe bits.word) } .fi .RE .PP .RS 4 .nf error: a union is built with one of its fields, and this gives 2 a union holds one field at a time: they all start where it starts --> example.sg:7:33 7 | var bits = Bits { word = 1, number = 2.0 } | ^^^^^^ .fi .RE .SS "A field that owns memory" .RS 4 .nf union Name { string text int code } void main() { } .fi .RE .PP .RS 4 .nf error: a union holds plain data, and `string` is not plain data is numbers, `bool`, `char`, raw pointers, `CFunc`, and arrays, structs and unions of them: what C lays out --> example.sg:2:5 2 | string text | ^^^^^^ .fi .RE .PP Whichever field was written last, the bytes of a text would be read as a number, or a number as a text; and nothing would know to free the text. .SS "Compared by its fields" .RS 4 .nf union Bits : Equatable { uint word float number } void main() { } .fi .RE .PP .RS 4 .nf error: union `Bits` cannot be `Equatable` a union does not know which of its fields it holds, so it is not compared by them --> example.sg:1:14 1 | union Bits : Equatable { | ^^^^^^^^^ hint: read the field it holds inside `unsafe` and compare that .fi .RE .SH "SEE ALSO" .BR sg-unsafe (7), .BR sg-packed (7), .BR sg-struct (7), .BR sg-enum (7), .BR sg-raw (7), .BR sg-align (7) .PP https://sg\-lang.org/reference/union.html