-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Make Using lazy #7415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make Using lazy #7415
Conversation
|
👨🎨 Maybe scala-arm's |
src/library/scala/util/Using.scala
Outdated
| val res = op(r) | ||
| if (p(res)) res | ||
| else throw new NoSuchElementException("Predicate does not hold for " + res) | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not really related to this PR, but is it a good idea to have filter if it throws an exception? I think we should not have it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems relatively related to this PR to me.
The question is, if not NoSuchElementException, what does the eventual Try contain?
(Side note: I tried out an optimisation where it uses a cached ControlThrowable initially, and then only turns it into a NoSuchElementException if you ask for a Try back. If you call foreach, it just catches the cached throwable and does nothing. Other than being a bit confusing from a maintenance perspective, I didn't see any downsides.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The question is, if not
NoSuchElementException, what does the eventualTrycontain?
Any exception that can be thrown here:
scala/src/library/scala/util/Using.scala
Lines 181 to 217 in 56c45cd
| def resource[R: Resource, A](resource: R)(body: R => A): A = { | |
| if (resource == null) throw new NullPointerException("null resource") | |
| @inline def safeAddSuppressed(t: Throwable, suppressed: Throwable): Unit = { | |
| // don't `addSuppressed` to something which is a `ControlThrowable` | |
| if (!t.isInstanceOf[ControlThrowable]) t.addSuppressed(suppressed) | |
| } | |
| var primary: Throwable = null | |
| try { | |
| body(resource) | |
| } catch { | |
| case t: Throwable => | |
| primary = t | |
| null.asInstanceOf[A] // compiler doesn't know `finally` will throw | |
| } finally { | |
| if (primary eq null) implicitly[Resource[R]].release(resource) | |
| else { | |
| var toThrow = primary | |
| try { | |
| implicitly[Resource[R]].release(resource) | |
| } catch { | |
| case other: Throwable => | |
| if (NonFatal(primary) && !NonFatal(other)) { | |
| // `other` is fatal, `primary` is not | |
| toThrow = other | |
| safeAddSuppressed(other, primary) | |
| } else { | |
| // `toThrow` is already `primary` | |
| safeAddSuppressed(primary, other) | |
| } | |
| } finally { | |
| throw toThrow | |
| } | |
| } | |
| } | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine with removing filter. If we decide it's a good idea later, it can always be added back.
|
@NthPortal useAndRelease? 使用 FastHub 从我的 Galaxy Note8 发送 |
25ab5f9 to
5800501
Compare
Make Using lazy, allowing `map` and `filter` to be called before the resource is released.
c912b95 to
5bd2aae
Compare
|
While we're repainting the shed, thoughts on renaming |
|
If you called it
|
|
I always prefer the loan pattern to programmer-implements-linear-types-in-their-head. So I'm not a fan. (Loan pattern would look like You can screw this up too by assigning intermediates to some mutable value, but it isn't nearly so error-prone for people who aren't used to lazy evaluation. |
|
@Ichoran in your example, all the resources get released at the I'm not opposed to that, though I'm curious as to others' thoughts on whether this is slowly exceeding the scope of what should be in the standard library. Originally, this was just meant to be a simple |
|
@Ichoran once it's managed externally, is there any reason for |
|
@NthPortal - Without coding it up I can't be sure, but I don't think the monadish thing is needed any longer. |
|
Superseded by #7468 |
Make Using lazy, allowing
mapandfilterto becalled before the resource is released.