[nem-en] Declaring Properties in the constructor - ala Boo

Michal Moskal michal.moskal at gmail.com
Sat Sep 30 15:49:52 CEST 2006


On 9/30/06, d <d at hell.art.pl> wrote:
>
> On Sep 29, 2006, at 10:07 PM, Michal Moskal wrote:
>
> > On 9/29/06, Mark Haniford <markhaniford at gmail.com> wrote:
> >
> >> I think one of the neat features of Boo is that you can declare
> >> property
> >> values in the constructor:
> >> http://boo.codehaus.org/Part+09+-+Methods#Part09-Methods-
> >> DeclaringPropertiesintheConstructor
> >>
> >> e.g.  window = Window (Width: 800, Height: 600, Title:
> >> "Window");  // where
> >> Width, Height, and Title are all public properties in the class
> >> Window
> >>
> >> This feature seems very convienent to me.  It would probably be
> >> pretty easy
> >> to write a macro to do it too, e.g. window = Construct(SomeClass,
> >> Width:
> >> 800, Height: 600, Title: "Window");
> >>
> >> But would it be more convenient to have it in the compiler
> >> proper?  In the
> >> case of constructors with arguments, you could disallow it, or
> >> have optional
> >> property arguments after the defined constructor arguments
> >
> > I personally like it -- that would be consistant with custom
> > attributes.
> >
> > The only problem I see is proper syntax. ':' cannot be used, as it is
> > typecast operator. On the other hand, plain '=' conflicts with named
> > parameters (I would rather avoid deciding if something is a property
> > or named parameter based on context).
>
>   Right, the idea is nice. Quite useful. But I see Nemerle is
> beginning to overflow with features, which will be understandable to
> some and not quite so clear to others. My suggestion would be then,
> to give features that are not commonly known (be it in the OO, or the
> FP world) a clear, descriptive syntax. For example:
>
> def c = Cons (1, 2, x = "foo", initializers (y = "bar", z = "baz"));
>
>   This seems clear to me, but maybe you can think of something
> better. You know, along those lines.

I kind of like:

  def c = Cone (1, 2, x = "foo") with (y = "bar", z = "baz")

It's general (not only for construction), it's easy to implement as a macro.

It's good because VB and Pascal have 'with'. Also OCaml has it. The
problem is that in OCaml it means to create a new object with listed
fields changed and in Nemerle it would mean to change the object and
return it. I would like to have both, but am not sure about the
syntax... Also the implementation of the copying semantics is not that
easy.

-- 
   Michał


More information about the devel-en mailing list