[Haskell-cafe] basic field questions

jamin1001 jamin1001 at yahoo.com
Thu Jan 25 05:22:56 EST 2007


Well, it seems this approach doesn't allow you to group some fields together
like "colour" and "weight", but instead you need to relist them piecemeal
for each new data constructor.  Also, you get a run-time error (rather than
compile-time) if you happen to reference a field that didn't happen to exist
in another data constructor, say like:


sq = squishiness $ Table {colour = Black, weight=1, height= 2}
main = putStr $ show sq

main: No match in record selector Main.squishiness


-Jamin



Jon Fairbairn wrote:
> 
> Ketil Malde <Ketil.Malde at bccs.uib.no> writes:
> 
>> jamin1001 wrote:
>> > What if I want to do something like data Chair = Chair
>> > {pos:: Int, color :: Int} data Table = Table {pos:: Int,
>> > color :: Int}
>> data Properties = Props { pos, color :: Int }
>> data Chair = Chair Props
>> data Table = Table Props
>> 
>> or:
>> 
>> data Chair = Chair Int Int
>> data Table = Table Int Int
>> 
>> class Furniture a where
>>    pos :: a -> Int
>>    color :: a -> Int
>> 
>> instance Furniture Chair where
>>    pos (Chair x _) = x
>>    color (Chair _ c) = c
> 
> 
> I'm not sure I follow what's really wanted here. Whats wrong
> with this:
> 
>> data Colour = Black | Brown deriving (Enum, Show)
>> data Furniture = Table {colour:: Colour, weight:: Double, height::
>> Double}
>>                | Chair {colour:: Colour, weight:: Double, squishiness::
>> Double}
>>                  deriving Show
> 
> and then...
> 
>       ___         ___ _
>      / _ \ /\  /\/ __(_)
>     / /_\// /_/ / /  | |      GHC Interactive, version 6.4.2, for Haskell
> 98.
>    / /_\\/ __  / /___| |      http://www.haskell.org/ghc/
>    \____/\/ /_/\____/|_|      Type :? for help.
> 
>    Loading package base-1.0 ... linking ... done.
>    Prelude> :l /tmp/foo.lhs
>    Compiling Foo              ( /tmp/foo.lhs, interpreted )
>    Ok, modules loaded: Foo.
>    *Foo> colour $ Table {colour = Black, weight=1, height= 2}
>    Black
>    *Foo> colour $ Chair {colour = Brown, weight = 1, squishiness=100}
>    Brown
>    *Foo>
> 
> 
> The requirement is that all the "colour" fields have the
> same type.
> 
>> The record system is somewhat wartish,
> 
> Sure, but it's not obvious to me that we're looking at one
> of the warts here, unless the problem is that my "Furniture"
> above isn't extensible in the appropriate sense or someone
> wants the different coulour fields to have different types
> (confusing, surely?).
> 
> -- 
> Jón Fairbairn                                 Jon.Fairbairn at cl.cam.ac.uk
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
> 
> 

-- 
View this message in context: http://www.nabble.com/basic-field-questions-tf3080392.html#a8599440
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.



More information about the Haskell-Cafe mailing list