Well, that would be the Scala 2 behavior, which, as I said, was heavily criticized for having to specify type parameters.
Also, for the record, what the StackOverflow OP wants is consistent behavior regardless of how the expression is written. And, I think the only real way to accomplish that is by having full global type inference, but I could be wrong.
I agree that the behaviour is confusing and frustrating.
But I am just saying that it is not a bug; it is working as expected by the definition of the algorithm. Furthermore, I am saying that no matter what specification you have for the algorithm, someone will find a situation where that behaviour would be inconvenient.
Sure, I agree in principle, but again, that is a slippery slope.
First, that was the usual solution for the Scala 2 behavior, and while I didn’t have an issue with doing List.empty[Foo] over Nil, a lot of people did.
Second, and more importantly, the code you shared would still be incorrect in that version, it would rather need to be: val agg = Agg.create[String | Int](“first”).add(“second”).add(10), because you need the wider type at the start of the chain, not at the end.
Third, I could just exaggerate to the absurd, and say that just never let the compiler infer types, and just type everything yourself.
Anyways, again, I am not trying to say anyone is wrong here, nor defend the current behavior.
I am just pointing out why it is the way it is, and why it is impossible to get it “right”.
But, if you ask me, yes, I kind of prefer the Scala 2 variant.
And I would not be against a petition to change the Scala 3 version… but that would never happen, it would likely break existing code and cause more confusion.