Conversation
endgame
left a comment
There was a problem hiding this comment.
Looks cool. There are a couple of operations we cannot do with the new functions:
- lift an
IO System.Handleto aRIO Handle - lift an action like
hClosetoRIOand consume the safeHandle.
Basically, do we want Handle-specific versions of unsafeAcquire and unsafeRelease?
tbagrel1
left a comment
There was a problem hiding this comment.
I left a few questions/suggestions, but the PR looks good to me!
| @@ -129,50 +140,34 @@ hClose :: Handle %1 -> RIO () | |||
| hClose (Handle h) = unsafeRelease h | |||
There was a problem hiding this comment.
two lines above (l. 137, but I couldn't comment there in the review pane), we use Control.pure, and a few lines before, we use Control.return. Also both openFile and openBinaryFile look very similar, maybe we could factor them as part of this handle functions overhaul?
There was a problem hiding this comment.
It's a little overkill. They are both pretty small functions.
The |
Yes, I can see it now - thanks.
If I'm not sure exactly what it would look like, but perhaps it's worth trying to think more generally than just RIO Handles - a programmer using
Should we provide a toolkit for these, maybe in terms of |
To elaborate on this point: I was hoping that we could do something like generalise
I can't think of a good way to lift the |
|
Meta: I am also a little thrown by the way that functions that return |
|
No, I probably just didn't pay attention. I'm ok to change it. But it's a breaking change, so it needs to be advertised. |
It's no different than the current situation: you need to do an |
|
Scanning over the discussions, there are a few threads here. I will summarise them all so that this can hopefully get merged:
|
I will not be making a bug for this - it would make |
Oh yes, it is the |
|
Thanks for the discussion, even if I took so much time to get back to it. After all this I came to the conclusion that I've been going at this all wrong. Namely it is not true that using Therefore, here is my work plan:
|
| data UnsafeResource a where | ||
| UnsafeResource :: Int -> a -> UnsafeResource a | ||
| data Resource a where | ||
| UnsafeResource :: Int -> a -> Resource a |
There was a problem hiding this comment.
If the constructor is not exported, does that mean it's safe to rename?
There was a problem hiding this comment.
It is safe to rename. But I like how the name emphasises that accessing this constructor is very unsafe and needs to be done with care. What would you like to see instead?
There was a problem hiding this comment.
I think that makes sense. Just checking.
| @@ -201,7 +202,7 @@ unsafeRelease (UnsafeResource key _) = RIO (\st -> Linear.mask_ (releaseWith key | |||
| unsafeAcquire :: | |||
There was a problem hiding this comment.
Is the docstring calling this unsafe still correct given your new posture towards resources?
There was a problem hiding this comment.
I consider it is an unsafe function in the usual sense in Haskell: it has conditions for application which can't be proved by the compiler, hence are the responsibility of the programmer.
Though I must admit that ResourceT has the same function and considers it safe. So there is certainly a question.
| -- | Specialised alias for 'release' | ||
| hClose :: Handle %1 -> RIO () | ||
| hClose (Handle h) = unsafeRelease h | ||
| hClose = release |
There was a problem hiding this comment.
Deprecate this? Not with a view to ever removing it, but programmers put hClose in their programs, then get told to use release, and then learn not to provide aliases in their own libraries?
There was a problem hiding this comment.
I don't know that I want to deprecate this. I would if I thought using hClose is less good than using release. But I'm not sure that it is, just it's ok that we have return despite it being a specialisation of pure.
| -- | Specialised alias for 'release' | ||
| hClose :: Handle %1 -> RIO () | ||
| hClose (Handle h) = unsafeRelease h | ||
| hClose = release |
There was a problem hiding this comment.
I don't know that I want to deprecate this. I would if I thought using hClose is less good than using release. But I'm not sure that it is, just it's ok that we have return despite it being a specialisation of pure.
| @@ -201,7 +202,7 @@ unsafeRelease (UnsafeResource key _) = RIO (\st -> Linear.mask_ (releaseWith key | |||
| unsafeAcquire :: | |||
There was a problem hiding this comment.
I consider it is an unsafe function in the usual sense in Haskell: it has conditions for application which can't be proved by the compiler, hence are the responsibility of the programmer.
Though I must admit that ResourceT has the same function and considers it safe. So there is certainly a question.
| data UnsafeResource a where | ||
| UnsafeResource :: Int -> a -> UnsafeResource a | ||
| data Resource a where | ||
| UnsafeResource :: Int -> a -> Resource a |
There was a problem hiding this comment.
It is safe to rename. But I like how the name emphasises that accessing this constructor is very unsafe and needs to be done with care. What would you like to see instead?
|
I have nothing further to add, and I don't know when I'll next try linear stuff in a project. Probably best to get this out as-is and see if further stuff emerges later. |
Having `Handle` be transparent lets you convert traditional IO functions on `Handle` into resource-aware function. It would use the unsafe API, of course. But it's better than the previous situation. My initial assumption that because every function on `UnsafeResource` (now rename `Resource`) was unsafe, then I need concrete resource types to be opaque was simply faulty. Closes #424.
9c1ff3a to
1e290fc
Compare
|
I've squashed the history as the intermediate commits were not relevant anymore. This will automerge when CI is green. I'll make a release in the coming days. |
Resource.unsafeFromSystemIOHandle lets you convert traditional IO
functions on
Handleinto resource-aware function.This also let me clean up the internal definition of handle-based
functions. I took the opportunity to remove auxiliary function and use
the multiplicity-polymorphic
flipthat the comments were asking for.Closes #424. Cc @endgame .