Skip to content

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".
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?


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.

Co-Authored-By: Jamie Thompson <[email protected]>
@anatoliykmetyuk anatoliykmetyuk merged commit 2640412 into scala:master Dec 18, 2019
@anatoliykmetyuk anatoliykmetyuk deleted the fix-#7788 branch December 18, 2019 16:56
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