-
Notifications
You must be signed in to change notification settings - Fork 1.1k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
Doc pages for packagings and givens #17808
Comments
This would be really helpful! Especially I'd like to understand why this Welcome to Scala 3.1.0 (11.0.11, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.
scala> object X:
| given Int = 42
|
// defined object X
scala> def a =
| import X.given Int
| given Int = 43
| summon[Int]
|
def a: Int
scala> a
val res0: Int = 42 does not give |
I'll try to supply this text and also discover where to stash it. I understand packages as packagings, but I've haven't looked at "encounter-order of contextual implicits". The other feature request would be, Provide a |
Thanks @som-snytt ! I'm currently working on a slide that tries to explain this to beginners (I'm this year daring to teach Scala 3 given using, while I previous years deemed Scala 2 implicits out of scope...). This is what slide 29 says (translated from Swedish):
Is this text OK as an approximation of the full story for now? (next weeks lecturing starts on Tuesday...) |
BTW: I'm introducing givens as a more flexible and powerful form of default arguments, to connect to what they already should know. |
@bjornregnell that is interesting and ambitious. I bet they have Coursera materials that might provide guidance or "wording". That's an interesting idea: default args supply an arg without context, and a given is the same thing but contextual because it is supplied by the current context. An example might be I agree that your items 2 & 3 are the fundamental concept, "lexical scope" vs "implicit scope", where implicit scope has to do with "related types" but our first intuition is the companion object of the type we wish to summon. I'm still sorting item 4. 😄 |
@bjornregnell in your snippet the synthesized names of the givens are both |
Hmm. @prolativ So you mean that if the are called the same they are not ambiguous but if they are called differently they are ambiguous -- should it not be the other way around? And: Did you mean that this is a bug that the priority is "backwards"? |
Here is added printing of synthesized names as suggested: scala> object X:
| given Int = 42
|
// defined object X
scala> X.given_Int
val res0: Int = 42
scala> def a =
| import X.given Int
| given Int = 43
| println(given_Int)
| println(X.given_Int)
| summon[Int]
|
def a: Int
scala> a
43
42
val res1: Int = 42 |
I mean if they have the same name then one definition shadows the other so this is not seen as an ambiguity. But the strange thing is that the rules of shadowing are different when referencing definitions directly and when searching for implicits |
Aha. Thnx. Yes strange. |
Should we file an issue -- is this a bug? |
Or is the closing of this a "won't fix"? #8092 |
I updated the closed ticket with a cleaner example. I suspect the ticket will remain closed and the behavior is just a puzzler. But it is really an edge case, in the sense that one should not introduce givens into scope in this way. What's needed is a scalafix lint to warn about it. (It's like old C puzzlers where you must decode the order in which I don't understand the previous comment about naming, because Scala 3 doesn't have Scala 2 shadowing of implicits. Maybe I will understand the comment after I trying to write some doc. (I also need more experience with the syntax. There is another ticket to document |
Default args also have context, so that starting-point in pre-knowledge seams reasonable: scala> var ctx = 42 // don't do this at home
var ctx: Int = 42
scala> def f(x: Int = ctx) = x + 1
def f(x: Int): Int
scala> f()
val res0: Int = 43
scala> ctx = 43
ctx: Int = 43
scala> f()
val res1: Int = 44 |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Please add overview doc pages for packagings and givens, per
#11296
#8092
The import vs local for givens came up on the forum, and Bjorn requested an explanatory page.
These pages should not assume Scala 2 expertise, and should also cover these "edge" cases.
They should explain
and
The text was updated successfully, but these errors were encountered: