Variables and Types
Nubo provides a clean and expressive type system with type inference, explicit annotations, structs, interfaces, and union types.
Declaring Variables
Section titled “Declaring Variables”Use the let keyword for mutable values:
let name = "Martin"let age = 19let isAdmin = trueVariables declared with let can be reassigned:
let sum = 0sum = sum + 10Constants
Section titled “Constants”Use const for immutable values:
const VERSION = "1.0.0"Constants are useful for values that should not change after initialization.
Built-in Types
Section titled “Built-in Types”Nubo includes common built-in types:
byte— single byte (bytescan be used to convert values to a[]byte)int— whole numbersfloat— decimal numbersbool—trueorfalsestring— text valueschar— a single character[]T— a list of values of typeTdict[K, V]— key-value storagefn(...) -> ...— function types (->only required for the type declaration of the function, not the actual use)struct— custom data typesiface— structural interfacesnil— an empty or missing valueany— any type
Type Inference
Section titled “Type Inference”Types are inferred when not specified:
let title = "Nubo" // stringlet count = 5 // intlet enabled = true // boolType Annotations
Section titled “Type Annotations”Types can be explicitly defined using ::
let x: int = 5const name: string = "John"let names: []string = ["Ana", "Bob"]Union Types
Section titled “Union Types”A value can accept more than one type using |:
let s: string|nil = "Hello"Union types are useful when a value may be present or missing.
if s == nil { println("No value")}You can also create named union types:
type NumberOrText: int|stringType Queries
Section titled “Type Queries”Use type() to inspect the runtime type of a value:
let s: string|nil = "Hello"println(s, type(s))Structs
Section titled “Structs”Create custom data types with struct:
struct User { name: string roles: []string data: dict[string, int]}Structs can be constructed by calling the struct name:
const user = User("John")Methods with impl
Section titled “Methods with impl”Use impl to define methods for a struct:
struct User { name: string}
impl User { fn init(self: User, name: string) void { self.name = name }
fn sayHello(self: User) void { println(`Hi! I am ${self.name}!`) }}
const john = User("John")john.sayHello()Function Types
Section titled “Function Types”Functions are first-class values:
let add = fn(a: int, b: int) int { return a + b}Functions can also be declared by name:
fn add(a: int, b: int) int { return a + b}Use void when a function does not return a value:
fn log(message: string) void { println(message)}