Record Puns/Wildcards

Ben Franksen ben.franksen at online.de
Wed Feb 25 19:58:03 UTC 2015


adam vogt wrote:
> With ghc-7.8.4 I get a different error "Empty record update of:
> default_config" when using a wildcard to update a record.

Yes. I remembered it wrong, this is the error I get, too (with ghc-7.6.3).

> I think you can't use wildcards in record updates because it's harder
> (for users and for ghc) to figure out which fields are involved when
> you don't name a constructor. 

Thanks, I think I see the problem now. If there was more than one 
constructor it could have completely different fields. So, in case of 
multiple constructors this would be a bad idea.

We could make an exception for data types with a single constructor, but 
perhaps it doesn't make sense to provide extra sugar for this special case.

> If you don't mind naming the constructor
> twice, you can do an "update" with:
> 
> myC = case defC of
>         C { .. } -> C { .. }
>           where a = 2

Nice trick, but I actually do mind the duplication ;-)

The reason I asked about this is that I'd like to have a simple way to embed 
user configuration, preferably without braces and commas and without having 
to define each possible configuration value (i.e. only those fields that 
differ from the default). Like xmonad, yi, etc, but simpler and more 
accessible to non-Haskell-programmers.

Really off-topic but on a related note, I never understood why the named 
field syntax in Haskell does not use semicolon as a separator (instead of 
comma) and allow indentation to replace braces and separators, as in

data X = X
  field1, field2 :: String
  field3 :: Int

and similar for lists and tuples.

Cheers
Ben
> On Tue, Feb 24, 2015 at 9:05 PM, Ben Franksen <ben.franksen at online.de>
> wrote:
>> I just noted that code like
>>
>> my_config = default_config {..} where
>>   name = "my project"
>>   description = "some longer text"
>>
>> gives me a syntax error, even if I have NamedFieldPuns and
>> RecordWildCards extensions enabled. It seems that these extensions only
>> work for record constructors and not for updating values.
>>
>> Is there a special reason puns/wildcards are not allowed in record
>> updates?
>>
>> Cheers
>> Ben
>> --
>> "There are two ways of constructing a software design: One way is to
>> make it so simple that there are obviously no deficiencies and the other
>> way is to make it so complicated that there are no obvious deficiencies.
>> The first method is far more difficult."   ― C.A.R. Hoare
>>
>>
>> _______________________________________________
>> Glasgow-haskell-users mailing list
>> Glasgow-haskell-users at haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/glasgow-haskell-users
> _______________________________________________
> Glasgow-haskell-users mailing list
> Glasgow-haskell-users at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/glasgow-haskell-users
-- 
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies and the other
way is to make it so complicated that there are no obvious deficiencies.
The first method is far more difficult."   ― C.A.R. Hoare




More information about the Glasgow-haskell-users mailing list