'''Odin''' is an [[imperative programming|imperative]], [[General-purpose programming language|general-purpose]], [[statically typed]], [[distinctly typed]], [[compiled programming language|compiled]] [[system programming language]] designed by [[Ginger Bill]]<ref>https://www.youtube.com/watch?v=2YLA4ajby00</ref>.
The language is designed for "high performance, modern systems, and built-in [[Data-oriented_design|data-oriented]] data types", supports [[compile-time]] [[parametric polymorphism]], [[Type introspection|runtime reflection]], [[cross-compilation]], [[manual memory management]], [[array programming]], and [[AoS_and_SoA|Structure of Arrays (SOA)]] data types.<ref>https://www.youtube.com/watch?v=iCqW_RepcW0</ref><ref>https://odin-lang.org/docs/overview/</ref>
== Example ==
=== Syntax ===
'''Odin expects a [[UTF-8]] encoded text file as its input. The file is required to have 's'.odin'' extension. The declaration syntax is inspired by [[Newsqueak]] and Jai. Here's an example of some declarations:
<syntaxhighlight lang="go">
// Variable declarations
x : int = 123
x := 123 // Type = 123inference
x := 123
x := int(123)
// Constant value declarations
X :: 123
X : : 123
Y : int : 123
Y :: int(123)
// Function declaration
Z :: proc() {}
Z : proc() : proc() {} // Redundant type declaration
</syntaxhighlight>The semicolons are statement terminators are optional<ref>https://odin-lang.org/news/optional-semicolons/</ref>. Unlike [[Go (programming language)|Go]], the Odin permits a newline in certain places where semicolon could otherwise be inserted, allowing programmer to have a higher selection of [[Indentation style|indentation styles]].
=== Explicit procedure overloading ===
Odin has explicit [[Function_overloading|procedure overloading]], but unlike many language which have implicit [[Function_overloading|procedure overloading]] (e.g. [[C++]]), meaningand eachGo procedurethe needsoverloads have to be specified explicitly within a procedure group.
<syntaxhighlight lang="go">
bool_to_string :: proc(b: bool) -> string {...}
int_to_string :: proc(i: int) -> string {...}
// "to_string" is will call either "bool_to_string" or "int_to_string" depending on type.
to_string :: proc{bool_to_string, int_to_string}
x := to_string(true)
y := to_string(123)
</syntaxhighlight>
This has parallels with [[C11_%28C_standard_revision%29|C11]]'s <code>_Generic</code> feature, but applies specifically to procedure values.
===Array programming===
Odin provides [[array programming]], enabling arithmetics on array elements:
Odin provides [[array programming]] feature. The arrays of numeric types can be added, subtracted, multiplied together, while the compiler will implicitly do the same operation value-by-value.<syntaxhighlight lang="go">
<syntaxhighlight lang="go"> ▼
c := a * b
d := a + b
</syntaxhighlight>
The language also features "swizzling" of arrays, similar to the operation in shader languages like GLSL.
The built-in procedure <code>swizzle</code> allows the elements of the array can be reordered in an arbitrary way. The indices specify which element of the original array is to be placed in a new array. <code>swizzle(a, 2, 1, 0)</code> will reorder elements of an array with length 3 to be in backwards order. For arrays up to dimension 4, there is a shorter notation for swizzling, using combination of letter ''x'', ''y'', ''z'', ''w'' in any order, akin to GLSL swizzling (e.g. <code>a.zyx</code>)<ref>https://www.khronos.org/opengl/wiki/Data_Type_(GLSL)#Swizzling</ref>.
<syntaxhighlight lang="go">
}
</syntaxhighlight>
====Matrix support====
A <code>matrix</code> is a [[Matrix_(mathematics)|mathematical type]] built into Odin. It is a regular array of numbers, arranged in rows and columns. Odin's matrix support allows for ''matrix-array and matrix-matrix operations'' making it a [[Basic_Linear_Algebra_Subprograms#Level_3|Level 3 Basic Linear Algebra Subprogram]]ing language.
a: matrix[2, 3]f32 // matrix that has 2 rows and 3 columns with an element type of f32
b: matrix[3, 2]f32 // matrix that has 3 rows and 2 columns with an element type of f32
v: [2]f32 // array that has 2 elements with an element type of f32
a = matrix[2, 3]f32{
1, 9, -13,
20, 5, -6,
}
b = matrix[3, 2]f32{
3, 5,
7, 9,
}
v = [2]f32{2, -4}
m := a * b // matrix-matrix multiplication
vp := m * v // matrix-array multiplication
The internal representation of a <code>matrix</code> in Odin is stored in [[Row-_and_column-major_order|column-major]] format<ref>https://odin-lang.org/docs/overview/#matrix-type</ref> while the matrix literals are written in standard (row-major like) order (e.g. <code>matrix[2, 3]f32</code> is internally <code>[3][2]f32</code> (with different a [[Data_structure_alignment|alignment]] requirement)). Column-major is used in order to utilize ([[SIMD]]) vector instructions effectively on modern hardware, if possible.
==Comparisons with other languages==
The syntax of Odin resembles [[Go (programming language)#Syntax|Go]]'s syntax with many adjustments.
Compared to C++, Odin:
* Removes UB from the language
|