Sorry, if the title is not very clear but I really do not know how to synthesis this in a few words.
Let me explain it in details and provide a motivating example of why this functionality would be useful.
Imagine a definition like this:
def foo[B, C](bar: Bar[B, C])(f: A => B): C
Where instances of bar
does not really infer what B
should be, but does impose constraints on it, like requiring an instance of Ordering. I would like if the compiler could carry that constraint such that when inferring B
from the function, it ensures that constraint is satisfied.
Now, I know it may be hard to imagine when that could be useful, so here is an example.
I was trying to implement the following function:
// It was actually an extension method on IterableOnce.
def groupMapTo[A, K, B, CC, MC](data: IterableOnce[A])
(colFactory: Factory[B, CC], mapFactory: Factory[(K, B), MC] = Map)
(key: A => K)(f: A => B): MC
// So it could be used like this:
list.groupMapTo(colFactory = SortedSet)(_.foo)(_.bar)
The above definition doesn’t work because B
is already inferred to be Nothing
; and thus it had to be changed to require the factories after the functions, which made calling this function less readable (IMHO).
It would be great if it could just wait for f
to know what B
is. Nevertheless, since I used SortedSet instead of a normal Set, it requires an Ordering, so even if it shouldn’t infer what B
is, it should add that constraint. So, if f
is inferred to return something which is not sortable, then it wouldn’t compile.
(Similarly, it would be good if it also could carry type bounds)
Finally, the main objective of this post would be to answer the following three questions:
- Is this just an improvement over the compiler or is this considered a change in the language specifications?
- Regardless of the previous question, how feasible / difficult would be to implement this change?
- Does more people find this useful? Does anyone have other examples?