[Haskell] Re: Records

Johannes Waldmann waldmann at imn.htwk-leipzig.de
Mon Nov 28 12:01:04 EST 2005


Ketil Malde wrote:

>> Perhaps  data Foo = Foo { foo :: Int, bar :: Int ; bar = 2 * foo self }
>> with a reserved word "self" is better. - Are there semantic problems?
> 
> Can't you solve this by writing a fiunction to construct Foo with the
> desired properties? 

Sure but by the same argument I wouldn't need records at all
because I could use tuples :-) I'd say it makes the intention
of the programmer more explicit, and this is what pragmatics
of programming languages is all about, isn't it ...

Also, for updates ( like  x { foo = 3 } )
I'd expect that a new record is built
and the initializer is called again - this would be
inconvenient to write with functions because of the unwrapping
and wrapping (you would need to name all components even if they don't
change so you don't want to mention them).

I think it would be even more convenient if it were possible
to have initializers in separate modules (a la orphan instances)
because often I have one module that just defines the data type,
and then several modules that define operations.

I would need to know all the default-initialized fields
already when writing the type definition,
which is wrong design (it forces me to make decisions too early
and write them down in the wrong place).

I realize that my above idea looks something like introducing
objects through the back door. Because one might say that I want
something like a "Foo" with an additional component (bar)
that is ignored by those who don't know them.
This is indeed a severe criticism - normally I tend to avoid
implementation inheritance and now it looks like I'm advocating it...

best regards,
-- 
-- Johannes Waldmann -- Tel/Fax (0341) 3076 6479/80 --
---- http://www.imn.htwk-leipzig.de/~waldmann/ -------



More information about the Haskell mailing list