Skip to content

Commit ebc1b11

Browse files
author
Benjamin Hagemeister
committed
Fixed evaluation of constructor parameter with Scala 2.10
1 parent b36337a commit ebc1b11

File tree

7 files changed

+10
-57
lines changed

7 files changed

+10
-57
lines changed

README.md

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -492,23 +492,6 @@ def foo(bar: String, baz: Int)(p: Boolean): Unit = {
492492
foo("baz", 42)(true) // foo(bar=baz, baz=42)(p=true)
493493
```
494494

495-
Please note, that it's currently not possible to access the arguments passed
496-
to a constructor if you are using Scala 2.10, e.g. the following code will
497-
work as expected with 2.11, but _not_ with 2.10:
498-
```scala
499-
def debug(implicit name: sourcecode.Name, args: sourcecode.Args): Unit = {
500-
println(name.value + args.value.map(_.map(a => a.source + "=" + a.value).mkString("(", ", ", ")")).mkString(""))
501-
}
502-
503-
class Foo(arg1: String, arg2: Int) {
504-
debug
505-
}
506-
507-
new Foo("bar", 42)
508-
```
509-
510-
For 2.10 `sourcecode.Args.value will be an empty list.`
511-
512495
Embedding Domain-Specific Languages
513496
-----------------------------------
514497

build.sbt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ lazy val sourcecode = crossProject.settings(
2222
if (scalaVersion.value startsWith "2.10.") Seq(baseDirectory.value / ".."/"shared"/"src"/ "main" / "scala-2.10")
2323
else Seq(baseDirectory.value / ".."/"shared" / "src" / "main" / "scala-2.11")
2424
},
25-
unmanagedSourceDirectories in Test ++= {
26-
if (scalaVersion.value startsWith "2.10.") Seq(baseDirectory.value / ".."/"shared"/"src"/ "test" / "scala-2.10")
27-
else Seq(baseDirectory.value / ".."/"shared" / "src" / "test" / "scala-2.11")
28-
},
2925
publishTo := Some("releases" at "https://oss.sonatype.org/service/local/staging/deploy/maven2"),
3026

3127
pomExtra :=

sourcecode/shared/src/main/scala-2.10/sourcecode/Compat.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ object Compat{
1515
if (owner.isMethod || owner.isClass) owner else nearestClassOrMethod(owner.owner)
1616

1717
val com = nearestClassOrMethod(enclosingOwner(c))
18-
if (com.isClass) List() else com.asMethod.paramss
18+
if (com.isClass) {
19+
val pc = com.typeSignature.members.filter(m => m.isMethod && m.asMethod.isPrimaryConstructor)
20+
pc.head.asMethod.paramss
21+
} else {
22+
com.asMethod.paramss
23+
}
1924
}
2025
}

sourcecode/shared/src/test/scala-2.10/sourcecode/ArgsPrimaryConstructorTests.scala

Lines changed: 0 additions & 17 deletions
This file was deleted.

sourcecode/shared/src/test/scala-2.11/sourcecode/ArgsPrimaryConstructorTests.scala

Lines changed: 0 additions & 17 deletions
This file was deleted.

sourcecode/shared/src/test/scala/sourcecode/ArgsTests.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,17 @@ object ArgsTests {
2727
}
2828

2929
class Foo(p1: String, p2: Long, p3: Boolean)(foo: String, bar: String) {
30+
debug
3031

3132
def this(p1: String, p2: Long) = {
3233
this(p1, p2, false)("foo", "bar")
3334
debug
3435
}
3536
}
3637

38+
new Foo("text", 42, false)("foo", "bar")
39+
assert(args == Seq(Seq("p1" -> "text", "p2" -> 42, "p3" -> false), Seq("foo" -> "foo", "bar" -> "bar")))
40+
3741
new Foo("text", 42)
3842
assert(args == Seq(Seq("p1" -> "text", "p2" -> 42)))
3943

sourcecode/shared/src/test/scala/sourcecode/Tests.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ object Tests{
2121
ManualImplicit()
2222
TextTests()
2323
ArgsTests()
24-
ArgsPrimaryConstructorTests()
2524

2625
println("================LogExample================")
2726
logExample()

0 commit comments

Comments
 (0)