the new statement

10 views
Skip to first unread message

Jake Verbaten

unread,
Mar 24, 2012, 4:19:48 PM3/24/12
to cando...@googlegroups.com
The "OO" example uses a new operator

// First create a fast shallow clone of the Rectangle prototype
rect = new Rectangle

// Then initialize it using a one of the functions
rect.initialize(rect, 3, 5)

Personally I like having new as a fast shallow clone however having to call initialize manually is a pain

You could support

rect = new Rectangle(3, 5)

which would desugar to 

rect = new Rectangle
rect:initialize(3,5)

Tim Caswell

unread,
Mar 24, 2012, 5:22:47 PM3/24/12
to cando...@googlegroups.com
Remember that candor is to js like C is to C++.  C++ has constructors, but in C, you malloc your struct (or declare it local) and then pass it to some initialize function.  The thing I really don't like about new in JS is all the magic it does.  It makes learning the language really hard.

Fast clone and : syntax are almost all the primitives needed to implement your own oop framework.  The main thing missing is varargs.  If we has varargs, for example, I could write this:

    Object = {
      new: (self, ...) {
        obj = new self
        if (obj.initialize) obj:initialize(...)
        return obj
      }
    }

    Rect = new Object
    Rect.initialize = (self, w, h) {
      self.w = w
      self.h = h
    }

    r = Rect:new(4, 5)

Jake Verbaten

unread,
Mar 24, 2012, 5:32:27 PM3/24/12
to cando...@googlegroups.com
I actually think I agree with you on that.

Having the new keyword desugar to initialization invoking is too much magic.

Bradley Meck

unread,
Mar 25, 2012, 4:01:04 PM3/25/12
to cando...@googlegroups.com
Just a note for future viewers of this post. In order to deal with Objects that must be initialized via (somewhat repetitive) configuration, please make a configurable factory.

Jake Verbaten

unread,
Mar 25, 2012, 4:09:42 PM3/25/12
to cando...@googlegroups.com
When you say configurable factory do you mean something like

Proto = { ... }

factory(state) {
  o = new Proto
  o.state = state
  return o
}

On Sun, Mar 25, 2012 at 9:01 PM, Bradley Meck <bradle...@gmail.com> wrote:
Just a note for future viewers of this post. In order to deal with Objects that must be initialized via (somewhat repetitive) configuration, please make a configurable factory.

--
You received this message because you are subscribed to the Google Groups "candorlang" group.
To view this discussion on the web visit https://groups.google.com/d/msg/candorlang/-/1M4X5S0NM9MJ.
To post to this group, send email to cando...@googlegroups.com.
To unsubscribe from this group, send email to candorlang+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/candorlang?hl=en.

Fedor Indutny

unread,
Mar 25, 2012, 4:12:22 PM3/25/12
to cando...@googlegroups.com
Jake,

I think it's more like this:


(All credits to Tim Caswell).

Cheers,
Fedor.
Reply all
Reply to author
Forward
0 new messages