Data.Fixed.Fixed constructor

David Menendez dave at zednenem.com
Fri Jul 24 00:58:43 EDT 2009


On Thu, Jul 23, 2009 at 11:21 PM, Ashley Yakeley<ashley at semantic.org> wrote:
> David Menendez wrote:
>
>> instance Data Double where
>>  toConstr = mkFloatConstr floatType   -- that should probably be
>> "doubleType"
>>  gunfold _ z c = case constrRep c of
>>                    (FloatConstr x) -> z x
>>                    _ -> error "gunfold"
>>  dataTypeOf _ = doubleType
>
> I could do something like this for Fixed, but deriving Fixed automatically
> does essentially the same thing.

It depends on how abstract you want Fixed to be. If you use the
derived Data instances, then gshow and gread (from Data.Generics.Text)
expose the implementation.

*Fixed Data.Generics.Text> gshow (4::Micro)
"(MkFixed (4000000))"
*Fixed Data.Generics.Text> gread "(MkFixed (4))" :: [(Micro,String)]
[(0.000004,"")]
*Fixed Data.Generics.Text> gread "(MkFixed (4))" :: [(Pico,String)]
[(0.000000000004,"")]


In contrast, the StringRep version I posted elsewhere works fine with
gshow/gread while still hiding the implementation.

*Main Data.Generics.Text> gshow (4::Micro)
"(4.000000)"
*Main Data.Generics.Text> gread "(4)" :: [(Micro,String)]
[(4.000000,"")]
*Main Data.Generics.Text> gread "(4)" :: [(Pico,String)]
[(4.000000000000,"")]


-- 
Dave Menendez <dave at zednenem.com>
<http://www.eyrie.org/~zednenem/>


More information about the Libraries mailing list