Skip to content

Document using parameters on classes can provide given instances #10882

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
bishabosha opened this issue Dec 22, 2020 · 4 comments · Fixed by #12312
Closed

Document using parameters on classes can provide given instances #10882

bishabosha opened this issue Dec 22, 2020 · 4 comments · Fixed by #12312

Comments

@bishabosha
Copy link
Member

bishabosha commented Dec 22, 2020

We should probably document that using clauses with parameters annotated val/var generate a given instance in their body (this is obvious for Scala 2 users, but maybe not for someone who has only ever seen the new syntax, where using may have been understood as only a "consumer" of given instances)

Minimized example

class GivenIntBox(using val givenInt: Int)

@main def test =
  val b = GivenIntBox(using 23)

  import b.given

  println(summon[Int])

Output

23
@b-studios
Copy link
Contributor

b-studios commented Jan 8, 2021

But this is also the case with contextual parameters on methods, and with contextual functions right? They also introduce a "given" in the corresponding scope. Retract...you are right .. it should be explained.

@b-studios b-studios self-assigned this Jan 8, 2021
@amitport
Copy link

amitport commented May 2, 2021

BUT, why does it generate a given instance? It seems completely surprising behavior to me. It would be much better to have an explicit declaration than to allow this, IMHO.

@som-snytt
Copy link
Contributor

I donated some explanatory words which might be approximately correct and about in the right spot.

I remember there was a use case for export this.foo as bar, and extending that, export this.member as given T or something. But that is idle speculation.

I don't know that this is obvious for Scala 2 users. There are other gotchas around class parameters and aliasing, such as accidental shadowing, so maybe it's not completely surprising that some behaviors are completely surprising.

@amitport
Copy link

amitport commented May 3, 2021

"it's not completely surprising that some behaviors are completely surprising" :) yes, one gets used to it with scala. Still, it's not the best quality of the language.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants