Skip to content

Commit

Permalink
syntax refinements
Browse files Browse the repository at this point in the history
  • Loading branch information
pweisenburger committed Nov 23, 2016
1 parent eb306f3 commit c41836e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 30 deletions.
19 changes: 10 additions & 9 deletions README.md
Expand Up @@ -23,7 +23,7 @@ def g(implicit x: Int) = x.toString

> f("Hi, " + g)
// desugaring
> f { implicit $bang => "Hi, " + g }
> f { implicit imparg$1 => "Hi, " + g }
Hi, 5
```

Expand All @@ -40,11 +40,11 @@ class Thingy {
def f(a: (import Thingy) => Int) = println(a(new Thingy))

// current syntax as implemented in the compiler plugin
def f(a: Thingy `import._ =>` Int) = println(a(new Thingy))
def f(a: Thingy `import =>` Int) = println(a(new Thingy))

> f(4 + u - v)
// desugaring
> f { $bang => import $bang._; 4 + u - v }
> f { imparg$1 => import imparg$1._; 4 + u - v }
3
```

Expand All @@ -59,7 +59,7 @@ object Thingy {
// no original syntax proposal for the Scala language extension

// current syntax as implemented in the compiler plugin
def f(a: Int `import._` Thingy.type) = println(a)
def f(a: Int `import` Thingy.type) = println(a)

> f(u + 1)
// desugaring
Expand Down Expand Up @@ -97,8 +97,9 @@ The latter only defines the types used by the plugin as follows:
```scala
package object dslparadise {
type `implicit =>`[-T, +R] = T => R
type `import._ =>`[-T, +R] = T => R
type `import._`[+T, I] = T
type `implicit import =>`[-T, +R] = T => R
type `import =>`[-T, +R] = T => R
type `import`[+T, I] = T
}
```

Expand Down Expand Up @@ -148,7 +149,7 @@ dirty-namespace-but-convenient could be solved by Static Scope Injection, e.g.
if `Pattern.compile` was defined as:

```scala
def compile(s: String, flags: Int `import._` Pattern) = ...
def compile(s: String, flags: Int `import` Pattern) = ...
```

You could then write:
Expand Down Expand Up @@ -260,7 +261,7 @@ Or equivalently using Scope Injection:
class ImplicitHolder {
implicit val session: Session = ...
}
def withSession[T](thunk: ImplicitHolder `import._ =>` T)
def withSession[T](thunk: ImplicitHolder `import =>` T)
```

In which case you could write:
Expand Down Expand Up @@ -344,7 +345,7 @@ With Scope Injection, you could define `async` as:
object Holder {
val await = ???
}
macro def async[T](thunk: Holder `import._ =>` T): Future[T] = ???
macro def async[T](thunk: Holder `import =>` T): Future[T] = ???
```

Which would give you a call-site syntax:
Expand Down
7 changes: 3 additions & 4 deletions library/src/main/scala/dslparadise/package.scala
@@ -1,7 +1,6 @@
package object dslparadise {
type `implicit =>`[-T, +R] = T => R

type `import._ =>`[-T, +R] = T => R

type `import._`[+T, I] = T
type `implicit import =>`[-T, +R] = T => R
type `import =>`[-T, +R] = T => R
type `import`[+T, I] = T
}
8 changes: 6 additions & 2 deletions plugin/src/main/scala/dslparadise/typechecker/Typers.scala
Expand Up @@ -16,11 +16,15 @@ trait Typers {
q"{ implicit $$bang => $arg }"
},

"dslparadise.import._ =>" -> { (arg: Tree, pt: Type) =>
"dslparadise.implicit import =>" -> { (arg: Tree, pt: Type) =>
q"{ implicit $$bang => import $$bang._; $arg }"
},

"dslparadise.import =>" -> { (arg: Tree, pt: Type) =>
q"{ $$bang => import $$bang._; $arg }"
},

"dslparadise.import._" -> { (arg: Tree, pt: Type) =>
"dslparadise.import" -> { (arg: Tree, pt: Type) =>
q"{ import ${pt.typeArgs(1).typeSymbol.companionSymbol}._; $arg }"
}
)
Expand Down
30 changes: 15 additions & 15 deletions sandbox/src/main/scala/Main.scala
Expand Up @@ -12,7 +12,7 @@ object ImplicitContextPropagation {
def g(implicit x: Int) = x.toString

f("Hi, " + g)
f { implicit $bang => "Hi, " + g }
f { implicit imparg => "Hi, " + g }
}


Expand All @@ -21,21 +21,21 @@ object ScopePropagation {
val u = 6
val v = 7
}
def f(a: Thingy `import._ =>` Int) = println(a(new Thingy))

def f(a: Thingy `import =>` Int) = println(a(new Thingy))

f(4 + u - v)
f { $bang => import $bang._; 4 + u - v }
}
f { imparg => import imparg._; 4 + u - v }
}


object StaticScopeInjection {
object Thingy {
val u = 6
}
def f(a: Int `import._` Thingy.type) = println(a)

def f(a: Int `import` Thingy.type) = println(a)

f(u + 1)
f { import Thingy._; u + 1 }
}
Expand All @@ -45,7 +45,7 @@ object StaticScopeInjection {
object JavaRegex {
class Pattern
object Pattern {
def compile(s: String, i: Int `import._` Pattern): Pattern = new Pattern
def compile(s: String, i: Int `import` Pattern): Pattern = new Pattern

val MULTILINE = 0
val CASE_INSENSITIVE = 0
Expand All @@ -62,10 +62,10 @@ object SprayHttpResponse {
class HttpResponse
object HttpResponse {
def apply(
a: StatusCode `import._` StatusCodes.type,
b: HttpEntity `import._` HttpEntity.type,
c: List[HttpHeader] `import._` HttpHeaders.type,
d: HttpProtocol `import._` HttpProtocols.type) = new HttpResponse
a: StatusCode `import` StatusCodes.type,
b: HttpEntity `import` HttpEntity.type,
c: List[HttpHeader] `import` HttpHeaders.type,
d: HttpProtocol `import` HttpProtocols.type) = new HttpResponse
}

class StatusCode
Expand All @@ -80,7 +80,7 @@ object SprayHttpResponse {

class HttpHeader
object HttpHeaders {
def Accept(a: Seq[MediaRange] `import._` MediaRanges.type) = new HttpHeader
def Accept(a: Seq[MediaRange] `import` MediaRanges.type) = new HttpHeader
}

class MediaRange
Expand Down

0 comments on commit c41836e

Please sign in to comment.