RE: [Haskell] sugar for extensible types (was: class associated types, via GADTs.)
On 16 February 2005 08:08, Ralf Laemmel wrote:
Assoc types and GADTs are great but I am still too fond of encoding extensible datatypes with (open) classes. I contributed to some related discussion at comp.compilers a while ago [1]. The Haskell code samples are worth sharing (because they are sooooo simple):
http://homepages.cwi.nl/~ralf/OOHaskell/src/interpreter/nonextensible.hs
http://homepages.cwi.nl/~ralf/OOHaskell/src/interpreter/extensible.hs (Note: *no* OOHaskell idioms used.)
I wish we'd done exceptions like this. An extensible exception type is entirely possible, and using it is not hard either - see the attached file implemented in terms of the existing Control.Exception. For example: test = do x `catch` (\(IOException e) -> print e) `catch` (\(ArithException e) -> print e) and I make a new type into an exception like this: data T = ... deriving (Typeable, Show) instance Exception T this can replace DynExceptions and the strange catchJust/tryJust stuff in Control.Exception. Perhaps we can figure out a reasonable migration path that wouldn't break too much code in one go? Cheers, Simon
On Wed, Feb 16, 2005 at 12:38:45PM -0000, Simon Marlow wrote:
test = do x `catch` (\(IOException e) -> print e) `catch` (\(ArithException e) -> print e)
Although slightly off-topic, and though you probably already realized it, beware that this is comparable to try { try { x } catch (IOException e) { print e } } catch (ArithException e) { print e } instead of the usual try { x } catch (IOException e) { print e } catch (ArithException e) { print e } which wasn't immediately obvious to me at first. Groeten, Remi -- Nobody can be exactly like me. Even I have trouble doing it.
On Wed, Feb 16, 2005 at 12:38:45PM -0000, Simon Marlow wrote:
On 16 February 2005 08:08, Ralf Laemmel wrote:
Assoc types and GADTs are great but I am still too fond of encoding extensible datatypes with (open) classes. I contributed to some related discussion at comp.compilers a while ago [1]. The Haskell code samples are worth sharing (because they are sooooo simple):
http://homepages.cwi.nl/~ralf/OOHaskell/src/interpreter/nonextensible.hs
http://homepages.cwi.nl/~ralf/OOHaskell/src/interpreter/extensible.hs (Note: *no* OOHaskell idioms used.)
I wish we'd done exceptions like this. An extensible exception type is entirely possible, and using it is not hard either - see the attached file implemented in terms of the existing Control.Exception. For example:
test = do x `catch` (\(IOException e) -> print e) `catch` (\(ArithException e) -> print e)
and I make a new type into an exception like this:
data T = ... deriving (Typeable, Show) instance Exception T
this can replace DynExceptions and the strange catchJust/tryJust stuff in Control.Exception. Perhaps we can figure out a reasonable migration path that wouldn't break too much code in one go?
This would be great. How about including this interface in a new module, perhaps Data.ExtensibleExceptions (or something shorter?) then new code can use this better interface and eventually the old interface can be depreciated. John -- John Meacham - ⑆repetae.net⑆john⑈
participants (3)
-
John Meacham -
Remi Turk -
Simon Marlow