Extended initialization
Posted: 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.
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:
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?
- 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?