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 19:36, 15 November 2021 (Improve license text, link, and citation). 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
First appearedJuly 7, 2016; 8 years ago (2016-07-07)
Typing disciplineStatic, strong, inferred, structural, generic
Platformx86-64, ARM, WebAssembly
OSCross-platform
License3-clause BSD[1]
Filename extensions.odin
Websiteodin-lang.org
Influenced by
Pascal, C, Go, Oberon-2, Newsqueak, Jai, GLSL[2]

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

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 SOA data types.[4][5]

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

Odin is designed as being an alternative for the C programming language on "high performance, modern systems"[6], with features like compile-time parametric polymorphism, array programming, and runtime reflection.

Syntax

Odin's declaration syntax is inspired by Newsqueak and Jai.

// Variable declarations
x : int = 123
x := 123 // Type inference

// Constant value declarations
X :: 123
Y : int : 123

// Function declaration
Z :: proc() {}

Explicit procedure overloading

Odin has procedure overloading, but unlike C++ and Go the overloads have to be specified explicitly.

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}

Array programming

Odin provides array programming, enabling arithmetics on array elements:

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]

The language also features "swizzling" of arrays, similar to the operation in shader languages like GLSL.

// 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
}

Comparisons with other languages

The syntax of Odin resembles Go's syntax with many 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[7]

Compared to Go, Odin:

Notable software built with Odin

See also

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