.\" Made by website/plain/build.mjs from website/words/use.md; change that file. .TH SG-USE 7 2026-11-28 "SG" "SG reference" .SH NAME use \- brings the public names of a module into a file, to be written without the module in front .SH SYNOPSIS .TP \fBuse accounts\fR the public names of the module accounts, the folder accounts/ beside the program .TP \fBuse units.wind\fR a module inside a module, named by its whole path from the root .TP \fBunits.format(x), accounts.Account\fR a full name, which needs no use .TP \fBuse money\fR a package, by the name its line in sg.project gives it .TP \fBstd.Hex\fR a type of the language's own module, by its full name; its types need no use .SH DESCRIPTION \fBuse\fR names a module at the top of a file, one on a line, before everything else. Every \fBpublic\fR name of that module can then be written bare in the file: \fBAccount\fR instead of \fBaccounts.Account\fR\&. It counts for that file only; each file writes the \fBuse\fR lines it needs. The files of one module see each other with no \fBuse\fR\&. .PP A module is a folder, and it is named by its path from the root of the program, also from inside another module: \fBuse units.wind\fR, even in a file of \fBunits/\fR\&. The root is the file given to \fBsg\fR, with the folders beside it as its modules, or every file of the folder given. A module is read only when a file names it, with \fBuse\fR or with a full name, so a folder nobody names is not looked at. .PP A full name, \fBunits.format\fR, works with or without \fBuse\fR\&. When two modules named with \fBuse\fR have the same name, the bare name is an error where it is used, and the full name settles it. A name is looked for in this order: a variable, the file's own module, the names brought in by \fBuse\fR, then the types of the language. So a type added to the library never breaks a program that has a name of its own like it. .PP The library, the module \fBstd\fR, needs no \fBuse\fR: its types are seen from every module, and \fBuse std\fR is an error. A type of your own with the name of one of its types comes first, and the library's is then written with the module in front: \fBstd.Duration\fR, \fBstd.Hex\fR, \fBstd.List\fR, \fBstd.File.readText(path)\fR\&. This holds for every type of the library, \fBList\fR, \fBHashMap\fR, \fBFile\fR, \fBIoError\fR and \fBTcpStream\fR among them, and a function, a const or a static may have such a name too. Kept are only the words of the language, the types of numbers, \fBbool\fR, \fBchar\fR, \fBstring\fR and \fBvoid\fR, and the three types its syntax makes: \fBResult\fR (by \fBtry\fR, \fBOk\fR and \fBError\fR), \fBTask\fR (by \fBasync\fR) and \fBFunc\fR (by a function's name used as a value). \fBStdin\fR, \fBStdout\fR and \fBStderr\fR are kept too: they are not types but the streams every function has. .PP A \fBuse\fR that nothing needs is not an error. Two modules may \fBuse\fR each other. There is no \fBuse ... as\fR: a type gets a second name with \fBalias\fR\&. .PP A package is named after \fBuse\fR by the word its \fBrequire\fR line in \fBsg.project\fR gives it, or by the last part of its address when the line gives none. Its root files are that module and its folders the modules under it (\fBmoney.reader\fR). Inside a package, its own folders are named from its root, and the program's modules are not there for it. .SH EXAMPLES .SS "The library, with no use" .RS 4 .nf void main() { var timeout = Duration.fromMilliseconds(1500) print(timeout) print(Hex.encode("SG".asBytes())) var value = Json.parse("[10, 20, 30]") ?? JsonValue.Null print(value.at(1)?.asLong() ?? 0) } .fi .RE .PP .RS 4 .nf 1.5s 5347 20 .fi .RE .PP \fBDuration\fR, \fBHex\fR and \fBJson\fR are types of the library, seen from every file. .SS "A name of your own, and the library's by its full name" .RS 4 .nf struct Hex : Formattable { int column int row } void main() { var cell = Hex { column = 3, row = 5 } print(cell) print(std.Hex.encode("SG".asBytes())) } .fi .RE .PP .RS 4 .nf Hex { column = 3, row = 5 } 5347 .fi .RE .PP \fBHex\fR is the struct of the program, a cell of a map. The library's \fBHex\fR is still there under its full name. .SS "A File of your own" .RS 4 .nf struct File { string name int lines } void main() { var notes = File { name = "notes.txt", lines = 3 } print(notes.name) var words = std.List() words.add("sg") match std.File.readText(read "/no/such/file.txt") { Ok(text) => print(text) Error(NotFound) => print("not there") Error(other) => print(other) } print(words) } .fi .RE .PP .RS 4 .nf notes.txt not there ["sg"] .fi .RE .PP \fBFile\fR here is the program's struct. The library's file functions are still there as \fBstd.File\fR, and its collections as \fBstd.List\fR, as for any type of the library. .SS "A module beside the program" .RS 4 .nf weather/ main.sg units/ temperature.sg wind/ speed.sg .fi .RE .PP The file weather/units/temperature.sg: .PP .RS 4 .nf public double fahrenheit(double celsius) { return celsius * 9 / 5 + 32 } public string format(double celsius) { return $"{celsius} C".toString() } .fi .RE .PP The file weather/units/wind/speed.sg: .PP .RS 4 .nf public double metersPerSecond(double kmh) { return kmh / 3.6 } public string format(double kmh) { return $"{kmh} km/h".toString() } .fi .RE .PP The file weather/main.sg: .PP .RS 4 .nf use units use units.wind void main() { var celsius = 21.5 var kmh = 36.0 print($"{units.format(celsius)}, {fahrenheit(celsius)} F") print($"{units.wind.format(kmh)}, {metersPerSecond(kmh)} m/s") } .fi .RE .PP .RS 4 .nf $ sg weather 21.5 C, 70.7 F 36 km/h, 10 m/s $ sg build weather -o weather-app && ./weather-app 21.5 C, 70.7 F 36 km/h, 10 m/s .fi .RE .PP \fBunits\fR and \fBunits.wind\fR are two modules: a folder inside a module is a module of its own, and \fBuse units\fR does not bring in \fBunits.wind\fR\&. \fBfahrenheit\fR and \fBmetersPerSecond\fR are written bare. Both modules have a \fBformat\fR, so \fBmain.sg\fR writes it with the module in front. .SS "Two modules that use each other" The file planner/calendar/day.sg: .PP .RS 4 .nf use holidays public struct Day : Copyable { public int month public int date public int weekday public bool isWorkday(read this) { return this.weekday < 6 && !isHoliday(this) } } .fi .RE .PP The file planner/holidays/list.sg: .PP .RS 4 .nf use calendar public bool isHoliday(Day day) { return day.month == 1 && day.date == 1 || day.month == 12 && day.date == 25 } .fi .RE .PP The file planner/main.sg: .PP .RS 4 .nf use calendar void main() { var newYear = Day { month = 1, date = 1, weekday = 4 } var nextDay = newYear with { date = 2, weekday = 5 } print(newYear.isWorkday()) print(nextDay.isWorkday()) } .fi .RE .PP .RS 4 .nf $ sg planner false true .fi .RE .PP \fBcalendar\fR asks \fBholidays\fR, and \fBholidays\fR takes the \fBDay\fR of \fBcalendar\fR\&. Neither has to be split up to break the circle. .SS "A package" The program \fBshop\fR takes the package \fBmoney\fR from the folder beside it while both are being written; a \fBrequire\fR line alone would fetch it from its address. .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 \fBuse money\fR is the name \fBrequire\fR gives. In the package, \fBuse internal\fR names its folder \fBmoney/internal/\fR from the package's root; the program would write \fBmoney.internal\fR, which a folder named \fBinternal\fR keeps for \fBmoney\fR alone. .SH "WHAT THE COMPILER REFUSES" .SS "use below other lines" .RS 4 .nf const int Days = 7 use calendar void main() { print(Days) } .fi .RE .PP .RS 4 .nf error: `use` is written at the top of the file, before everything else --> example.sg:3:1 3 | use calendar | ^^^ .fi .RE .PP Comments may stand above the \fBuse\fR lines; nothing else may. The same \fBuse\fR line twice is refused too: \fB`use calendar` is written twice\fR\&. .SS "A module that is not there" .RS 4 .nf use stock void main() { print("inventory") } .fi .RE .PP .RS 4 .nf error: there is no module named `stock` a module is a folder with source files, next to the files of the program: `stock/` --> example.sg:1:5 1 | use stock | ^^^^^ hint: a package is named on a `require` line of `sg.project`, beside the program, which `sg add
` writes .fi .RE .PP A name \fBuse\fR writes is a folder of the program or a package \fBsg.project\fR names, so the hint says how a package is named. When a module of that name is further down, the hint gives its whole path: \fBuse wind\fR in a file of \fBunits/\fR gets the hint \fB`use units.wind`\fR\&. .SS "use std" .RS 4 .nf use std void main() { print(Duration.fromSeconds(3)) } .fi .RE .PP .RS 4 .nf error: `std` is the language's own module, and its types are seen from every module --> example.sg:1:5 1 | use std | ^^^ hint: take the line away: `Duration`, `Instant` and `DateTime` need no `use` .fi .RE .SS "One name in two modules" With the folder \fBweather\fR above: .PP The file weather/main.sg: .PP .RS 4 .nf use units use units.wind void main() { print(format(21.5)) } .fi .RE .PP .RS 4 .nf $ sg weather error: `format` exists in `units` and `units.wind` --> weather/main.sg:5:11 5 | print(format(21.5)) | ^^^^^^^^^^^^ hint: write the module in front: `units.format` .fi .RE .SH "SEE ALSO" .BR sg-public (7), .BR sg-alias (7), .BR sg (1), .BR sg-build (1), .BR sg-add (1) .PP https://sg\-lang.org/reference/use.html