Forums

Extended initialization

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Extended initialization

Postby Charles » Thu Aug 14, 2008 5:20 am

Cobra is a little behind in this area as C#, VB, Chrome/Oxygene and Boo have this covered.
Code: Select all
    // C# 3.0
    var r = new Rectangle {
        P1 = new Point { X = 0, Y = 1 },
        P2 = new Point { X = 2, Y = 3 }
    };

    ' Visual Basic 9.0
    Dim r as New Rectangle With {.P1 = New Point With {.X = 0, .Y = 1}, .P2 = New Point With {.X = 2, .Y = 3}}

    { Chrome / Oxygene / Object Pascal }
    var r = new Rectangle(P1 := new Point(X := 0, Y := 1), P2 := new Point(X := 2, Y := 3));

    # Boo
    r = Rectangle(P1: Point(X: 0, Y: 1), P2: Point(X: 2, Y: 3))

    # Cobra
    r = Rectangle(.p1=Point(.x=0, .y=1), .p2=Point(.x=2, .y=3))

    # passing init args and setting properties
    c = Customer('Acme, Inc.', .region=Regions.South)

The Cobra syntax is like Chrome and Boo where the assignments are kept inside the parens. Cobra programmers are already familiar with using leading dots to access members so that part seemed pretty natural.

Alternatively, we could put the assignments outside. Here are both:
# Extended Initialization

# In call
r = Rectangle(.p1=Point(.x=0, .y=1), .p2=Point(.x=2, .y=3))
c = Customer('Acme, Inc.', .region=Regions.South)

# Post call
r = Rectangle() (.p1=Point()(.x=0, .y=1), .p2=Point()(.x=2, .y=3))
c = Customer('Acme, Inc.') (.region=Regions.South)

# one more idea: don't require the leading .

# In call
r = Rectangle(p1=Point(x=0, y=1), p2=Point(x=2, y=3))
c = Customer('Acme, Inc.', region=Regions.South)

# Post call
r = Rectangle() (p1=Point()(x=0, y=1), p2=Point()(x=2, y=3))
c = Customer('Acme, Inc.') (region=Regions.South)

Hmm, coming from Python, the dot-less syntax looks pretty nice. If Cobra ever supports keyword arguments they would take precedence over properties (and probably with no confusion given the names are the same and we're talking about initializers here).

Note that Cobra is already blocking the use of assignment expressions (x=y) as arguments in method calls as a way to reserve that syntax for setting properties and/or passing keyword args.

Comments?
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Extended initialization

Postby Charles » Tue Aug 19, 2008 3:57 am

I forgot to note that C# actually supports the "keyword argument" syntax of SomeClass(arg1, PropName=A, PropName=B) in attributes. But then in C# 3.0, they chose the alternate syntax you see above, presumably to avoid collisions with assignment statements and/or because they liked it better.

I'm currently leaning towards the keyword syntax for Cobra:
saveButton = Button(parent=panel, text='Save', autoSize=true)

Cobra currently gives an error if you use an assignment for an argument so no legacy code will be affected.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Extended initialization

Postby relez » Sat Aug 23, 2008 3:22 pm

yes, you're right, i believe. dotless sintax is surely better
relez
 
Posts: 69

Re: Extended initialization

Postby Charles » Thu Aug 28, 2008 1:01 am

This is checked into development now. Some error checking is missing, but a positive test case works fine.
Charles
 
Posts: 2515
Location: Los Angeles, CA


Return to Discussion

Who is online

Users browsing this forum: No registered users and 11 guests