When:
a convenient syntax is provided for some common types. These are especially useful when writing C bindings, but can be used in any of the above locations.
Regular types and generics can be used:
Int32
My::Nested::Type
Array(String)
alias Int32OrString = Int32 | String
The pipe (|
) in types creates a union type. Int32 | String
is read "Int32 or String". In regular code, Int32 | String
means invoking the method |
on Int32
with String
as an argument.
alias Int32OrNil = Int32?
is the same as:
alias Int32OrNil = Int32 | ::Nil
In regular code, Int32?
is a syntax error.
alias Int32Ptr = Int32*
is the same as:
alias Int32Ptr = Pointer(Int32)
In regular code, Int32*
means invoking the *
method on Int32
.
alias Int32_8 = Int32[8]
is the same as:
alias Int32_8 = StaticArray(Int32, 8)
In regular code, Int32[8]
means invoking the []
method on Int32
with 8
as an argument.
alias Int32StringTuple = {Int32, String}
is the same as:
alias Int32StringTuple = Tuple(Int32, String)
In regular code, {Int32, String}
is a tuple instance containing Int32
and String
as its elements. This is different than the above tuple type.
alias Int32ToString = Int32 -> String
is the same as:
alias Int32ToString = Proc(Int32, String)
To specify a Proc without arguments:
alias ProcThatReturnsInt32 = -> Int32
To specify multiple arguments:
alias Int32AndCharToString = Int32, Char -> String
For nested procs (and any type, in general), you can use parentheses:
alias ComplexProc = (Int32 -> Int32) -> String
In regular code Int32 -> String
is a syntax error.
self
can be used in the type grammar to denote a self
type. Refer to the type restrictions section.
class
is used to refer to a class type, instead of an instance type.
For example:
def foo(x : Int32)
"instance"
end
def foo(x : Int32.class)
"class"
end
foo 1 # "instance"
foo Int32 # "class"
class
is also useful for creating arrays and collections of class type:
class Parent
end
class Child1 < Parent
end
class Child2 < Parent
end
ary = [] of Parent.class
ary << Child1
ary << Child2
An underscore is allowed in type restrictions. It matches anything:
# Same as not specifying a restriction, not very useful
def foo(x : _)
end
# A bit more useful: any two arguments Proc that returns an Int32:
def foo(x : _, _ -> Int32)
end
typeof
is allowed in the type grammar. It returns a union type of the type of the passed expressions:
alias SameAsInt32 = typeof(1 + 2)
alias Int32OrString = typeof(1, "a")