Skip to content

Commit

Permalink
Remove Scope in favor of simple (A=>R)=>R
Browse files Browse the repository at this point in the history
  • Loading branch information
Atry committed Feb 28, 2018
1 parent 587ab79 commit 4161b44
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 81 deletions.
17 changes: 14 additions & 3 deletions Dsl/src/main/scala/com/thoughtworks/dsl/Dsl.scala
@@ -1,9 +1,6 @@
package com.thoughtworks.dsl

import com.thoughtworks.dsl.Dsl.shift

import scala.annotation.{Annotation, StaticAnnotation, TypeConstraint, compileTimeOnly}
import scala.util.control.NonFatal

/** The domain-specific interpreter for `Instruction` in `Domain`,
* which is a dependent type type class that registers an asynchronous callback function,
Expand All @@ -21,6 +18,20 @@ trait Dsl[-Instruction, Domain, +Value] {

object Dsl {

implicit def scopeDsl[Instruction, Domain, FinalResult, InstructionValue](
implicit restDsl: Dsl[Instruction, Domain, InstructionValue])
: Dsl[Instruction, (FinalResult => Domain) => Domain, InstructionValue] = {
new Dsl[Instruction, (FinalResult => Domain) => Domain, InstructionValue] {
def interpret(instruction: Instruction,
handler: InstructionValue => (FinalResult => Domain) => Domain): (FinalResult => Domain) => Domain = {
(continue: FinalResult => Domain) =>
restDsl.interpret(instruction, { a =>
handler(a)(continue)
})
}
}
}

final case class Catch[Domain](onFailure: Throwable => Domain)
extends AnyVal
with Instruction[Catch[Domain], Domain => Domain]
Expand Down
8 changes: 1 addition & 7 deletions build.sbt
Expand Up @@ -18,14 +18,8 @@ lazy val `instructions-ScalazBind` =
lazy val `instructions-CatsFlatMap` =
project.dependsOn(Dsl, `instructions-Shift` % Test, `instructions-Yield` % Test)

lazy val `domains-Scope` = project.dependsOn(Dsl)

lazy val `instructions-Arm` =
project.dependsOn(Dsl,
`domains-Scope`,
`instructions-Shift`,
`instructions-Yield` % Test,
`domains-ExceptionHandling`)
project.dependsOn(Dsl, `instructions-Shift`, `instructions-Yield` % Test, `domains-ExceptionHandling`)

organization in ThisBuild := "com.thoughtworks.dsl"

Expand Down

This file was deleted.

Expand Up @@ -2,7 +2,6 @@ package com.thoughtworks.dsl.instructions

import com.thoughtworks.dsl.Dsl
import com.thoughtworks.dsl.Dsl.Instruction
import com.thoughtworks.dsl.domains.{ExceptionHandling, Scope}
import resource.Resource

/**
Expand Down Expand Up @@ -32,27 +31,4 @@ object Arm {
}
}

implicit def armDsl[Domain, R](
implicit dsl: com.thoughtworks.dsl.Dsl[com.thoughtworks.dsl.Dsl.Catch[Domain], Domain, Domain => Domain])
: Dsl[Arm[R], Scope[Domain], R] = new Dsl[Arm[R], Scope[Domain], R] {
def interpret(arm: Arm[R], inUse: R => Scope[Domain]): Scope[Domain] = {
val resourceFactory = arm.resourceFactory
val resource = arm.resource

new Scope[Domain] {
def apply(continue: Domain => Domain): Domain = {
continue {
val r = resourceFactory()
try {
resource.open(r)
!Shift(inUse(r))
} finally {
resource.close(r)
}
}
}
}
}
}

}
@@ -1,6 +1,6 @@
package com.thoughtworks.dsl.instructions

import com.thoughtworks.dsl.domains.{ExceptionHandling, Scope}
import com.thoughtworks.dsl.domains.ExceptionHandling
import com.thoughtworks.dsl.instructions.Shift.Continuation
import org.scalatest.{FreeSpec, Matchers}

Expand All @@ -10,28 +10,16 @@ import org.scalatest.{FreeSpec, Matchers}
class ArmSpec extends FreeSpec with Matchers {

"AutoCloseable" - {
//
// "using" in {
// var isOpen = false
// def autoCloseable: Unit = {
// isOpen should be(false)
// !Arm(new AutoCloseable {
// def close(): Unit = {
// isOpen should be(true)
// isOpen = false
// }
// })
// isOpen should be(true)
// }
// isOpen should be(false)
// }

type Scope[A] = (A => A) => A
def noop[A](a: A): Scope[A] = _(a)

"scope" - {

"arm" in {
var isOpen = false

def raii: Scope[ExceptionHandling[Stream[Int]]] = Scope.noop {
def raii: Scope[ExceptionHandling[Stream[Int]]] = noop {
ExceptionHandling.success {
!Yield(1)
isOpen should be(false)
Expand Down

0 comments on commit 4161b44

Please sign in to comment.