[Pkg-haskell-commits] [SCM] haskell-testpack branch, master, updated. debian/1.0.2-1-4-gb0d6b36
John Goerzen
jgoerzen at complete.org
Fri Apr 23 14:48:48 UTC 2010
The following commit has been merged in the master branch:
commit ba4312e92531238ef20173970fe30419e319e672
Author: John Goerzen <jgoerzen at complete.org>
Date: Wed Dec 1 04:52:26 2004 +0100
Checkpointing
Keywords:
(jgoerzen at complete.org--projects/missingh--head--0.5--patch-133)
diff --git a/ChangeLog b/ChangeLog
index 1b061c4..d73b8f1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,6 +2,19 @@
# arch-tag: automatic-ChangeLog--jgoerzen at complete.org--projects/missingh--head--0.5
#
+2004-11-30 21:52:26 GMT John Goerzen <jgoerzen at complete.org> patch-133
+
+ Summary:
+ Checkpointing
+ Revision:
+ missingh--head--0.5--patch-133
+
+
+ modified files:
+ ChangeLog libsrc/MissingH/ConfigParser.hs
+ testsrc/ConfigParser/Maintest.hs
+
+
2004-11-30 20:57:23 GMT John Goerzen <jgoerzen at complete.org> patch-132
Summary:
diff --git a/libsrc/MissingH/ConfigParser.hs b/libsrc/MissingH/ConfigParser.hs
index 3e7e0be..c4e5455 100644
--- a/libsrc/MissingH/ConfigParser.hs
+++ b/libsrc/MissingH/ConfigParser.hs
@@ -62,6 +62,7 @@ module MissingH.ConfigParser
-- $usagenomonad
-- ** Error Monad Usage
+ -- $usageerrormonad
-- * Types
SectionSpec, OptionSpec, ConfigParser(..),
@@ -571,7 +572,55 @@ You can transform errors into exceptions in your code by using
In short, you can just put @forceEither $@ in front of every call that returns
a 'CPResult'. This is still a pure functional call, so it can be used outside
-of any monads.
+of the IO monads. The exception, however, can only be caught in the IO
+monad.
+
+If you don't want to bother with 'forceEither', you can use the error monad. It's simple and better... read on.
+-}
+
+{- $usageerrormonad
+
+The 'CPResult' type is actually defined in terms of the Error monad, which is
+itself based on the Either data type.
+
+Here's a neat example of chaining together calls to build up a 'ConfigParser'
+object:
+
+>do let cp = emptyCP
+> cp <- add_section cp "sect1"
+> cp <- set cp "sect1" "opt1" "foo"
+> cp <- set cp "sect1" "opt2" "bar"
+> options cp "sect1"
+
+The return value of this little snippet is @Right [\"opt1\", \"opt2\"]@.
+(Note to beginners: unlike the IO monad, you /can/ escape from the Error
+monad.)
+
+Although it's not obvious, there actually was error checking there. If
+any of those calls would have generated an error, processing would have
+stopped immediately and a @Left@ value would have been returned. Consider
+this example:
+
+>do let cp = emptyCP
+> cp <- add_section cp "sect1"
+> cp <- set cp "sect1" "opt1" "foo"
+> cp <- set cp "sect2" "opt2" "bar"
+> options cp "sect1"
+
+The return value from this is @Left ('NoSection' \"sect2\", \"set\")@. The
+second call to 'set' failed, so the final call was skipped, and the result
+of the entire computation was considered to be an error.
+
+You can combine this with the non-monadic style to get a final, pure value
+out of it:
+
+>forceEither $ do let cp = emptyCP
+> cp <- add_section cp "sect1"
+> cp <- set cp "sect1" "opt1" "foo"
+> cp <- set cp "sect1" "opt2" "bar"
+> options cp "sect1"
+
+This returns ["opt1", "opt2"]. A quite normal value.
-}
diff --git a/testsrc/ConfigParser/Maintest.hs b/testsrc/ConfigParser/Maintest.hs
index 634112d..655da4a 100644
--- a/testsrc/ConfigParser/Maintest.hs
+++ b/testsrc/ConfigParser/Maintest.hs
@@ -109,10 +109,35 @@ test_ex_nomonad =
hPutStr fh "Your setting is:"
hPutStr fh $ forceEither $ get cp "file1" "location"
-
+test_ex_errormonad =
+ [
+ TestLabel "chaining1" $ TestCase $
+ (Right ["opt1", "opt2"]) @=?
+ do let cp = emptyCP
+ cp <- add_section cp "sect1"
+ cp <- set cp "sect1" "opt1" "foo"
+ cp <- set cp "sect1" "opt2" "bar"
+ options cp "sect1"
+ ,TestLabel "chaining2" $ TestCase $
+ (Left (NoSection "sect2", "set")) @=?
+ do let cp = emptyCP
+ cp <- add_section cp "sect1"
+ cp <- set cp "sect1" "opt1" "foo"
+ cp <- set cp "sect2" "opt2" "bar"
+ options cp "sect1"
+ ,TestLabel "chaining3" $ TestCase $
+ ["opt1", "opt2"] @=? (
+ forceEither $ do let cp = emptyCP
+ cp <- add_section cp "sect1"
+ cp <- set cp "sect1" "opt1" "foo"
+ cp <- set cp "sect1" "opt2" "bar"
+ options cp "sect1"
+ )
+ ]
tests = TestList [TestLabel "test_basic" (TestList test_basic),
TestLabel "test_defaults" (TestList test_defaults),
TestLabel "test_nodefault" (TestList test_nodefault),
- TestLabel "test_ex_nomonad" (TestCase test_ex_nomonad)]
+ TestLabel "test_ex_nomonad" (TestCase test_ex_nomonad),
+ TestLabel "test_ex_errormonad" (TestList test_ex_errormonad)]
--
haskell-testpack
More information about the Pkg-haskell-commits
mailing list