Skip to content

Instantly share code, notes, and snippets.

Created March 18, 2013 16:33
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/5188571 to your computer and use it in GitHub Desktop.
Save anonymous/5188571 to your computer and use it in GitHub Desktop.
Proposal for new syntax for type definition. Motivation: Rust tries to mix concepts from Functional languages with syntax from C/C++. structs (records) are equivalent to the C/C++ version. However enums in rust do not behave like enums from c: Enums in rust are used to define Algebraic Data Types (sum types) that act similar to 'tagged unions' i…
type Name = {
first: ~str,
last: ~str
}
type Shape = Circle(int) | Rect(int, int) // Variants with constructors
type Direction = North | South | East | West // without constructors
enum Flags = {
Optimise = 1,
Debug = 2,
Assemble = 4,
Default = Optimise | Debug
}
type Point = (int, int) // Tuples with name can replace tuple-structs
fn stream<T> () -> (Port<T>, Chan<T>); // tuples with no name can be used for
// returning multiple values
type Coordinate = Point // alias or typedef
type Celcius = (float) //tuple with one element can be used as haskell newtype
type Kelvin = (float)
fn celcius_to_kelvin(cel: Celcius) -> Kelvin {
return *cel + 273.0f;
}
fn main() {
let name = Name{first: ~"John", last: ~"Smith"};
let shape = @Circle(20);
let point = Point(10,20);
let flags = Flags.Optimise // this is why we have seperate enum keyword.
let temp = Celcius(0); // looks like constructor and matches the type definition
let temp = celcius_to_kelvin(temp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment