.\" Made by website/plain/build.mjs from website/words/new.md; change that file. .TH SG-NEW 7 2026-11-28 "SG" "SG reference" .SH NAME new \- puts a value on the heap and gives the pointer that owns it .SH SYNOPSIS .TP \fBnew Point { x = 3, y = 4 }\fR a struct on the heap; the value is a Point* .TP \fBnew Expr.Number(1)\fR a variant of an enum on the heap: an Expr* .TP \fBnew x\fR any other value: new List { 7, 9 } is a List* .TP \fBT*\fR a pointer that owns the value it points to .TP \fBT*?\fR the same, or null .TP \fBnew shared x\fR a value with several owners: see shared .SH DESCRIPTION \fBnew\fR puts a value on the heap and gives a \fBT*\fR, a pointer that owns it. It does not mean "make an object": a struct is made without it and lives where its variable is. A value is never put on the heap by itself, so each \fBnew\fR in the code is one allocation, and a \fBPoint\fR is not taken where a \fBPoint*\fR is wanted. .PP Through a \fBT*\fR, fields and methods are reached with \fB\&.\fR, as on the value itself, and \fBprint\fR writes what it points to. \fB==\fR and the order compare what two pointers own: \fBa == b\fR on two \fBPoint*\fR compares the points, as on two \fBint*\fR it compares the numbers. The other operators work on numbers, not on a \fBT*\fR; a number needs no \fBnew\fR\&. A borrow of a \fBT*\fR goes where a borrow of a \fBT\fR is wanted: \fBshow(read p)\fR for \fBvoid show(read Point p)\fR\&. .PP A \fBT*\fR has one owner, as every value does. \fBvar q = p\fR moves the pointer, not the value, and \fBp\fR can no longer be used. The value is freed, with everything it owns, where its owner ends: at the end of its block, when the pointer is written over, or at the end of the function it was given to. There is no \fBdelete\fR, no garbage collector and no count of owners. .PP A type that holds itself, such as the node of a list or a tree, needs a pointer for the part that repeats: \fBNode*? next\fR\&. Without it the type would have no size. .SH EXAMPLES .SS "A value on the heap" .RS 4 .nf struct Point : Formattable { int x int y } void main() { Point* p = new Point { x = 3, y = 4 } p.x += 1 print($"{p.x}, {p.y}") print(p) } .fi .RE .PP .RS 4 .nf 4, 4 Point { x = 4, y = 4 } .fi .RE .PP \fBp\fR is a \fBPoint*\fR; \fBvar p = new Point { ... }\fR gives it the same type. \fBp.x\fR reaches the field through the pointer, and \fBprint(p)\fR writes the point, not an address. .SS "When it is freed" .RS 4 .nf struct Page : Disposable { int number void dispose(ref this) { print($"free page {this.number}") } } void main() { var page = new Page { number = 1 } if true { var scratch = new Page { number = 2 } print("in the block") } page = new Page { number = 3 } print("end of main") } .fi .RE .PP .RS 4 .nf in the block free page 2 free page 1 end of main free page 3 .fi .RE .PP \fBdispose\fR runs just before a \fBPage\fR is freed. Page 2 ends with its block, page 1 when \fBpage\fR is written over, and page 3 at the end of \fBmain\fR\&. .SS "Moving a pointer" .RS 4 .nf struct Page : Disposable { int number void dispose(ref this) { print($"free page {this.number}") } } Page* blank(int number) { return new Page { number = number } } void archive(Page* page) { print($"archive page {page.number}") } void main() { var page = blank(7) var moved = page print(moved.number) archive(moved) print("end of main") } .fi .RE .PP .RS 4 .nf 7 archive page 7 free page 7 end of main .fi .RE .PP The page is made once and never copied: \fBblank\fR gives the pointer back, \fBmoved\fR takes it from \fBpage\fR, and \fBarchive\fR takes it from \fBmoved\fR\&. It is freed where \fBarchive\fR ends, since \fBarchive\fR was its last owner. .SS "Borrowing through a pointer" .RS 4 .nf struct Account { string owner long balance } void deposit(ref Account account, long amount) { account.balance += amount } void show(read Account account) { print($"{account.owner}: {account.balance}") } void main() { var account = new Account { owner = "Ada".clone(), balance = 100 } deposit(ref account, 50) show(read account) var balance = ref account.balance balance -= 30 show(read account) } .fi .RE .PP .RS 4 .nf Ada: 150 Ada: 120 .fi .RE .PP \fBdeposit\fR and \fBshow\fR take an \fBAccount\fR, and are given the one behind the pointer. \fBref account.balance\fR borrows a field of it. .SS "A list that points to itself" .RS 4 .nf struct Node { string task Node*? next } void main() { Node*? todo = null todo = new Node { task = "write tests".clone(), next = todo } todo = new Node { task = "fix the bug".clone(), next = todo } todo = new Node { task = "read the report".clone(), next = todo } var current = read todo while current is not null { print(current.task) current = read current.next } } .fi .RE .PP .RS 4 .nf read the report fix the bug write tests .fi .RE .PP Each new node takes the list so far as its \fBnext\fR, so the last one made comes first. \fBcurrent\fR only borrows: it walks the list without moving a node. The whole list is freed when \fBtodo\fR ends. .SS "A tree changed in place" .RS 4 .nf struct Node { int key Node*? left Node*? right } void insert(ref Node*? tree, int key) { if tree is null { tree = new Node { key = key, left = null, right = null } return } if key < tree.key { insert(ref tree.left, key) } else { insert(ref tree.right, key) } } void inOrder(read Node*? tree, ref List out) { if tree is null { return } inOrder(read tree.left, ref out) out.add(tree.key) inOrder(read tree.right, ref out) } void main() { Node*? tree = null var keys = List { 50, 20, 70, 10, 30 } for key in read keys { insert(ref tree, key) } var sorted = List() inOrder(read tree, ref sorted) print(sorted) } .fi .RE .PP .RS 4 .nf [10, 20, 30, 50, 70] .fi .RE .PP \fBinsert\fR borrows the place where a node belongs, \fBref tree.left\fR or \fBref tree.right\fR, and puts a new node there when it is empty. After \fBif tree is null { ... return }\fR, \fBtree\fR is a \fBNode*\fR, so \fBtree.key\fR needs no test. .SH "WHAT THE COMPILER REFUSES" .SS "A type that holds itself" .RS 4 .nf struct Node { int value Node? next } void main() { var last = Node { value = 2, next = null } print(last.value) } .fi .RE .PP .RS 4 .nf error: struct `Node` contains itself, so it has no finite size --> example.sg:3:5 3 | Node? next | ^^^^^ hint: put it behind a pointer: `Node*? next` .fi .RE .SS "A value where a pointer is wanted" .RS 4 .nf struct Node { int value Node*? next } void main() { var last = Node { value = 2, next = null } var first = Node { value = 1, next = last } print(first.value) } .fi .RE .PP .RS 4 .nf error: expected `Node*`, found `Node` --> example.sg:8:42 8 | var first = Node { value = 1, next = last } | ^^^^ .fi .RE .PP \fBlast\fR lives in \fBmain\fR, not on the heap. \fBvar last = new Node { ... }\fR makes it a \fBNode*\fR, which \fBnext\fR takes. .SS "A pointer used after it moved" .RS 4 .nf struct Point { int x int y } void main() { var p = new Point { x = 3, y = 4 } var q = p print(p.x) print(q.y) } .fi .RE .PP .RS 4 .nf error: `p` was moved and can no longer be used --> example.sg:9:11 9 | print(p.x) | ^ it was moved here: --> example.sg:8:13 8 | var q = p | ^ .fi .RE .PP A pointer is not copied, so two variables never own the same value. \fBvar q = read p\fR would only borrow it. .SS "A pointer moved out through a borrow" .RS 4 .nf struct Node { string task Node*? next } void dropFirst(ref Node*? list) { if list is not null { list = list.next } } void main() { Node*? todo = new Node { task = "write tests".clone(), next = null } todo = new Node { task = "fix the bug".clone(), next = todo } dropFirst(ref todo) print(todo is null) } .fi .RE .PP .RS 4 .nf error: a `Node*?` field cannot be moved out through a borrow --> example.sg:8:16 8 | list = list.next | ^^^^^^^^^ hint: take it with `list.next.take()`, which leaves `null` there .fi .RE .PP A borrow must leave the value whole. With \fBlist = list.next.take()\fR the next node moves up, and the first node, written over, is freed. .SH "SEE ALSO" .BR sg-pointer (7), .BR sg-nullable (7), .BR sg-null (7), .BR sg-shared (7), .BR sg-struct (7), .BR sg-enum (7) .PP https://sg\-lang.org/reference/new.html