Pattern matching 'undefined'

John Meacham john@repetae.net
Mon, 26 May 2003 18:42:47 -0700


ah, well than what you probably want is a default value for your fields.
while not directly supported, it can easily be simulated as follows.

data Foo = Foo { fieldA :: Maybe String, fieldB :: Maybe Int, fieldC ::
Maybe String}

foo = Foo {fieldA = Nothing, fieldB = Nothing, fieldC = Nothing}

now what you can do is 

x = foo {fieldA = Just "hasFieldA"}
y = foo {fieldB = Just 34}

by using 'foo' rather than Foo you get your 'default' nothing values for
everything not specified.
        John

On Mon, May 26, 2003 at 11:50:27PM +0200, Alain Cremieux wrote:
> The problem is that there may be 50 fields (my example was too simple, 
> sorry). So I want precisely to avoid 'Just' and 'Nothing', because I 
> want to be able to say :
> graph = AttrGraphDot {color = Blue}
> without having to name the 49 other fields.
> Field labels seem really adequate to do this, and by having defined the 
> type Color with Red | Blue | ... there will be a compile-time check on 
> the correctness of the value. This would be impossible with an ordinary 
> implementation : [(String, String)]  which will be verified only at 
> run-time.
> But then I must be able to convert the field-labelled datatype to a list 
> [(String, String)], depending on which labels have been provided.
> This is not so easy, especially since it is not possible to 
> pattern-match against field labels, which are functions.

-- 
---------------------------------------------------------------------------
John Meacham - California Institute of Technology, Alum. - john@foo.net
---------------------------------------------------------------------------