[Haskell-cafe] type inference and named fields

Malcolm Wallace Malcolm.Wallace at cs.york.ac.uk
Fri Jun 24 06:06:19 EDT 2005


Christian Maeder <maeder at tzi.de> writes:

> >>voidcast :: Fields a -> Fields Void
> >>voidcast v@(VariantWithTwo{}) = v { field1 = Void , field2 = Void }
> >>voidcast v@(VariantWithOne{}) = v { field1 = Void }
> 
> I would not expect that updating only field1 can change the type of v.
> The right thing is to construct a new value. This looks as follows with
> record syntax:
> 
> voidcast VariantWithTwo{} =
>     VariantWithTwo { field1 = Void , field2 = Void }
> voidcast VariantWithOne{} =
>     VariantWithOne { field1 = Void }

Ah, but the reason I want to use field update, rather than a new
construction on the rhs, is that my type really has lots of other
fields (not mentioned here), which are all of fixed definite types
(not parametric).  It is much nicer to be able to write

    v { field1 = Void }

than

    VariantWithOne { field1 = Void
                   , field2 = field2 v
                   , field3 = field3 v
                   , field4 = field4 v
                   , field5 = field5 v
                     ...
                   }

which has so much more scope for making a mistake, not to mention the
extra maintainance work required if I ever add or delete a field from
the type.

Regards,
    Malcolm


More information about the Haskell-Cafe mailing list