Error-containing Maybes

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Error-containing Maybes

Ivan Lazar Miljenovic
The Maybe type is often (almost always?) used to indicate that a
computation may fail; specifically, `Maybe a` typically indicates that
it may not be possible to always produce a value of type `a`.

However, there are times when the inverse of this is desirable
(usually within a monadic/side-effectful context): if Nothing is
returned then the computation was a success; otherwise, an error
message is returned.  Whilst this usage definitely fits the types, it
breaks the mould of how we think/use Maybe in most cases and may at
times cause confusion.  As such, it may be advisable (and I know I've
done this at least once) to define a `data Result e = Error e |
Success` data type to model this behaviour.

Is this a typical enough requirement that would make it worth adding
such a datatype to base (despite my usual aversion to piling more
things in there) or having a library up on Hackage just for this very
simplistic type?

(Another option would be to use `Either error ()`, which could be
lifted better into an ExceptT result, albeit with the additional
useless parameter/value in there.)

--
Ivan Lazar Miljenovic
[hidden email]
http://IvanMiljenovic.wordpress.com
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Error-containing Maybes

Robin Palotai
How about `type Result e = Either e ()` ?

2017-06-15 11:38 GMT+02:00 Ivan Lazar Miljenovic <[hidden email]>:
The Maybe type is often (almost always?) used to indicate that a
computation may fail; specifically, `Maybe a` typically indicates that
it may not be possible to always produce a value of type `a`.

However, there are times when the inverse of this is desirable
(usually within a monadic/side-effectful context): if Nothing is
returned then the computation was a success; otherwise, an error
message is returned.  Whilst this usage definitely fits the types, it
breaks the mould of how we think/use Maybe in most cases and may at
times cause confusion.  As such, it may be advisable (and I know I've
done this at least once) to define a `data Result e = Error e |
Success` data type to model this behaviour.

Is this a typical enough requirement that would make it worth adding
such a datatype to base (despite my usual aversion to piling more
things in there) or having a library up on Hackage just for this very
simplistic type?

(Another option would be to use `Either error ()`, which could be
lifted better into an ExceptT result, albeit with the additional
useless parameter/value in there.)

--
Ivan Lazar Miljenovic
[hidden email]
http://IvanMiljenovic.wordpress.com
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.


_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Error-containing Maybes

Vlatko Basic
In reply to this post by Ivan Lazar Miljenovic
Something similar to what EitherR does?

https://hackage.haskell.org/package/errors-2.2.0/docs/Data-EitherR.html



-------- Original Message  --------
Subject: [Haskell-cafe] Error-containing Maybes
From: Ivan Lazar Miljenovic <[hidden email]>
To: Haskell Cafe <[hidden email]>
Date: 15/06/17 11:38

> The Maybe type is often (almost always?) used to indicate that a
> computation may fail; specifically, `Maybe a` typically indicates that
> it may not be possible to always produce a value of type `a`.
>
> However, there are times when the inverse of this is desirable
> (usually within a monadic/side-effectful context): if Nothing is
> returned then the computation was a success; otherwise, an error
> message is returned.  Whilst this usage definitely fits the types, it
> breaks the mould of how we think/use Maybe in most cases and may at
> times cause confusion.  As such, it may be advisable (and I know I've
> done this at least once) to define a `data Result e = Error e |
> Success` data type to model this behaviour.
>
> Is this a typical enough requirement that would make it worth adding
> such a datatype to base (despite my usual aversion to piling more
> things in there) or having a library up on Hackage just for this very
> simplistic type?
>
> (Another option would be to use `Either error ()`, which could be
> lifted better into an ExceptT result, albeit with the additional
> useless parameter/value in there.)
>
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Error-containing Maybes

Ivan Lazar Miljenovic
In reply to this post by Robin Palotai
On 15 June 2017 at 20:21, Robin Palotai <[hidden email]> wrote:
> See https://hackage.haskell.org/package/errors-2.2.0/docs/Data-EitherR.html
> for somewhat related type.

Ahah, wasn't aware of that package.

> How about `type Result e = Either e ()` ?

I did mention that at the end of my email ;-)

>
> 2017-06-15 11:38 GMT+02:00 Ivan Lazar Miljenovic
> <[hidden email]>:
>>
>> The Maybe type is often (almost always?) used to indicate that a
>> computation may fail; specifically, `Maybe a` typically indicates that
>> it may not be possible to always produce a value of type `a`.
>>
>> However, there are times when the inverse of this is desirable
>> (usually within a monadic/side-effectful context): if Nothing is
>> returned then the computation was a success; otherwise, an error
>> message is returned.  Whilst this usage definitely fits the types, it
>> breaks the mould of how we think/use Maybe in most cases and may at
>> times cause confusion.  As such, it may be advisable (and I know I've
>> done this at least once) to define a `data Result e = Error e |
>> Success` data type to model this behaviour.
>>
>> Is this a typical enough requirement that would make it worth adding
>> such a datatype to base (despite my usual aversion to piling more
>> things in there) or having a library up on Hackage just for this very
>> simplistic type?
>>
>> (Another option would be to use `Either error ()`, which could be
>> lifted better into an ExceptT result, albeit with the additional
>> useless parameter/value in there.)
>>
>> --
>> Ivan Lazar Miljenovic
>> [hidden email]
>> http://IvanMiljenovic.wordpress.com
>> _______________________________________________
>> Haskell-Cafe mailing list
>> To (un)subscribe, modify options or view archives go to:
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>> Only members subscribed via the mailman list are allowed to post.
>
>



--
Ivan Lazar Miljenovic
[hidden email]
http://IvanMiljenovic.wordpress.com
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.