Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

implementation for subset types

5 views
Skip to first unread message

TSa

unread,
Nov 24, 2006, 5:07:09 AM11/24/06
to p6l
HaloO,

I want to propose a little addition to subset type definitions.
They should get an implementation body like classes and roles.
This is useful to add methods to the type. Here is my Rectangle
example from the 'how to change the type of objects' thread.

# @.points contains Point objects
subset Rectangle of Polygon where { +@.points == 4 && ...}
{
# new methods
method width { return (@.points[1] - @.points[0]).abs }
method height { return (@.points[2] - @.points[1]).abs }

# overridden method from Polygon
method area { return self.width * self.height }
}

The content of such a definition might be restricted to methods
because I don't see how adding state with a has declarator could
work. Depending on the where clause the object's data would need
to be augmented with the additional slots.

We could continue the subtype chain with

subset Square of Rectangle where { .width == .height };

The semicolon might be necessary because it indicates the empty
definition body. Or could the definition block be completely
optional?


BTW, can a subset definition also be based on a role?


Regards, TSa.
--

Jonathan Lang

unread,
Nov 24, 2006, 7:24:20 PM11/24/06
to p6l
TSa wrote:
> HaloO,
>
> I want to propose a little addition to subset type definitions.
> They should get an implementation body like classes and roles.
> This is useful to add methods to the type. Here is my Rectangle
> example from the 'how to change the type of objects' thread.
>
> # @.points contains Point objects
> subset Rectangle of Polygon where { +@.points == 4 && ...}
> {
> # new methods
> method width { return (@.points[1] - @.points[0]).abs }
> method height { return (@.points[2] - @.points[1]).abs }
>
> # overridden method from Polygon
> method area { return self.width * self.height }
> }

As opposed to

subset Rectangle of class {
is Polygon;
method width { ... }
method height { ... }
...


} where { +@.points == 4 && ...}

?

Anonymous classes are useful things.

> BTW, can a subset definition also be based on a role?

IMHO, there's no reason not to allow this. About the only time that
something should be permitted for classes but not roles is when it
deals with object management.

--
Jonathan "Dataweaver" Lang

TSa

unread,
Nov 27, 2006, 3:52:12 AM11/27/06
to p6l
HaloO,

Jonathan Lang wrote:
> subset Rectangle of class {
> is Polygon;
> method width { ... }
> method height { ... }
> ...
> } where { +@.points == 4 && ...}
>
> ?
>
> Anonymous classes are useful things.

This is weird. The Polygon object shall be dynamically classified
as a Rectangle depending on the where clause. How does an object
that is an instance of the anonymous class get rid of its membership
when e.g. a fifth vertex is added? Or going the other way, how does
a general Polygon become an instance of the anonymous class when it
is modified such that it becomes a Rectangle? Remember that I've
used the Polygon/Rectangle example also to illustrate the question
how to retype an object.

The thing that the proposal shall achieve is to attach methods to
subset types. Multis can already be defined outside of class/role
implementations. So an alternative to the proposal to add a body
to the subset definition is to invent a syntax for outside methods.
I think it could look as follows:

method width on Rectangle {...}
method height on Rectangle {...}

The on-part drops the method into the respective type.

Regards, TSa.
--

0 new messages