-
Notifications
You must be signed in to change notification settings - Fork 19
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
Explore using ST for effects (issue #24) #33
Conversation
There are two instances of the typeclass supplied: one for Effect + EffectBuffer and one for ST + STBuffer. This makes it possible to write code that mutates buffers that can run in either Effect or ST. The functional dependencies on MutableBuffer go in both directions so that the type-checker only needs to know _either_ the type of the buffer or the type of the effect: this should make it easier to write generic code without type assertions, but it does mean that both typeclass instances must go in the same module (Node.Buffer.Mutable) in order to avoid orphaned instances.
I haven’t looked properly yet but having just read the PR description this seems like it’s worth pursuing. 👍 |
Fixes #24 |
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 like the approach, but I'd quite like to make this non-breaking, and in particular I'd like the Buffer
type to retain its current meaning - a mutable Buffer
which can be mutated in the Effect
monad. I wonder if we can make it non-breaking as follows:
- Have
Buffer
retain its current meaning (i.e. the type which in this PR is currently calledEffectBuffer
) - Introduce a new type
ImmutableBuffer
for immutable buffers (I think immutable buffers are unusual, generally the point of them is for use in imperative-style code which uses mutation, so it makes sense to have the names reflect that, right?) - Re-export the methods of the
MutableBuffer
class fromNode.Buffer
, keeping the API as close as possible to the current API.
Perhaps it would also make sense to put the class on its own in a module Node.Buffer.Class
, put the unsafe method implementations in a Node.Buffer.Internal
module, and put the concrete mutable buffer types and instances in Node.Buffer
and Node.Buffer.ST
modules respectively.
As requested in #discussion_r244534678
As requested in #discussion_r244536851
As requested in #discussion_r244536918
Mutable buffers are now the default again, and immutable buffers are moved to Node.Buffer.Immutable
I have now shuffled things around to try and keep the API is similar as possible to before - I hope this satisfies your requests.
I have experimented with this in a separate branch: https://github.com/Dretch/purescript-node-buffer/tree/stbuffer-typeclass-modularised There is one thing I am not sure is okay with this approach: because the instances of
Instead we must have:
This means that users of this module will have to add type assertions near methods of |
Thanks! I think I actually prefer it without the |
Yup, I understand the I have now merged the EDIT: Actually there is one more thing to mention. Right now the docs for |
I'm tempted to say leave it, because otherwise moving the docs back again will be a faff once that issue is resolved. |
Does anyone else have comments on this? This is looking like it's basically ready for merging to me now. Note that this is technically a breaking change - as @Dretch pointed out - because we've generalised most of the functions exported by |
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.
Thanks very much; I think this looks great.
error after addition of MutableBuffer class purescript-node/purescript-node-buffer#33 ``` No type class instance was found for Node.Buffer.Class.MutableBuffer t2 Effect The instance head contains unknown type variables. Consider adding a type annotation. while applying a function fromString of type MutableBuffer t0 t1 => String -> Encoding -> t1 t0 to argument "Not Found" while inferring the type of fromString "Not Found" in value declaration serveFilesAndGet ```
error after addition of MutableBuffer class purescript-node/purescript-node-buffer#33 ``` No type class instance was found for Node.Buffer.Class.MutableBuffer t2 Effect The instance head contains unknown type variables. Consider adding a type annotation. while applying a function fromString of type MutableBuffer t0 t1 => String -> Encoding -> t1 t0 to argument "Not Found" while inferring the type of fromString "Not Found" in value declaration serveFilesAndGet ```
I have been exploring how this library could look if it allowed using
ST
for effects in addition toEffect
.The changes I suggest here break backwards compatibility somewhat, in order to provide an API that I think makes more sense. Function names have been kept the same but the types and module arrangements have changed.
In short, the modules I propose to have in this library are:
Node.Buffer
: Immutable buffers and functions that operate on them. This is something that the current library does not have, but I think it makes sense in the same way thatSTArray
hasArray
.Node.Buffer.Mutable
: Contains anEffectBuffer
type, aSTBuffer h
type and aMutableBuffer b e
typeclass with all the read and write operations, and instances ofMutableBuffer EffectBuffer Effect
andMutableBuffer (STBuffer h) (ST h)
. The typeclass allows you to write code that can operate in theEffect
or theST
effect, rather like howMArray
works in Haskell.Node.Buffer.Mutable.Unsafe
: Unsafe operations on mutable buffers - using aMutableBufferUnsafe b e
typeclass.Node.Buffer.Encoding
- UnchangedI realise this is quite a big unsolicited change, so I don't expect it to be merged... but maybe I can provoke some discussion: what do you think?