Proposition: accessory functions which are local to type.
Evgenij Merenkov
evgenij1 at hotmail.com
Thu Jul 15 00:38:34 EDT 2010
What I don't like about current accessory functions is that they pollute namespace.
data Coordinate2D = Coordinate2D { x :: Double, y :: Double}
will make x and y not available for other types:
data Coordinate3D = Coordinate3D { x :: Double, y :: Double, z :: Double}
What I am proposing is something like this:
data Coordinate = Coordinate where { x :: Double, y :: Double}
By using "where" (or other keyword) we mark those accesor functions as local to type. This means, that function can only be used in a context of a type, like this:
coord <- getRandomCoordinate
For modifying (same as current):
let newCoord = coord {x = 3, y = 4}
For accessing part of a type:
let x = coord.x
Current form "x coord" will generate error, as there is no standalone function "x". "x" can only be accessed in context of any function application that returns needed type. Meaning, it is valid to write something like this:
let plus (Coordinate2D x1 y1) (Coordinate2D x2 y2) = Coordinate2D (x1+x2) (y1+y2)
x = (plus coord coord).x
Yes, that means that function composition will need to use another symbol, but I think it is a great trade-off, as it will reduce namespace pollution and will make use of notation familiar to mainstram programmers (Java, C#, C, C++, etc).
_________________________________________________________________
Windows Live Hotmail: Your friends can get your Facebook updates, right from Hotmail®.
http://www.microsoft.com/middleeast/windows/windowslive/see-it-in-action/social-network-basics.aspx?ocid=PID23461::T:WLMTAGL:ON:WL:en-xm:SI_SB_4:092009
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-prime/attachments/20100714/5ead8f82/attachment.html
More information about the Haskell-prime
mailing list