What are the difference in usage between Either and Except in Haskell?











up vote
4
down vote

favorite
2












I have a class that can be created from several arguments in Haskell which requires some complex validation of those arguments. Currently I have something like



makeAThingExcept :: String -> String -> ... String -> Except ThingError AThing
makeAThingExcept s1 s2 ... = do
unless (s1CheckPasses s1) (throwError (BadS1 s1))
...

data ThingError = BadS1 String ...

instance Show ThingError where
show (BadS1 s) = "Bad S1: " ++ s

makeAThing :: String -> String -> ... String -> AThing
makeAThing s1 s2 ... = case runExcept (makeAThingExcept s1 s2 ...) of
Right thing -> thing
Left err -> error (show err)


Setting aside whether there is a better way to do this by using more specific types than String as arguments to makeAThingExcept, is there a reason to prefer Except over Either in a case like this? What are the differences here between the capabilities and idiom of Except vs Either?










share|improve this question


















  • 2




    It probably comes down what type class instances are defined for Except; you can convert between Except and Either using except and runExcept.
    – chepner
    Nov 7 at 14:49










  • @chepner Ah, so there's some fluidity there, wrt. tuning Except vs Either depending on context.
    – orome
    Nov 7 at 14:57










  • Here in my package, FWIW, getting a valid AThing is really essential, and entering valid args can be a bit tricky. So failure from user input (e.g. from a typo) can be common; but in code, something would have had to go quite wrong with whatever was generating the arguments: code that supplied bad arguments would be doing a bad job of making AThings.
    – orome
    Nov 7 at 14:58










  • So, naively, the former case is like an "either" or even a "maybe" (it could happen fix it and try again), while the latter is closer to an "exception" (your code isn't specifying AThings correctly).
    – orome
    Nov 7 at 14:58






  • 1




    You'll have to wait for someone who actually knows to provide an answer :) I think the only real difference is that the name ExceptT was introduced to provide a type that was semantically devoted to error handling, as opposed to Either which is the generic way to define sum types, only one application of which is to provide error-handling semantics.
    – chepner
    Nov 7 at 15:04















up vote
4
down vote

favorite
2












I have a class that can be created from several arguments in Haskell which requires some complex validation of those arguments. Currently I have something like



makeAThingExcept :: String -> String -> ... String -> Except ThingError AThing
makeAThingExcept s1 s2 ... = do
unless (s1CheckPasses s1) (throwError (BadS1 s1))
...

data ThingError = BadS1 String ...

instance Show ThingError where
show (BadS1 s) = "Bad S1: " ++ s

makeAThing :: String -> String -> ... String -> AThing
makeAThing s1 s2 ... = case runExcept (makeAThingExcept s1 s2 ...) of
Right thing -> thing
Left err -> error (show err)


Setting aside whether there is a better way to do this by using more specific types than String as arguments to makeAThingExcept, is there a reason to prefer Except over Either in a case like this? What are the differences here between the capabilities and idiom of Except vs Either?










share|improve this question


















  • 2




    It probably comes down what type class instances are defined for Except; you can convert between Except and Either using except and runExcept.
    – chepner
    Nov 7 at 14:49










  • @chepner Ah, so there's some fluidity there, wrt. tuning Except vs Either depending on context.
    – orome
    Nov 7 at 14:57










  • Here in my package, FWIW, getting a valid AThing is really essential, and entering valid args can be a bit tricky. So failure from user input (e.g. from a typo) can be common; but in code, something would have had to go quite wrong with whatever was generating the arguments: code that supplied bad arguments would be doing a bad job of making AThings.
    – orome
    Nov 7 at 14:58










  • So, naively, the former case is like an "either" or even a "maybe" (it could happen fix it and try again), while the latter is closer to an "exception" (your code isn't specifying AThings correctly).
    – orome
    Nov 7 at 14:58






  • 1




    You'll have to wait for someone who actually knows to provide an answer :) I think the only real difference is that the name ExceptT was introduced to provide a type that was semantically devoted to error handling, as opposed to Either which is the generic way to define sum types, only one application of which is to provide error-handling semantics.
    – chepner
    Nov 7 at 15:04













up vote
4
down vote

favorite
2









up vote
4
down vote

favorite
2






2





I have a class that can be created from several arguments in Haskell which requires some complex validation of those arguments. Currently I have something like



makeAThingExcept :: String -> String -> ... String -> Except ThingError AThing
makeAThingExcept s1 s2 ... = do
unless (s1CheckPasses s1) (throwError (BadS1 s1))
...

data ThingError = BadS1 String ...

instance Show ThingError where
show (BadS1 s) = "Bad S1: " ++ s

makeAThing :: String -> String -> ... String -> AThing
makeAThing s1 s2 ... = case runExcept (makeAThingExcept s1 s2 ...) of
Right thing -> thing
Left err -> error (show err)


Setting aside whether there is a better way to do this by using more specific types than String as arguments to makeAThingExcept, is there a reason to prefer Except over Either in a case like this? What are the differences here between the capabilities and idiom of Except vs Either?










share|improve this question













I have a class that can be created from several arguments in Haskell which requires some complex validation of those arguments. Currently I have something like



makeAThingExcept :: String -> String -> ... String -> Except ThingError AThing
makeAThingExcept s1 s2 ... = do
unless (s1CheckPasses s1) (throwError (BadS1 s1))
...

data ThingError = BadS1 String ...

instance Show ThingError where
show (BadS1 s) = "Bad S1: " ++ s

makeAThing :: String -> String -> ... String -> AThing
makeAThing s1 s2 ... = case runExcept (makeAThingExcept s1 s2 ...) of
Right thing -> thing
Left err -> error (show err)


Setting aside whether there is a better way to do this by using more specific types than String as arguments to makeAThingExcept, is there a reason to prefer Except over Either in a case like this? What are the differences here between the capabilities and idiom of Except vs Either?







haskell error-handling exception-handling user-input






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 7 at 14:31









orome

13.5k22101233




13.5k22101233








  • 2




    It probably comes down what type class instances are defined for Except; you can convert between Except and Either using except and runExcept.
    – chepner
    Nov 7 at 14:49










  • @chepner Ah, so there's some fluidity there, wrt. tuning Except vs Either depending on context.
    – orome
    Nov 7 at 14:57










  • Here in my package, FWIW, getting a valid AThing is really essential, and entering valid args can be a bit tricky. So failure from user input (e.g. from a typo) can be common; but in code, something would have had to go quite wrong with whatever was generating the arguments: code that supplied bad arguments would be doing a bad job of making AThings.
    – orome
    Nov 7 at 14:58










  • So, naively, the former case is like an "either" or even a "maybe" (it could happen fix it and try again), while the latter is closer to an "exception" (your code isn't specifying AThings correctly).
    – orome
    Nov 7 at 14:58






  • 1




    You'll have to wait for someone who actually knows to provide an answer :) I think the only real difference is that the name ExceptT was introduced to provide a type that was semantically devoted to error handling, as opposed to Either which is the generic way to define sum types, only one application of which is to provide error-handling semantics.
    – chepner
    Nov 7 at 15:04














  • 2




    It probably comes down what type class instances are defined for Except; you can convert between Except and Either using except and runExcept.
    – chepner
    Nov 7 at 14:49










  • @chepner Ah, so there's some fluidity there, wrt. tuning Except vs Either depending on context.
    – orome
    Nov 7 at 14:57










  • Here in my package, FWIW, getting a valid AThing is really essential, and entering valid args can be a bit tricky. So failure from user input (e.g. from a typo) can be common; but in code, something would have had to go quite wrong with whatever was generating the arguments: code that supplied bad arguments would be doing a bad job of making AThings.
    – orome
    Nov 7 at 14:58










  • So, naively, the former case is like an "either" or even a "maybe" (it could happen fix it and try again), while the latter is closer to an "exception" (your code isn't specifying AThings correctly).
    – orome
    Nov 7 at 14:58






  • 1




    You'll have to wait for someone who actually knows to provide an answer :) I think the only real difference is that the name ExceptT was introduced to provide a type that was semantically devoted to error handling, as opposed to Either which is the generic way to define sum types, only one application of which is to provide error-handling semantics.
    – chepner
    Nov 7 at 15:04








2




2




It probably comes down what type class instances are defined for Except; you can convert between Except and Either using except and runExcept.
– chepner
Nov 7 at 14:49




It probably comes down what type class instances are defined for Except; you can convert between Except and Either using except and runExcept.
– chepner
Nov 7 at 14:49












@chepner Ah, so there's some fluidity there, wrt. tuning Except vs Either depending on context.
– orome
Nov 7 at 14:57




@chepner Ah, so there's some fluidity there, wrt. tuning Except vs Either depending on context.
– orome
Nov 7 at 14:57












Here in my package, FWIW, getting a valid AThing is really essential, and entering valid args can be a bit tricky. So failure from user input (e.g. from a typo) can be common; but in code, something would have had to go quite wrong with whatever was generating the arguments: code that supplied bad arguments would be doing a bad job of making AThings.
– orome
Nov 7 at 14:58




Here in my package, FWIW, getting a valid AThing is really essential, and entering valid args can be a bit tricky. So failure from user input (e.g. from a typo) can be common; but in code, something would have had to go quite wrong with whatever was generating the arguments: code that supplied bad arguments would be doing a bad job of making AThings.
– orome
Nov 7 at 14:58












So, naively, the former case is like an "either" or even a "maybe" (it could happen fix it and try again), while the latter is closer to an "exception" (your code isn't specifying AThings correctly).
– orome
Nov 7 at 14:58




So, naively, the former case is like an "either" or even a "maybe" (it could happen fix it and try again), while the latter is closer to an "exception" (your code isn't specifying AThings correctly).
– orome
Nov 7 at 14:58




1




1




You'll have to wait for someone who actually knows to provide an answer :) I think the only real difference is that the name ExceptT was introduced to provide a type that was semantically devoted to error handling, as opposed to Either which is the generic way to define sum types, only one application of which is to provide error-handling semantics.
– chepner
Nov 7 at 15:04




You'll have to wait for someone who actually knows to provide an answer :) I think the only real difference is that the name ExceptT was introduced to provide a type that was semantically devoted to error handling, as opposed to Either which is the generic way to define sum types, only one application of which is to provide error-handling semantics.
– chepner
Nov 7 at 15:04












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










As noted in the comments, it's easy to convert between Except & Either. The runtime representation is the same, even.



I would always choose to use Either. It's ubiquitous in libraries. I very rarely see Except.



Except is a special case of ExceptT, which you will see in libraries. If you find yourself writing a lot of functions with Reader SomeType (Either e a) or IO (Either e a) or Monad m => m (Either e a), then you might want to consider ExceptT. It's fine not to worry about it until then - Either is easier to use until it isn't.






share|improve this answer





















  • I have a follow up question.
    – orome
    4 hours ago











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53191510%2fwhat-are-the-difference-in-usage-between-either-and-except-in-haskell%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










As noted in the comments, it's easy to convert between Except & Either. The runtime representation is the same, even.



I would always choose to use Either. It's ubiquitous in libraries. I very rarely see Except.



Except is a special case of ExceptT, which you will see in libraries. If you find yourself writing a lot of functions with Reader SomeType (Either e a) or IO (Either e a) or Monad m => m (Either e a), then you might want to consider ExceptT. It's fine not to worry about it until then - Either is easier to use until it isn't.






share|improve this answer





















  • I have a follow up question.
    – orome
    4 hours ago















up vote
2
down vote



accepted










As noted in the comments, it's easy to convert between Except & Either. The runtime representation is the same, even.



I would always choose to use Either. It's ubiquitous in libraries. I very rarely see Except.



Except is a special case of ExceptT, which you will see in libraries. If you find yourself writing a lot of functions with Reader SomeType (Either e a) or IO (Either e a) or Monad m => m (Either e a), then you might want to consider ExceptT. It's fine not to worry about it until then - Either is easier to use until it isn't.






share|improve this answer





















  • I have a follow up question.
    – orome
    4 hours ago













up vote
2
down vote



accepted







up vote
2
down vote



accepted






As noted in the comments, it's easy to convert between Except & Either. The runtime representation is the same, even.



I would always choose to use Either. It's ubiquitous in libraries. I very rarely see Except.



Except is a special case of ExceptT, which you will see in libraries. If you find yourself writing a lot of functions with Reader SomeType (Either e a) or IO (Either e a) or Monad m => m (Either e a), then you might want to consider ExceptT. It's fine not to worry about it until then - Either is easier to use until it isn't.






share|improve this answer












As noted in the comments, it's easy to convert between Except & Either. The runtime representation is the same, even.



I would always choose to use Either. It's ubiquitous in libraries. I very rarely see Except.



Except is a special case of ExceptT, which you will see in libraries. If you find yourself writing a lot of functions with Reader SomeType (Either e a) or IO (Either e a) or Monad m => m (Either e a), then you might want to consider ExceptT. It's fine not to worry about it until then - Either is easier to use until it isn't.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 7 at 15:44









bergey

2,003412




2,003412












  • I have a follow up question.
    – orome
    4 hours ago


















  • I have a follow up question.
    – orome
    4 hours ago
















I have a follow up question.
– orome
4 hours ago




I have a follow up question.
– orome
4 hours ago


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53191510%2fwhat-are-the-difference-in-usage-between-either-and-except-in-haskell%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()