[Grace-core] Fwd: [important] Traits
Andrew P Black
andrew.p.black at gmail.com
Tue Mar 12 12:11:07 PDT 2013
One thing that I don't like at all in our current proposal is that a client can make an object by saying
object trait-expresssion
and if I want that constructor to do something else, then I have to do two things:
(1) Write the factory method that does the two things, e.g.
method interval.from(base)to(limit)by(increment) {
if (increment=0) then { return error "interval with 0 increment" }
if (increment>0) then { object intervalUp(base,limit,increment) }
else { object intervalDown(base,limit,increment) }
(2) change every occurrence of object interval(base,limit,increment) in the client code to interval.from(base)to(limit)by(increment).
Number (1) is what you would expect, but number (2) is very bad. We can set a good example in our libraries by ALWAYS providing factory objects. But we have to make the traits public too, so that clients can inherit from them. So I don't see a way of preventing clients from client using them directly. Maybe allow object trait-expression ONLY in method that are annotated as factory methods?
On 11 Mar 2013, at 22:14 , James Noble wrote:
> Off the top of my head, I don't know why a "first class" trait couldn't
> simply implement its constructor method itself - making it a bit
> more like our original class design, closer to a Smalltalk class,
> so you wouldn't need to make factories that did nothing but called
> the trait "new" method. Presumably we can stop there and avoid
> the temptation towards the whole static mess...
We did something like this in Emerald, where an object could be both a class and a type. That is, a class was an object with a factory method, and a type was an object with s "signature" method. The same object could have both.
IF traits are first class, then we could have an object with methods
template // answers a trait that can be used as a template
new // answers an object that looks like it was made from self.template
signature // answers a type such that self.new:self.signature
But, at least for the beginning versions of the language, I think that we want to continue assuming that traits, type and objects are three different "sorts" of things.
So everything is not an object :-(
Andrew
More information about the Grace-core
mailing list