-
Notifications
You must be signed in to change notification settings - Fork 1k
Updated pages to be “Scala 3 Only” #2689
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ num: 61 | |
previous-page: ca-given-using-clauses | ||
next-page: ca-given-imports | ||
--- | ||
<span class="tag tag-inline">Scala 3 only</span> | ||
|
||
|
||
{% comment %} | ||
|
@@ -22,29 +23,54 @@ In that case you don’t have to define a parameter name, and can just provide t | |
|
||
For example, this `maximum` method takes a _context parameter_ of type `Ord`, only to pass it on as an argument to `max`: | ||
|
||
{% tabs context-bounds-max-named-param %} | ||
|
||
{% tab 'Scala 3 Only' %} | ||
|
||
```scala | ||
def maximum[A](xs: List[A])(using ord: Ord[A]): A = | ||
xs.reduceLeft(max(ord)) | ||
julienrf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
{% endtab %} | ||
|
||
{% endtabs %} | ||
|
||
In that code the parameter name `ord` isn’t actually required; it can be passed on as an inferred argument to `max`, so you just state that `maximum` uses the type `Ord[A]` without giving it a name: | ||
|
||
{% tabs context-bounds-no-param-name %} | ||
|
||
{% tab 'Scala 3 Only' %} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok so anonymous context parameter truly is a scala 3 only thing, so if you add Scala 2 tab to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’ve removed this example from the ca-context-bounds.md file because I don’t think it should be part of that file. Instead, I think we should move it to the file ca-given-using-clauses.md. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, maybe it’s better to have it in this file. If you think it should be here, let me know and I’ll update it. |
||
|
||
```scala | ||
def maximum[A](xs: List[A])(using Ord[A]): A = | ||
xs.reduceLeft(max) | ||
``` | ||
|
||
{% endtab %} | ||
|
||
{% endtabs %} | ||
|
||
|
||
## Context bounds | ||
|
||
Given that background, a _context bound_ is a shorthand syntax for expressing the pattern of, “a context parameter that depends on a type parameter.” | ||
|
||
Using a context bound, the `maximum` method can be written like this: | ||
|
||
{% tabs context-bounds-max-rewritten %} | ||
|
||
{% tab 'Scala 3 Only' %} | ||
julienrf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```scala | ||
def maximum[A: Ord](xs: List[A]): A = xs.reduceLeft(max) | ||
``` | ||
|
||
{% endtab %} | ||
|
||
{% endtabs %} | ||
|
||
|
||
A bound like `: Ord` on a type parameter `A` of a method or class indicates a context parameter with `Ord[A]`. | ||
|
||
For more information about context bounds, see the [“What are context bounds?”](https://docs.scala-lang.org/tutorials/FAQ/context-bounds.html) section of the Scala FAQ. |
Uh oh!
There was an error while loading. Please reload this page.