Record Puns/Wildcards

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Record Puns/Wildcards

Ben Franksen
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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/glasgow-haskell-users
Reply | Threaded
Open this post in threaded view
|

Re: Record Puns/Wildcards

Adam Vogt
Hi Ben,

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

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. If you don't mind naming the constructor
twice, you can do an "update" with:

myC = case defC of
        C { .. } -> C { .. }
          where a = 2
-- myC == C 2 2 3

-- where we have for example
data CType = C { a, b, c :: Int } deriving Show
defC = C 1 2 3

If CType had multiple constructors, I think you'd be better off doing
the update with NamedFieldPuns syntax (defC { a }), or ordinary update
syntax (defC { a = 2 }).

Regards,
Adam


Regards,
Adam

On Tue, Feb 24, 2015 at 9:05 PM, Ben Franksen <[hidden email]> 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
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/glasgow-haskell-users
_______________________________________________
Glasgow-haskell-users mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/glasgow-haskell-users
Reply | Threaded
Open this post in threaded view
|

Re: Record Puns/Wildcards

Ben Franksen
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 <[hidden email]>
> 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
>> [hidden email]
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/glasgow-haskell-users
> _______________________________________________
> Glasgow-haskell-users mailing list
> [hidden email]
> 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


_______________________________________________
Glasgow-haskell-users mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/glasgow-haskell-users