Skip to content
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

Fix #7788: Add new syntax for conditional given instances #7794

Merged
merged 7 commits into from
Dec 18, 2019

Conversation

odersky
Copy link
Contributor

@odersky odersky commented Dec 17, 2019

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.

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] =
Copy link
Member

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")))

Copy link
Member

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?

compiler/src/dotty/tools/dotc/parsing/Parsers.scala Outdated Show resolved Hide resolved

val isConditional =
in.token == ARROW
&& vparamss.length == 1
Copy link
Member

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"))

@odersky
Copy link
Contributor Author

odersky commented Dec 18, 2019

@bishabosha Should be fixed now. I did not include a neg test since the old syntax will probably be dropped anyway.

tests/run/i7788.scala Outdated Show resolved Hide resolved
Co-Authored-By: Jamie Thompson <bishbashboshjt@gmail.com>
tests/run/i7788.scala Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants