.\" Made by website/plain/build.mjs from website/words/public.md; change that file. .TH SG-PUBLIC 7 2026-11-28 "SG" "SG reference" .SH NAME public \- shows a name to other modules; without it, a name stays inside its own module .SH SYNOPSIS .TP \fBpublic struct Name, public enum Name\fR a type other modules can name; the variants of an enum come with it .TP \fBpublic T name(...) { }\fR a function other modules can call .TP \fBpublic T field\fR a field other modules can read and write .TP \fBpublic readonly T field\fR a field other modules can only read .TP \fBpublic T m(read this), public Name create(...)\fR a method, or a constructor, other modules can call .TP \fBpublic const T Name = value, public static T Name = value\fR a const, a static .TP \fBpublic alias Name = T\fR a second name of a type .TP \fBextern { public int f(...) }\fR a function of C that other modules can call .TP \fBno public\fR the name is seen by the files of its own module only .SH DESCRIPTION A module is a folder: every \fB\&.sg\fR file in it. A name written without \fBpublic\fR is seen by every file of its own module and by no other. With \fBpublic\fR it is seen by the modules that bring it in with \fBuse\fR, or write it in full (\fBaccounts.Account\fR). The rule is the same for types, functions, fields, methods, consts, statics and aliases, and there are only these two levels. The variants of an enum are seen wherever the enum is. .PP A type and its fields are shown one by one. A \fBpublic struct\fR can keep some fields to itself: outside the module such a field cannot be read or written, and the struct cannot be built field by field, since the outside could not give that field a value. It is built by a \fBpublic\fR function, usually \fBcreate\fR\&. This is how a module keeps a rule true: only its own code can change the field. A \fBpublic\fR field can be written from outside; \fBpublic readonly\fR lets the outside read it and nothing more. .PP A \fBpublic\fR name cannot show a type that is not public: a public function that takes or gives a private struct, or a public field of a private type, is an error that points at both. .PP There is no \fBprivate\fR: a name without \fBpublic\fR already stays inside its module, and the word is refused with that hint. A field without \fBpublic\fR is seen by every file of its folder. To keep it from those files too, put the type in a folder of its own, a module of its own under the first (\fBaccounts/ledger/\fR): the files beside it then see only what it makes \fBpublic\fR, as any other module does. The folder is the one line that hides a name, so a reader finds what can touch a field by looking at one folder. .PP The root of a program, the file given to \fBsg\fR or the files of the folder given, has no module name, so no module can \fBuse\fR it. \fBpublic\fR is allowed there and changes nothing: a program of one file does not need it. In a package the \fBpublic\fR names of its modules are what a program sees, and a module in a folder named \fBinternal\fR is seen only from inside the folder that holds it. The library is a module too, \fBstd\fR, and a program sees only its \fBpublic\fR names. \fBpublic\fR costs nothing when the program runs. .SH EXAMPLES .SS "A module and what it shows" .RS 4 .nf bank/ main.sg accounts/ account.sg fees.sg .fi .RE .PP The file bank/accounts/account.sg: .PP .RS 4 .nf public struct Account { public readonly string owner public string? note long cents public Account create(string owner) { return Account { owner = owner, note = null, cents = 0 } } public void deposit(ref this, long cents) { this.cents += cents - fee(cents) } public long balance(read this) { return this.cents } } .fi .RE .PP The file bank/accounts/fees.sg: .PP .RS 4 .nf long fee(long cents) { if cents < 10000 { return 25 } return 0 } .fi .RE .PP The file bank/main.sg: .PP .RS 4 .nf use accounts void main() { var account = Account("Ada") account.deposit(5000) account.deposit(20000) account.note = "rent" print($"{account.owner}: {account.balance()} cents") print(account.note ?? "") } .fi .RE .PP .RS 4 .nf $ cd bank $ sg main.sg Ada: 24975 cents rent .fi .RE .PP \fBmain.sg\fR reads \fBowner\fR and writes \fBnote\fR, both public. \fBcents\fR has no \fBpublic\fR, so only the \fBaccounts\fR module changes it, through \fBdeposit\fR\&. \fBfee\fR is in another file of the same folder, so \fBdeposit\fR calls it with nothing written; \fBmain.sg\fR cannot call it. .SS "A field kept from the files beside it" .RS 4 .nf bank/ main.sg accounts/ transfer.sg ledger/ ledger.sg .fi .RE .PP The file bank/accounts/ledger/ledger.sg: .PP .RS 4 .nf public struct Ledger { long cents public Ledger create() { return Ledger { cents = 0 } } public void add(ref this, long cents) { if cents > 0 { this.cents += cents } } public long total(read this) { return this.cents } } .fi .RE .PP The file bank/accounts/transfer.sg: .PP .RS 4 .nf use accounts.ledger public long moved(long a, long b) { var book = Ledger() book.add(a) book.add(b) book.add(-500) return book.total() } .fi .RE .PP The file bank/main.sg: .PP .RS 4 .nf use accounts void main() { print(moved(1200, 300)) } .fi .RE .PP .RS 4 .nf $ cd bank $ sg main.sg 1500 .fi .RE .PP \fBcents\fR has no \fBpublic\fR, and \fBLedger\fR is alone in \fBaccounts/ledger/\fR, so \fBtransfer.sg\fR, a file of \fBaccounts\fR, changes it only through \fBadd\fR, which passes over an amount below zero. Had \fBLedger\fR been a file of \fBaccounts\fR, \fBtransfer.sg\fR could write \fBbook.cents = \-500\fR\&. .SS "The library shows its public names" .RS 4 .nf void main() { var options = JsonOptions { indent = 2 } options.maxDepth = 16 print($"{options.maxDepth} {options.indent ?? 0}") var timeout = Duration.fromSeconds(90) print(timeout.milliseconds()) } .fi .RE .PP .RS 4 .nf 16 2 90000 .fi .RE .PP The fields of \fBJsonOptions\fR are public, so a program builds it field by field and changes it. A \fBDuration\fR keeps its count of nanoseconds to itself: it is made by its functions and read through its methods. .SS "An enum, a const and an alias" The file savings/rates/plan.sg: .PP .RS 4 .nf public const int MaxPercent = 5 public enum Plan : Formattable { Easy Fixed(int years) public int percent(read this) { return match this { Easy => 1 Fixed(years) => capped(years + 1) } } } int capped(int percent) { return percent.min(MaxPercent) } public alias Plans = List .fi .RE .PP The file savings/main.sg: .PP .RS 4 .nf use rates void main() { Plans plans = List { Plan.Easy, Plan.Fixed(2), Plan.Fixed(9) } for plan in read plans { print($"{plan}: {plan.percent()}%") } print($"at most {MaxPercent}%") } .fi .RE .PP .RS 4 .nf $ sg savings Easy: 1% Fixed(2): 3% Fixed(9): 5% at most 5% .fi .RE .PP \fBPlan.Easy\fR and \fBPlan.Fixed\fR are seen because \fBPlan\fR is. A method of an enum is public the same way as one of a struct. \fBcapped\fR stays inside \fBrates\fR, though the public \fBpercent\fR calls it. .SS "public in a package" A package is a library with an \fBsg.project\fR of its own. Here the program \fBshop\fR takes the package \fBmoney\fR from the folder beside it: .PP .RS 4 .nf shop/ sg.project main.sg money/ sg.project format.sg internal/ group.sg .fi .RE .PP The file shop/sg.project: .PP .RS 4 .nf require money example.com/ada/money 1.0.0 local money ../money .fi .RE .PP The file money/sg.project: .PP .RS 4 .nf package example.com/ada/money .fi .RE .PP The file money/format.sg: .PP .RS 4 .nf use internal public string euros(long cents) { return $"{grouped(cents / 100)}.{cents % 100:d2} EUR".toString() } .fi .RE .PP The file money/internal/group.sg: .PP .RS 4 .nf public string grouped(long whole) { if whole < 1000 { return $"{whole}".toString() } return $"{grouped(whole / 1000)},{whole % 1000:d3}".toString() } .fi .RE .PP The file shop/main.sg: .PP .RS 4 .nf use money void main() { print(euros(123456789)) print(money.euros(5)) } .fi .RE .PP .RS 4 .nf $ cd shop $ sg main.sg 1,234,567.89 EUR 0.05 EUR .fi .RE .PP The program sees the \fBpublic\fR names of the package's modules, here \fBeuros\fR\&. \fBgrouped\fR is public too, but its module is in a folder named \fBinternal\fR, so only \fBmoney\fR and its folders name it: \fBuse money.internal\fR in \fBshop/main.sg\fR is refused with \fB`money.internal` is in a folder `internal`: only the modules inside `money` name it\fR\&. .SH "WHAT THE COMPILER REFUSES" .SS "A field that is not public" With the \fBaccounts\fR module of the first example: .PP The file bank/main.sg: .PP .RS 4 .nf use accounts void main() { var account = Account("Ada") account.cents = 1000000 print(account.balance()) } .fi .RE .PP .RS 4 .nf $ cd bank $ sg main.sg error: `cents` is not public: it stays inside the `accounts` module --> main.sg:5:13 5 | account.cents = 1000000 | ^^^^^ .fi .RE .PP A function gets its own message, which shows where it is: \fBprint(fee(5000))\fR gives \fB`fee` exists in the `accounts` module but is not public\fR, with the line of \fBfee\fR in \fBaccounts/fees.sg\fR\&. Building the struct field by field, \fBAccount { owner = "Ada", note = null, cents = 1000000 }\fR, gives \fB`cents` is not public, so an `Account` cannot be built field by field outside the `accounts` module\fR, with the hint \fB`Account(owner)`\fR\&. .SS "private" .RS 4 .nf struct Point { private int x } void main() { } .fi .RE .PP .RS 4 .nf error: there is no `private`: a name without `public` stays inside its module --> example.sg:2:5 2 | private int x | ^^^^^^^ hint: leave the word out; to hide it from the files beside it too, put the type in a folder of its own .fi .RE .SS "A public function that shows a private type" A file added to \fBbank/accounts/\fR: .PP The file bank/accounts/statement.sg: .PP .RS 4 .nf struct Line { string text long cents } public Line opening(read Account account) { return Line { text = "opening balance", cents = account.cents } } .fi .RE .PP .RS 4 .nf $ cd bank $ sg main.sg error: `opening` is public, but it shows `Line`, which is not --> accounts/statement.sg:6:13 6 | public Line opening(read Account account) { | ^^^^^^^ `Line` is defined here, without `public`: --> accounts/statement.sg:1:8 1 | struct Line { | ^^^^ hint: write `public` in front of `Line`, or take it off `opening` .fi .RE .PP Other modules could call \fBopening\fR but could not name what it gives. The check is made in modules only, since nothing can \fBuse\fR the root. .SS "A type of the library built field by field" .RS 4 .nf void main() { var timeout = Duration { ns = 90000000000 } print(timeout) } .fi .RE .PP .RS 4 .nf error: a `Duration` is made by its functions, not field by field --> example.sg:2:19 2 | var timeout = Duration { ns = 90000000000 } | ^^^^^^^^ hint: `Duration.fromMilliseconds(n)`, `Duration.fromSeconds(n)` .fi .RE .SS "public on a test" .RS 4 .nf public test "a deposit is counted" { assert(25000 - 25 == 24975) } void main() { print("bank") } .fi .RE .PP .RS 4 .nf error: a test is not `public`: only the test command runs it --> example.sg:1:1 1 | public test "a deposit is counted" { | ^^^^^^ .fi .RE .SH "SEE ALSO" .BR sg-use (7), .BR sg-readonly (7), .BR sg-struct (7), .BR sg-enum (7), .BR sg-alias (7), .BR sg-const (7) .PP https://sg\-lang.org/reference/public.html