Allow given instances to alias structural instances #17812
Replies: 0 comments 5 replies
-
What is a structural given? I don't think I've seen such terminology before. Can't you just write the following?: given Monad[List] & Traverse[List] = Instances.listInstance |
Beta Was this translation helpful? Give feedback.
-
It is the terminology for a given where there is no "rhs" |
Beta Was this translation helpful? Give feedback.
-
Thanks @bishabosha. I grabbed the term from the grammar:
https://docs.scala-lang.org/scala3/reference/contextual/givens.html#syntax |
Beta Was this translation helpful? Give feedback.
-
But you can't! Although you can with parentheses: trait Monad[F[_]]
trait Traverse[F[_]]
object Instances:
given listInstance: (Monad[List] & Traverse[List]) = new Monad[List] with Traverse[List] {}
object Functor:
given (Monad[List] & Traverse[List]) = Instances.listInstance
@main def main = { import Functor.given ; println(summon[Monad[List]] -> summon[Traverse[List]]) } https://scastie.scala-lang.org/Y006CKiITOO1hz8Hivy2fA Essentially everything in the feature request works, but requires parentheses in a way that's inconsistent with the rest of the grammar – you don't need parentheses for an equivalent |
Beta Was this translation helpful? Give feedback.
-
@neko-kai Nice! I had tried the intersection but hadn't thought to use parens! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Consider a structural given:
Now imagine we want to define an alias for this instance in another module:
This is not currently supported. Structural instances cannot be aliased via
=
. Note the following works fine though:Beta Was this translation helpful? Give feedback.
All reactions