Generics
Sprig's generics are deliberately small. You declare type parameters in a generic block, a call works out its type arguments from the arguments you pass, generics have no variance, and one generic type never converts into another behind your back.
Here's a complete example first:
# Generics: one or more parameters per block. A call or constructor takes its
# type arguments from its arguments; Box[Int](value=41) would mean the same.
generic T:
class Box:
let value: T
generic T:
func identity(value: T) -> T:
return value
let box = Box(value=41)
print(identity(box.value) + 1)42Declaring a generic
Put a class, variant or function inside a generic block:
generic T:
class Box:
let value: T- A block can declare several type parameters, as in
generic K, V:. The names have to be distinct. - Type parameters only exist inside the block; using
Toutside it is rejected withSPR-NAME-UNRESOLVED.
Type arguments
Most of the time you don't write them. A call takes its type arguments from the arguments you pass. This is newer than v0.7.1-beta.1; with that release, you still write them on every call:
let box = Box(value=42) # Box[Int]
let value = identity("pear") # identity[String]
let entry = Entry(key="age", value=18) # Entry[String, Int]
let some = Option.Some(value=1) # Option[Int].Some
let groups = lists.group_by(orders, fn(o: Order) => o.customer) # group_by[Order, String]You can always write them yourself, and then yours are used. It's all or nothing: once you write one, you write every one, in declaration order. Some places always need them:
let box = Box[Int](value=42)
let none = Option[Int].None # a case without a payload has no arguments to look at
let entries: List[Entry[String, Int]] = [] # in a type, you always write themHere's how the compiler works them out:
- Only the call's arguments count. The type you assign the result to, or the parameter you pass it to, never does.
- A plain number counts only when nothing else says what the type is. If
smallis anInt32,lists.sorted([small, 8])sortsInt32values, and the8becomes anInt32too.lists.sorted([1, 2])on its own sortsIntvalues. null,[]and{}say nothing. A list or map literal with elements counts by its elements. Passed for a plainT, a literal counts as a whole, and only when nothing else says whatTis: on its own,identity([1])gives aMutableList[Int], aslet xs = [1]does, but next to aList[Int]value the literal becomes aList[Int]too.- A lambda's parameter types are written, so they count as written; its body gives the result type.
- When arguments give different types, the one the others fit into wins:
Int32andIntmakeInt, and two cases of one variant make the variant. If there's no such type, the call is an error. - Java generic types such as
ArrayList[String]always take written type arguments. A Java generic method infers its own type arguments when its arguments fix them (Collections.sort(names)); one that only the result or a lambda would fix is written out; see JVM interop.
After that, every argument is checked against the types the compiler found, exactly as if you had written them. When the arguments can't say, the compiler stops and asks you to write them:
import "@std/lists.spr" as lists
let first: String? = lists.first([])SPR-TYPE-GENERIC-ARGS-REQUIRED [TYPE] main.spr:3:22: Cannot infer type argument T of 'lists.first': argument 1 is an empty list, which says nothing about T
hint: Write lists.first[Type](...) with T spelled out; type arguments are inferred only from a call's arguments, never from where its result goes.
1 error(s); run 'sprig explain <code>' for details on a diagnostic code.The String? on the left doesn't help, so write lists.first[String]([]). When two arguments disagree, the same error names both, for example T is String from argument 1 but Bool from argument 2. If an argument simply has the wrong shape for its parameter, such as a number where a function goes, you get the usual type mismatch instead, because writing the type arguments wouldn't help.
The wrong number of type arguments is rejected with SPR-TYPE-GENERIC-ARITY. That includes a bare Box in a type position and type arguments on a type that isn't generic.
What you can do with a type parameter
Inside a generic declaration, a value of type T can be assigned, passed, returned and stored in a compatible generic container. By default it has no operators and no methods: value + value is rejected with SPR-TYPE-OPERAND.
There are two capabilities you can add, both written first in the function body. The first is equality: start the function with requires T: Equatable, and == and != work on T, comparing by value:
generic T:
func same(a: T, b: T) -> Bool:
requires T: Equatable
return a == b
print(same("a", "a"))
print(same(1, 2))true
falseThe second is ordering: with requires T: Comparable, you can use <, <=, > and >= on T, and call sort() on a MutableList[T]:
generic T:
func larger(a: T, b: T) -> T:
requires T: Comparable
if a >= b:
return a
return b
generic T:
func sorted_copy(items: List[T]) -> List[T]:
requires T: Comparable
let copy = items.toMutableList()
copy.sort()
return copy.toList()
print(larger(3, 9))
print(larger("pear", "apple"))
print(sorted_copy([2.5, 0.5, 1.0]))9
pear
[0.5, 1.0, 2.5]- The Comparable types are
Int,Int32,Float,Float32,Decimal,BigIntandString, the types that already have<. A type parameter of the calling function qualifies too, if that function also declaresrequires X: Comparable. - A comparison means exactly what it means on the concrete type; for example, every comparison with a Float NaN is false.
- Every call is checked, whether you write the type argument or the compiler works it out. Passing a type without ordering is an error at the call:
generic T:
func larger(a: T, b: T) -> T:
requires T: Comparable
if a >= b:
return a
return b
print(larger(true, false))SPR-GENERIC-CONSTRAINT [TYPE] main.spr:8:7: Type argument 'Bool' for T is not Comparable, which 'larger' requires (expected Comparable type, actual Bool)
hint: Comparable types are Int, Int32, Float, Float32, Decimal, BigInt and String; for other types, pass an explicit comparison function.The two capabilities are independent: Comparable doesn't grant ==, so a function that needs both writes both requires lines. Any other capability name is rejected with SPR-GENERIC-CONSTRAINT.
Nullable type arguments
When the declaration stores T directly, Box[String?] is fine. But if the declaration already writes T?:
generic T:
class Box:
let value: T?then Box[String?] is rejected with SPR-TYPE-GENERIC-NULLABLE, because the declaration already decides whether that spot can be null. Write Box[String] instead.
When the compiler works the type out, it follows the same rule. A String? passed where the declaration writes T? makes T a String, so Box(value=maybe) is a Box[String]. Passed for a plain T, it makes T a String?, unless the declaration writes T? somewhere else; then T is String, and the nullable argument is reported as it would be with [String] written out.
Checked again at your call
A generic function or class is checked once, with T standing for any type. Rules that depend on the actual type are checked again at each call or construction with the types it gets, written or worked out, and that includes what the body does with T. A map literal keyed by T, or a List[T] handed to sets.of, makes T a Map key, so a Float is rejected at your call:
generic T:
func seen_before(value: T) -> Bool:
let seen = {value: true}
return value in seen
print(seen_before(0.5))SPR-NUM-CONVERSION [TYPE] main.spr:6:7: Float and Float32 cannot be Map keys: NaN and signed zero have no stable key equality
hint: Rejected inside 'seen_before' with T = Float, at main.spr:3:20. Use an explicit quantized Int key or Decimal key.The hint says where inside the generic code the type ends up, even when that's in a library such as @std/sets.spr. The nullable rule above works the same way: a String? that reaches a declaration writing T? is rejected at your call.
Generic variants
Variants can be generic too. A case with a payload works out its type arguments from the payload, like a constructor; a case without one, such as Option[Int].None, needs them written. In a match branch you write only the case name:
generic T:
variant Option:
Some:
value: T
None:
func describe(option: Option[Int]) -> String:
return match option:
case Option.Some as some:
"some " + some.value
case Option.None:
"none"
print(describe(Option.Some(value=1)))
print(describe(Option[Int].None))some 1
noneThe match still has to be exhaustive. Add a case to Option later, and every match that misses it is rejected with SPR-MATCH-NONEXHAUSTIVE.
When brackets mean indexing
values[index] is still indexing, not a type argument. The compiler decides by the name in front of the brackets: if it names a generic declaration, the brackets hold type arguments; otherwise they're an index. handlers[0](arg), indexing and then calling the result, works too.
What it compiles to
In the generated Java, generics are erased: a type parameter becomes Object, generic classes are raw types on the JVM, and the compiler adds boxing and casts where needed. The generics reference has the exact rules.
Imported Java classes can take concrete type arguments too, such as ArrayList[String]; see JVM interop.
Not implemented yet
Inferring type arguments from the expected type, variance and user-defined capabilities don't exist yet. The full list is in known limitations.
