Jump to content

Draft:Odin (programming language)

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Xplane80 (talk | contribs) at 18:16, 15 November 2021 (Remove untyped literals information which is wrong; indentation style example). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Odin
Paradigmsimperative, procedural
Designed byGinger Bill
Typing disciplineStatic, strong, inferred, structural, generic
Platformx86-64, ARM, WebAssembly
OSCross-platform
LicenseBSD-3 License
Filename extensions.odin
Websiteodin-lang.org
Influenced by
C, Pascal, Go, Oberon-2, Newsqueak, Jai[1]

Odin is an imperative, general-purpose, statically typed, distinctly typed, compiled system programming language designed by Ginger Bill[2].

The language is designed for "high performance, modern systems, and built-in data-oriented data types", supports compile-time parametric polymorphism, runtime reflection, cross-compilation, manual memory management, array programming, and Structure of Arrays (SOA) data types.[3][4]

Example

package main

import "core:fmt"

main :: proc() {
	program := "+ + * 😃 - /"
	accumulator := 0

	for token in program {
		switch token {
		case '+': accumulator += 1
		case '-': accumulator -= 1
		case '*': accumulator *= 2
		case '/': accumulator /= 2
		case '😃': accumulator *= accumulator
		case: // Ignore everything else
		}
	}

	fmt.printf("The program \"%s\" calculates the value %d\n",
	           program, accumulator)
}

Design

Initially Odin was being designed as a replacement for the C programming language, however with features like compile-time parametric polymorphism, array programming and runtime reflection, it started to be designed more as a C++ replacement. The syntax of Odin resembles Go's syntax with some adjustments.

Compared to C++, Odin:

  • Removes UB from the language
  • Removes text-based preprocessing stage
  • Introduces strong typing
  • Adds array programming and improves runtime reflection
  • Has explicit function overloading[5]

Compared to Go, Odin:

Syntax

Odin expects a UTF-8 encoded text file as its input. The file is required to have .odin extension. The declaration syntax is inspired by Newsqueak and Jai. Here's an example of some declarations:

a : int = 3  // Declares variable a of type int
a : = 3      // Declares variable a. Type is inferred from RHS expression
a := 3       // : and = are usually written together

a : int : 3  // Declares constant a of type int
a : : 3      // Declares constant a. Type is inferred from RHS expression
a :: 3       // : and : are usually written together

The semicolons are statement terminators and optional. Unlike Go, the Odin permits a newline in certain places where semicolon could otherwise be inserted, allowing programmer to have a higher selection of indentation styles, for example chosing to use Allman variation of K&R indentation style:

if cond {
} else {
}

if cond 
{
}
else // Odin allows else to be on the next line
{      
}

Explicit procedure overloading

Odin has procedure overloading, but unlike C++ and Go the overloads have to be specified explicitly. Each overloaded is specified as a procedure with a distinct name. The overload is then specified explicitly by listing all the names of procedures that are being overloaded and giving the overload a new name.

bool_to_string :: proc(b: bool) -> string {...}
int_to_string  :: proc(i: int)  -> string {...}

to_string :: proc{bool_to_string, int_to_string}

In the example above the procedure to_string can is an explicit overload of bool_to_string and int_to_string. If to_string is called with bool parameter, bool_to_string will be called.

Array programming

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.

a := [3]f32{1, 2, 3}
b := [3]f32{5, 6, 7}
c := a * b
d := a + b
e := 1 + (c - d) / 2
fmt.printf("%.1f\n", e) // [0.5, 3.0, 6.5]

Using swizzle(array, indices..) function, the elements of the array can be reordered in an arbitrary way. The indices specify the which element of the original array that is placed at a given spot in the new array. swizzle(a, 3,2,1) will reorder elements of 3 dimensional array 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.

// Declaring type Vector to be the same as array of 3 f32's.
Vector3 :: [3]f32

// Cross product using swizzle function
cross :: proc(a, b: Vector3) -> Vector3 {
	i := swizzle(a, 1, 2, 0) * swizzle(b, 2, 0, 1)
	j := swizzle(a, 2, 0, 1) * swizzle(b, 1, 2, 0)
	return i - j
}

// Cross product using shorter swizzle notation
cross_shorter :: proc(a, b: Vector3) -> Vector3 {
	i := a.yzx * b.zxy
	j := a.zxy * b.yzx
	return i - j
}

Notable software built with Odin

See also

  1. ^ https://odin-lang.org/docs/faq/
  2. ^ https://www.youtube.com/watch?v=2YLA4ajby00
  3. ^ https://www.youtube.com/watch?v=iCqW_RepcW0
  4. ^ https://odin-lang.org/docs/overview/
  5. ^ "Overview". odin-lang.org. Retrieved 2021-11-15.