The document at https://github.com/rtfeldman/roc/tree/trunk/compiler/README.md makes a few references to something called specialization, but I'm having difficulty understanding exactly what that entails. Is there any chance that someone here knows a good resource that might be able to help explain it? Thank you very much!
it is a process of making specific versions of a function. For instance, it's hard to generate code For
List.get : Nat, List a -> Result a [ OutOfBounds ]*
A list is represented as an array of bytes. So when you say you want the 3rd element, what do you actually mean?
That depends on the actual width of the a
in question when you do a call
List.get_I64 : Nat, List I64 -> Result I64 [ OutOfBounds ]*
Here index 3 would mean starting at the (3 * 8 = ) 24th byte, while
List.get_I8 : Nat, List I8 -> Result I8 [ OutOfBounds ]*
now it means the (3 * 1 = ) 3rd byte
specialization is the process of making those Listl.get_*
functions
so when we encounter a List.get
in your program, we look at the actual type at that location, and (if it's not been done already) make a specific version at that point
That makes sense! Thank you!
when specializing with regard to type this is called monomorphization, because it turns polymorphism (the type variables) in to specific versions that have no polymorphism
but in general there are other things that we can specialize for (whether certain arguments can be updated in-place for instance)
Last updated: Jul 05 2025 at 12:14 UTC