Use Data.Default

Ilya Portnov [2010-04-21 18:55:56]
Use Data.Default
Filename
YAML.hs
YAMLInstances.hs
diff --git a/YAML.hs b/YAML.hs
index 1ab48cc..38ebab6 100644
--- a/YAML.hs
+++ b/YAML.hs
@@ -4,13 +4,13 @@ module YAML where

 import Control.Monad
 import Data.Maybe
--- import Data.Convertible
+import Data.Default
 import Data.Object
 import Data.Object.Yaml
 import qualified Data.ByteString.Char8 as BS
 import Text.Libyaml hiding (encode, decode)

-class (ConvertSuccess YamlObject a, ConvertSuccess a YamlObject) => IsYamlObject a where
+class (ConvertSuccess YamlObject a, ConvertSuccess a YamlObject, Default a) => IsYamlObject a where

 getAttr :: BS.ByteString -> YamlObject -> Maybe YamlObject
 getAttr key (Mapping pairs) = lookup (toYamlScalar key) pairs
diff --git a/YAMLInstances.hs b/YAMLInstances.hs
index 650ea32..745fc39 100644
--- a/YAMLInstances.hs
+++ b/YAMLInstances.hs
@@ -3,6 +3,7 @@
 module YAMLInstances where

 import Data.Maybe
+import Data.Default
 import Data.Object
 import Data.Object.Yaml
 import qualified Data.ByteString.Char8 as BS
@@ -38,8 +39,14 @@ instance ConvertSuccess YamlObject Point where
       x = fromMaybe 0 $ getScalarAttr "x" obj
       y = fromMaybe 0 $ getScalarAttr "y" obj

+instance Default Point where
+  def = Point 0 0
+
 instance IsYamlObject Point where

+instance Default YamlObject where
+  def = Sequence []
+
 instance IsYamlObject YamlObject where

 data Call = Call { methodName :: BS.ByteString, args :: YamlObject }
@@ -61,6 +68,9 @@ instance ConvertSuccess YamlObject Call where
       name = fromMaybe "defaultMethod" $ getScalarAttr "call" obj
       args = fromMaybe (Sequence []) $ getAttr "args" obj

+instance Default Call where
+  def = Call "defaultMethod" def
+
 instance IsYamlObject Call where

 yamlMethod :: (IsYamlObject a, IsYamlObject b) => (a -> IO b) -> YamlObject -> IO YamlObject
ViewGit