-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #7788: Add new syntax for conditional given instances #7794
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
Conversation
Treat given (A, B) => ... as equivalent to given A, B => ... Motivation: Because of the similarity with function types, it's very easy to wrap multiple types in parentheses by mistake. On the other hand, it should be extremely rare to demand a tuple of givens. So the syntax tweak is there to "do what I mean".
@@ -3440,19 +3468,43 @@ object Parsers { | |||
stats.foreach(checkExtensionMethod(tparams, _)) | |||
ModuleDef(name, Template(makeConstructor(tparams, vparamss), Nil, Nil, self, stats)) | |||
else | |||
checkAllGivens(vparamss, "parameter of given instance") | |||
def conditionalParents(): List[Tree] = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The rules still accept given [A]: (given showA: Show[A]) => Show[List[A]]
e.g. :
trait Show[-A] with
def show(a:A): String
given Show[String] = x => x
given Show[Int] = _.toString
given [A,B]: (given sA: Show[A], sB: Show[B]) => Show[(A,B)] = (a,b) => s"(${sA.show(a)}, ${sB.show(b)})"
given [A]: (given Show[A]) => Show[List[A]] = as => as.map(summon[Show[A]].show).mkString(", ")
given showOption[A]: (given Show[A]) => Show[Option[A]] = o => o.map(summon[Show[A]].show).fold("Nothing")(s => s"Some($s)")
given showEither[A,B]: (given sA: Show[A], sb: Show[B]) => Show[Either[A,B]] = _.fold(a => s"Left(${summon[Show[A]].show(a)})", b => s"Right(${summon[Show[B]].show(b)})")
@main def ShowDemo =
println(summon[Show[(Int, String)]].show(0 -> "hello"))
println(summon[Show[List[Int]]].show(List(1,2,3)))
println(summon[Show[Option[Int]]].show(Some(25)))
println(summon[Show[Either[Int, String]]].show(Left(-1)))
println(summon[Show[Either[Int, String]]].show(Right("success message")))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this syntax is still ok to be alongside the other?
|
||
val isConditional = | ||
in.token == ARROW | ||
&& vparamss.length == 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there should probably be a neg test for curried param lists:
trait Show[-A] with
def show(a:A): String
given Show[String] = x => x
given Show[Int] = _.toString
given [A,B]: (sA: Show[A])(sB: Show[B]) => Show[(A,B)] = (a,b) => s"(${sA.show(a)}, ${sB.show(b)})"
given [A,B,C]: (Show[A])(Show[B])(Show[C]) => Show[(A,B,C)] = (a,b,c) => s"(${summon[Show[A]].show(a)}, ${summon[Show[B]].show(b)}, ${summon[Show[C]].show(c)})"
given showEither[A,B]: (sA: Show[A])(sb: Show[B]) => Show[Either[A,B]] = _.fold(a => s"Left(${summon[Show[A]].show(a)})", b => s"Right(${summon[Show[B]].show(b)})")
@main def ShowDemo =
println(summon[Show[(Int, String)]].show(0 -> "hello"))
@bishabosha Should be fixed now. I did not include a neg test since the old syntax will probably be dropped anyway. |
Co-Authored-By: Jamie Thompson <[email protected]>
Trial implementation to experiment with the syntax proposed in #7788.
In the end we will keep only the new or the old way of writing things.