Declaring a new type in Ante is accomplished with the
When a new type is declared it is considered to be different than the type(s)
it contains. Resultingly, if a function requires a Str but a Name is
given an error will be issued even if Name contains only a single Str.
This feature provides additional type safety by protecting against the use
of a value on a function that was defined with a different type in mind that
may have different semantics than the newly-defined type. In situations
where this is not desirable, type aliases can be used.
type Name = Str print s:Str = ... n = Name "John" print n //=> error, print:Name not found
An alias for a type can be used in any instance
the original type is used, it is exactly equal to the
original type. The syntax for an alias declaration is
the same as the syntax for a type declaration but with
= replaced with
type AnotherNameForStr is Str type Coord is i32,i32 s = AnotherNameForStr "Greetings!" c = Coord(2, 3) print s:Str = ... print (c: i32,i32) = ... //ok! print s print c
Types declared with
= are considered separate types, but
types declared with
is are always exactly equal to the types
on the right hand side of the declaration. In this way, the
distinction between type declaration with
similar to the distinction between
is when comparing
Structs and Records
If desired a types contained types can each be named and later accessed by this name. These contained types would then be called the fields and the overall type would be called either a struct or a record type.
type Person = name:Str age:u8 j = Person("John", 24) print j.name
Note that the rhs of the type declaration is the same syntax as a function’s parameters. Unlike parameters, a type declaration’s rhs can in an indented block as well.
type MyStr = s:Str str = MyStr "How are you doing today?" print str.s
Tagged union types are types that can be one of several possible cases. When declaring a tagged union each type must be separated with a ‘|’. Additionally, each case must be given a name to distinguish it from the other cases.
type Day = Mon | Tue | Wed | Thurs | Fri | Sat | Sun
These types can also have associated values:
//Both Absolute and Relative Paths have an associated Dir type Path = | Absolute Dir | Relative Dir //If made with the S constructor, this type has //a contained Str. Otherwise, it has an Int type StrOrInt = | S Str | I Int
The only way to extract values from a tagged union type is to use pattern matching across each possible case.
//The Maybe type represents having either a value or the //absence of a value and is often used to represent failure. type Maybe 't = | Some 't | None val = Some 3 //to print the underlying value if it exists: match val with | Some int -> print int | None -> print "No integer found!"