[Haskell-cafe] records proposals list

Wolfgang Jeltsch wolfgang at jeltsch.net
Sat Nov 19 09:26:36 EST 2005


Am Samstag, 19. November 2005 14:57 schrieb David Roundy:
> [...]

> 2. Multi-constructor getters, ideally as a function:
>
> An accessor ought to be able to access an identically-named field from
>
> multiple constructors of a given data type:
> > data FooBar = Foo { name :: String } | Bar { name :: String }
>
> However we access "name", we should be able to access it from either
> constructor easily (as Haskell 98 does, and we'd like to keep this).

Let's take a concrete example.  Say, I have a type Address which is declared 
as follows:

	data Address = OrdinaryAddr {
		name :: String,
		street :: String,
		number :: Int,
		city :: String,
		postalCode :: Int
	} | POBoxAddr {
		name :: String,
		poBox :: Int,
		city :: String,
		postalCode :: Int
	}

In this example, it would be really good if there was a getter function for 
extracting the name out of an ordinary address as well as an PO box address.  
But in my opinion, the above declaration is not very nice and one should 
write the following instead:

	data Address = Address {
		name :: String,
		destination :: Destination,
		city :: String,
		postalCode :: Int
	}

	data Destination = OrdinaryDest {
		street :: String,
		number :: Int
	} | POBoxDest {
		poBox :: Int
	}

And with this declaration we wouldn't need getter functions which are able to 
access identically-named fields from different data constructors of the same 
type.  So I wonder if this feature is really sensible.

> [...]

Best wishes,
Wolfgang


More information about the Haskell-Cafe mailing list