Skip to content

Commit eefe66f

Browse files
committed
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.
1 parent 874a203 commit eefe66f

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

tests/run/extmethods2.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
object Test extends App {
2+
3+
class TC
4+
5+
implied StringListOps given TC {
6+
type T = List[String]
7+
def (x: T) foo (y: T) = (x ++ y, the[TC])
8+
def (x: T) bar (y: Int) = (x(y), the[TC])
9+
}
10+
11+
def test given TC = {
12+
assert(List("abc").foo(List("def"))._1 == List("abc", "def"))
13+
assert(List("abc").bar(2)._1 == "c")
14+
}
15+
16+
test given TC()
17+
}

0 commit comments

Comments
 (0)