[Haskell-cafe] Record initialise question (RESOLVED)

John Ky newhoggy at gmail.com
Sat Jun 6 02:07:20 EDT 2009


On Fri, Jun 5, 2009 at 8:05 PM, Martijn van Steenbergen <
martijn at van.steenbergen.nl> wrote:

> Hi John,
>
> John Ky wrote:
>
>>  >  full = do
>>  >     let myOrder = init -- [1]
>>  >              { item = Just init
>>  >                 { itemId = "Something"
>>  >                 }
>>  >              , operation = Just Buy
>>  >              }
>>  >     putStrLn $ show myOrder
>>  >     return ()
>>
>> Where initOrder and initItem are both replaced with just 'init' and the
>> compiler works out from the context (ie. the type of the field) what the
>> types are supposed to be and therefore the actual init function to call?
>>
>
> Sure! This is one of the simplest forms of overloading. Here's a small
> example:
>
>  data Circle = Circle { center :: (Float, Float), radius :: Float }
>> data WrapCircle = WrapCircle { circle :: Circle }
>>
>> class Ini a where
>>  ini :: a
>>
>> instance Ini Circle where
>>  ini = Circle { center = (0, 0), radius = 1 }
>>
>> exampleCircle :: WrapCircle
>> exampleCircle = WrapCircle { circle = ini { radius = 2 } }
>>
>
> Hope this helps!
>
> Martijn.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090606/b6d94d3c/attachment.html


More information about the Haskell-Cafe mailing list