Allow Using or scopeExit in plain functions#253
Allow Using or scopeExit in plain functions#253lxohi wants to merge 7 commits intoThoughtWorksInc:1.5.xfrom
Conversation
|
What's the difference from Using?
lxohi <notifications@github.com> 于 2019年6月1日周六 02:45写道:
… Defer keyword works likes defer in Golang.
It runs the deferred statements in the reversed sequence when current
function finished.
------------------------------
You can view, comment on, or merge this pull request online at:
#253
Commit Summary
- .gitignore: temporarily adds .idea
- create keywords-Defer sub-project
- create Defer.scala
- README.md: add description of Defer keyword
File Changes
- *M* .gitignore
<https://github.com/ThoughtWorksInc/Dsl.scala/pull/253/files#diff-0>
(1)
- *M* README.md
<https://github.com/ThoughtWorksInc/Dsl.scala/pull/253/files#diff-1>
(1)
- *M* build.sbt
<https://github.com/ThoughtWorksInc/Dsl.scala/pull/253/files#diff-2>
(13)
- *A* keywords-Defer/.js/build.sbt
<https://github.com/ThoughtWorksInc/Dsl.scala/pull/253/files#diff-3>
(1)
- *A* keywords-Defer/.jvm/build.sbt
<https://github.com/ThoughtWorksInc/Dsl.scala/pull/253/files#diff-4>
(1)
- *A* keywords-Defer/build.sbt
<https://github.com/ThoughtWorksInc/Dsl.scala/pull/253/files#diff-5>
(1)
- *A*
keywords-Defer/src/main/scala/com/thoughtworks/dsl/keywords/Defer.scala
<https://github.com/ThoughtWorksInc/Dsl.scala/pull/253/files#diff-6>
(78)
Patch Links:
- https://github.com/ThoughtWorksInc/Dsl.scala/pull/253.patch
- https://github.com/ThoughtWorksInc/Dsl.scala/pull/253.diff
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#253?email_source=notifications&email_token=AAES3OUHF5KRU6IX56IF5D3PYJAM7A5CNFSM4HSAEQ72YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4GXCXYFQ>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAES3OV4KF2JVYKPSVK2NYTPYJAM7ANCNFSM4HSAEQ7Q>
.
|
|
We can let implicit def toAutoCloseable(op: => Unit): () => AutoCloseable = () => {
new AutoCloseable {
override def close(): Unit = op
}
}
private def tryUsingToDefer() = Task {
!Using(println("1"))
!Using(println("2"))
!Using(println("3"))
println("hello world")
}But I don't think it is a good practice because:
And by the way the current implementation of |
|
I think the implicit conversion is not necessary because Scala 2.12+ (and
2.11 with -Xexperimental) support SAM types.
lxohi <notifications@github.com> 于 2019年6月1日周六 22:38写道:
… Using side:
- Execute the enclosed op immediately.
- The op needs to return AutoCloseable.
- Call the AutoCloseable.close() at the end of current function.
Defer side:
- Execute the enclosed op at the end of current function.
- The op have no need to return anything.
------------------------------
We can let Using do the similar thing by adding a implicit conversion
like this:
implicit def toAutoCloseable(op: => Unit): () => AutoCloseable = () => {
new AutoCloseable {
override def close(): Unit = op
}
}
private def tryUsingToDefer() = Task {
!Using(println("1"))
!Using(println("2"))
!Using(println("3"))
println("hello world")
}
But I don't think it is a good practice because:
- The behavior of that implicit conversion looks a little bit weird.
- This may cause confusion with !Using lines. Adding this implicit
conversion will cause the meaning of Using keyword becomes *if you put
a AutoCloseable in the parentheses it will do A otherwise it will do B*
.
And by the way the current implementation of Defer is allowed to be used
in a normal function block.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#253?email_source=notifications&email_token=AAES3OTISYEKZPVZO2FWBQ3PYNMDVA5CNFSM4HSAEQ72YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODWXOI7A#issuecomment-498001020>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAES3OQO4D5P6J6LFF2XCM3PYNMDVANCNFSM4HSAEQ7Q>
.
|
|
I've tried using SAM types in Scala 2.11 with modified code like this: private def tryUsingToDefer() = Task {
!Using(() => println("1"))
!Using(() => println("2"))
!Using(() => println("3"))
println("hello world")
}It just not compile. I think this is because the parameter type of |
|
Yes, you are right. We need the following method to enable SAM types in Scala 2.12+. object Using {
def apply[R <: AutoCloseable](r: => R)(
implicit dummyImplicit: DummyImplicit = DummyImplicit.dummyImplicit): Using[R] = new Using(r _)
def apply(r: => AutoCloseable)(implicit dummyImplicit: DummyImplicit, dummyImplicit2: DummyImplicit) = new Using(() => r)
} |
|
I have some thoughts about the
|
|
Thank you for your detailed reply! About the points metioned above:
|
OK. Since
|
|
Will modify this branch as soon as #255 was merged (>ω<) |
Merge from original master
|
Hi, because I'm gonna disappear for 4 days, here's just a quick update. I've modified code which contains plain function support for Using with several test cases. While Here is a test case will fail due to this issue. I think this is because call-by-name param does not treated as domain. And this is caused by it's representation is a normal param in the AST not a function. I'm currently not sure is it doable/a good idea to let the compiler treat call-by-name param as a domain. I will try it when I'm back. |
Defer keyword works likes
deferin Golang.It runs the deferred statements in the reversed sequence when current function finished.