Skip to content

Instantly share code, notes, and snippets.

@cqcallaw
Last active April 14, 2018 17:17
Show Gist options
  • Save cqcallaw/6de8bbb25599a1a71d34c7638ef40fe7 to your computer and use it in GitHub Desktop.
Save cqcallaw/6de8bbb25599a1a71d34c7638ef40fe7 to your computer and use it in GitHub Desktop.
type parameter noodling
# in general, type and function definitions have type parameters. Type specifiers (e.g. variable types or formal parmaeters) have type arguments
result of <T> {
data:T,
errors:error_list
}
result2 of <T> {
data:T
| errors:error_list
}
# interior type with parameterization
t of <L> {
g {
h:L
}
}
# interior type with multiple parameters
t of <H, K> {
g {
a:H,
b:K
}
}
# interior type should be a semantic error
t2 of <H> {
g of <G> { # semantic error
a:H
}
}
# test unspecified internal type parameter
t of <H> {
a:H
}
g of <K> {
b:t # semantic error: type parameter required
}
r := @g of <int>
# when aliasing, the type specifier specifies a type _argument_, not a type _parameter_
wrapper of <T> {
a:result of <T>
}
# when aliasing, it is illegal to referenced un-defined type arguments
wrapper2 {
a:result of <T> # semantic error
}
wrapper3 of <T> {
a:result of <K> # semantic error
}
map of <T, K> {
key:T,
value:K,
next:map of <T, K>?
}
f := (i:T, j:K) of <T, K> -> map of <T, K> {
return @map of <T, K>
}
# record type with parameterized function member
t of <T> {
f := (a:T, b:K) of <K> {
print(a)
print(b)
}
}
g of <int> { # semantic error: type parameters may not hide types that already exist
b:int
}
# semantic error to specify too many type alias
rs1: = @result # no type parameters
rs1: = @result of <> # empty type parameter list
rs1: = @result of <int, string> # too many type parameters
r1 := @result of <int>
r2 := @result2 of <int>
r3 := @result of <result of <int>>
r4 := f(3, 5.0) of <int, double>
r5 := @result of <result of <int>>.data
r6 := @result of <int>?
r7 := @t of <bool>
r8 := @result of <int> with { data = 3 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment