in the solution proposed in this thread, one of the gains is that the companion object you’re selecting from is chosen based on type inference. in case of non-trivial type inference, the computed types can vary a lot depending on particular call site, so to emulate it with imports you would need to do the type inference manually and import from all inferred objects. anyway, after some deliberation, this argument looks somewhat weak (but still valid) to me, and other arguments (which revolve aroud uncluttering code) should be discussed instead.
however, if you go by the import ...{something as $}
route then you probably need to introduce many short symbols sometimes and that’s a bit of ugliness already, e.g.
import html.{Tags as <, Attrs as ^, Colors as C} // ... and so on
you can bundle all of it together under one scope (that’s probably often the case with html constructs), but often you woudn’t want to.
should there be ambiguity at all? for me, the natural choice would be to inject scope separately per argument, so there would be no ambiguity:
// example invocation, copied from quote above, but with my comments
environment(
// here the target type of this argument is `Light`, so we have injected `import Light._` just for this argument
Off,
// here the target type of this argument is `Water`, so we have injected `import Water._` just for this argument
On
)
could you elaborate on that? the notation without leading dot still looks convincing to me.
the compatibility is on the source code level, not binary compatibility.
to minimize suprises when using notation without leading dot, we could require language import to enable the feature. compiler would always work as if the scope injection is enabled, but then the language import would be checked and if it’s not in scope, then throw compilation problem (waring or error).
example where it works:
import scala.language.scopeInjection
foo(injected) // works
example where compilation fails:
// no import from scala.language
foo(injected) // compilation fails. injection detected, but not allowed.
note that the above stuff is just a suggestion, food for thought. i haven’t deliberated much on it. maybe the notation with leading dot would be way easier to implement and maintain.