2 ms·
> A potential fix for that is to base generic programming on explicit dictionary passing combined with implicit value parameter inference. Is there a language l
by ndriscoll 4y ago
> A potential fix for that is to base generic programming on explicit dictionary passing combined with implicit value parameter inference. Is there a language like this already?
If I understand the question, then yes this is how Scala works. When you write
def sort[T: Comparable](items: Vec[T])
It desugars to
def sort[T](items: Vec[T])(implicit c: Comparable[T])
Here Scala allows multiple parameter lists, Comparable[T] is a dictionary of methods, and the fact that it's an implicit parameter means there are some rules the compiler will follow to find the instance from either global or local scopes (e.g. methods with implicit parameters will implicitly thread that into other methods they call with implicit parameters of the same type). You can also explicitly pass the dictionary if you want, e.g. `sort(names)(stringComparator)`.
In practice, there's also implicit classes that get used to implement extension methods, so you might write `names.sort` and have that desugar to `VecExtensions(names).sort(stringComparator)`.