From eefe66f4e32383a377218c1733fb561c9f969cad Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Thu, 18 Apr 2019 15:54:09 +0200 Subject: [PATCH 1/2] Test demonstrating extension method shorthands This test shows that the overhead of extension methods can be reduced to be no more than 7 characters per method, using two tricks: - Any implicit parameters become parameters of the enclosing implied instance - A local type alias can be used to abbreviate the (repetitive) first parameter string. This demonstration strengthens the case for the current syntax for extension methods. --- tests/run/extmethods2.scala | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 tests/run/extmethods2.scala diff --git a/tests/run/extmethods2.scala b/tests/run/extmethods2.scala new file mode 100644 index 000000000000..57a94f9b6aa7 --- /dev/null +++ b/tests/run/extmethods2.scala @@ -0,0 +1,17 @@ +object Test extends App { + + class TC + + implied StringListOps given TC { + type T = List[String] + def (x: T) foo (y: T) = (x ++ y, the[TC]) + def (x: T) bar (y: Int) = (x(y), the[TC]) + } + + def test given TC = { + assert(List("abc").foo(List("def"))._1 == List("abc", "def")) + assert(List("abc").bar(2)._1 == "c") + } + + test given TC() +} \ No newline at end of file From 3eb8a788c19c6a70d98b382a22ec1c07dd88bcf1 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Thu, 18 Apr 2019 17:59:45 +0200 Subject: [PATCH 2/2] Fix test --- tests/run/extmethods2.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/run/extmethods2.scala b/tests/run/extmethods2.scala index 57a94f9b6aa7..febfc169bcf4 100644 --- a/tests/run/extmethods2.scala +++ b/tests/run/extmethods2.scala @@ -5,12 +5,12 @@ object Test extends App { implied StringListOps given TC { type T = List[String] def (x: T) foo (y: T) = (x ++ y, the[TC]) - def (x: T) bar (y: Int) = (x(y), the[TC]) + def (x: T) bar (y: Int) = (x(0)(y), the[TC]) } def test given TC = { assert(List("abc").foo(List("def"))._1 == List("abc", "def")) - assert(List("abc").bar(2)._1 == "c") + assert(List("abc").bar(2)._1 == 'c') } test given TC()