s-mage / err

Error handling for clojure/clojurescript based on cats library
Eclipse Public License 1.0
4 stars 1 forks source link

either-let not working as intended? #1

Open caleb opened 8 years ago

caleb commented 8 years ago

Hi,

I've been looking for a better way to do error handling in my apps and this library looks like the kind of thing I want.

I'm confused about either-let, and I don't know if I understand how it works:

  (either-let [a (ok 1)
               b (inc a)]
    a)
  => 2

Shouldn't the above snippet return 1 or (ok 1)?

If not, sorry for bugging you :)

-Caleb

s-mage commented 8 years ago

Hi!

Thank you for your feedback. Actually I don't know, It was more convenient to me to unwrap value if it's right when I wrote it but I wonder now if it was a good decision.

Actually I looked into source code, it uses alet from cats with a bit of sugar with default value. https://funcool.github.io/cats/latest/#alet

s-mage commented 8 years ago

Ah, sorry, I misunderstood you. Yes, I think it should, and it must be something with cats, because

err.core=> (alet [a (ok 1) b (inc a)] 42)
2
s-mage commented 8 years ago

And I see same error in documentation of err.

s-mage commented 8 years ago

@caleb so I'll check this out, thank you. Will reply in couple of hours or maybe days.

s-mage commented 8 years ago

@caleb I've read some docs and experimented a bit, this is where I've come so far:

err.core=> (alet [a (ok 1) b (ok 2)] a)
#<Right 1>
err.core=> (alet [a (ok 1) b (ok (inc a))] a)
#<Right 1>
err.core=> (alet [a (ok 1) b (ok (fail 'oops'))] a)
#<Right 1>
err.core=> (alet [a (ok 1) b (ok (fail 'oops'))] (+ a b))

ClassCastException cats.monad.either.Left cannot be cast to java.lang.Number  clojure.lang.Numbers.add (Numbers.java:128)
err.core=> (alet [a (ok 1) b (ok (inc a))] (+ a b))
#<Right 3>

So this works when all values in let are wrapped into (ok) or (fail). And now I think it's a good thing. I looked into my projects where I use the lib -- and I have no problems with it, because in real project you often use your own functions where results are wrapped already.

Probably it should signal if one of args doesn't have context, because it's seems very strange. And I've just asked developer of cats, here are his words:

at this moment it seems like inconsistent
and it seems like a bug :disappointed:
this additionally remember me that I should release a new version of cats ASAP
because master comes with many bugfixes
caleb commented 8 years ago

Thanks for looking into this and updating the read me!

I was also confused because if I remember correctly the either->> macro doesn't require all the values returned to be wrapped.

Thanks for clearing this up. I am updating my code to just always use (ok ...)

Sent from my iPhone

On May 31, 2016, at 3:46 AM, Sergey Smagin notifications@github.com wrote:

@caleb I've read some docs and experimented a bit, this is where I've come so far:

err.core=> (alet [a (ok 1) b (ok 2)] a)

<Right 1>

err.core=> (alet [a (ok 1) b (ok (inc a))] a)

<Right 1>

err.core=> (alet [a (ok 1) b (ok (fail 'oops'))] a)

<Right 1>

err.core=> (alet [a (ok 1) b (ok (fail 'oops'))](+ a b))

ClassCastException cats.monad.either.Left cannot be cast to java.lang.Number clojure.lang.Numbers.add (Numbers.java:128) err.core=> (alet [a (ok 1) b (ok (inc a))](+ a b))

<Right 3>

So this works when all values in let are wrapped into (ok) or (fail). And now I think it's a good thing. I looked into my projects where I use the lib -- and I have no problems with it, because in real project you often use your own functions where results are wrapped already.

Probably it should signal if one of args doesn't have context, because it's seems very strange. And I've just asked developer of cats, here are his words:

at this moment it seems like inconsistent and it seems like a bug :disappointed: this additionally remember me that I should release a new version of cats ASAP because master comes with many bugfixes — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.