Multi-parameter OOP
Pixel
pixel@mandrakesoft.com
29 Oct 2001 00:25:05 +0100
Richard Uhtenwoldt <greon@best.com> writes:
[...]
> My reason for posting is to
> bring attention to how something similar is done in the language merd,
> whose designer posts here under the name pixel, and which
> is actually implemented I think.
not really. still coding... :)
[...]
> http://merd.sourceforge.net/types.png
Was in too small resolution, fixed.
Try http://merd.sourceforge.net/types.pdf for nicer display.
> In addition, merd has a type called a struct, written a |&| b.
> For reasons I do not understand, instead of defining, eg,
>
> point = (double,double)
>
> or
>
> point = (Point,double,double)
>
> pixel prefers to define
>
> point = (X,double) |&| (Y,double)
The reason is tuples do not have any subtyping relationship (unless you add
it, but it gets ugly ;p)
(it also enforces that record fields are not ordered)
The |&| is defined elsewhere[1] as /\ aka the intersection type operator
(maybe i should rename it, |&| is ugly, /\ is not bad...)
It is generally used as a record subtyping operator.
It is also used for ad'hoc overloading:
show !! String -> String |&| Int -> String [2]
but in fact, it can be useful in many areas, if you allow |&| to work on
values:
default_val = 0 |&| []
[ 1, "foo" ].map(x -> x + 1|&|"bar")
#=> [ 2, "foobar" ]
- type of [ 1, "foo" ] is List(1 | "foo")
(just like type of [ True, False ] is List(Bool) where Bool = True | False)
- (x -> x + 1|&|"bar") is alike (x -> x + 1) |&| (x -> x + "bar")
so the type is Int->Int |&| String->String [3]
- the result is of type List(String | Int) [4]
for pattern matching:
"(\s+):(\d+)" returns String, String|&|Int allowing the use with no cast
as an Int and/or as a String
[1] Intersection Types and Bounded Polymorphism http://citeseer.nj.nec.com/54630.html
[2] can be factorised as show !! String|Int -> String
[3] well in fact + !! o,o -> o where o !< &Addable
so the type is x -> x where 1|&|"bar" !< x !< &Addable
which gives x -> x where x !< String|Int
when going to the closed world
where + is defined for types Int and String
[4] hum, in fact merd evaluates what it can at compile time, so the type is
the type of [ 2, "foobar" ] which is List(2 | "foobar")
--
Pixel