

If no prefix is present, the import will look relative to the current file. The core: prefix is used to state where the import is meant to look this is called a library collection. The following program imports the the fmt and os packages from the core library collection. Programs begin running in the package main.

Packages #Įvery Odin program is made up of packages. Z :: y + 7 // constant computations are possibleįor more information regarding value declarations in general, please see the Odin FAQ and Ginger Bill’s article On the Aesthetics of the Syntax of Declarations. The constant’s value must be able to be evaluated at compile time: x :: "what" // constant `x` has the untyped string value "what"Ĭonstants can be explicitly typed like a variable declaration: y : int : 123 X: = 123 // default type for an integer literal is `int`Ĭonstants are entities (symbols) which have an assigned value. The following are all equivalent: x: int = 123 You can assign multiple variables with it: x, y := 1, "hello" // declares `x` and `y` and infers the types from the assignments The assignment statement assigns a new value to a variable/location: x: int = 123 // declares a new variable `x` with type `int` and assigns a value to it Variables are initialized to zero by default unless specified otherwise. Y, z: int // declares y and z to have type `int` X = 1 // `1` is an untyped integer literal which can implicitly convert to `int`Ī variable declaration declares a new variable for the current scope. x: int = 1.0 // A float literal but it can be represented by an integer without precision lossĬonstant literals are “untyped” which means that they can implicitly convert to a type. In Odin, if a number constant can be represented by a type without precision loss, it will automatically convert to that type. A leading zero does not produce an octal constant (unlike C). If a number literal is suffixed with i, it is an imaginary number literal: 2i (2 multiply the square root of -1).īinary literals are prefixed with 0b, octal literals with 0o, and hexadecimal literals with 0x. A number that contains a dot is a floating point literal: 1.0e9 (one billion). A useful feature in Odin is that underscores are allowed for better readability: 1_000_000_000 (one billion). Numerical literals are written similar to most other programming languages. \UNNNNNNNN - hexadecimal 32-bit Unicode character UTF-8 encoded (8 digits).\uNNNN - hexadecimal 16-bit Unicode character UTF-8 encoded (4 digits).\xNN - hexadecimal 8 bit character (2 digits).If the string passed to len is a compile-time constant, the value from len will be a compile-time constant. The length of a string can be found using the built-in len proc: len("Foo")

Raw string literals are enclosed in single back ticks. Special characters are escaped with a backslash \. String literals are enclosed in double quotes and character literals in single quotes. This is to allow for future work on automatic documentation tools. Multi-line comments can be also be nested (unlike in C): /*Ĭomments are parsed as tokens within the compiler. Multi-line comments begin with /* and end with */. My_integer_variable: int // A comment for documentation Single line comments begin with //: // A comment Lexical elements and literals # Comments #Ĭomments can be anywhere outside of a string or character literal. To tell it to treat a single file as a standalone package, add -file, like so: odin run hellope.odin -file Odin thinks in terms of directory-based packages. If you do not wish to run the executable after compilation, the build command can be used. odin file to an executable and then runs that executable after compilation. odin file, then compile and run it using odin run. To begin this tour, let us start with a modified version of the famous “hello world” program: package main It is recommended to read the Getting started with Odin guide. This tutorial assumes a basic knowledge of programming concepts such as variables, statements, and types. This article is a basic tutorial for the programming language Odin.
