Forums

idea: defining class attributes via constructor?

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

idea: defining class attributes via constructor?

Postby gauthier » Fri Mar 06, 2009 3:05 pm

what if :
class Foo
def init(foo as String, bar as DateTime)
.foo = foo, .bar = bar

would render:
class Foo
var foo as String
var bar as DateTime
def init(foo as String, bar as DateTime)
.foo = foo, .bar = bar


you could also use
Code: Select all
.foo is private = anotherParameterName


with multiple constructor overloads:
  • it will grab up every found member
  • the compiler should fail if non nil members (for references) are not properly initialized over all available constructors
what do you think?
gauthier
 
Posts: 116

Re: idea: defining class attributes via constructor?

Postby jonathandavid » Fri Mar 06, 2009 3:19 pm

What happens if I don't want properties generated for some of the constructor parameters?
jonathandavid
 
Posts: 159

Re: idea: defining class attributes via constructor?

Postby gauthier » Fri Mar 06, 2009 3:32 pm

jonathandavid wrote:What happens if I don't want properties generated for some of the constructor parameters?


I'm unsure to understand, if you don't want properties/fields you won't use
Code: Select all
.someMemberName = someParameter
in a constructor

am I following you?
gauthier
 
Posts: 116

Re: idea: defining class attributes via constructor?

Postby Charles » Sat Mar 07, 2009 11:03 am

gauthier wrote:what if :
...
you could also use
Code: Select all
.foo is private = anotherParameterName


with multiple constructor overloads:
  • it will grab up every found member
  • the compiler should fail if non nil members (for references) are not properly initialized over all available constructors
what do you think?

I can't say that I like the idea of having class members generated inside an implementation which could be further into the declaration:
cue init(x as int, y as int)
require
x >= 0 and x < 100
y >= 0 and y < 100
ensure
.x == x and .y == y
body
.x, .y = x, y # <-- in your proposal, this line generates class members

So far the shortcut syntax that we have is still based on declarations like:
pro age from int as var

I like that there will be a "pro x" or "get x" line that you see in the class declaration, can search for, possibly jump to with an editor key (next declaration, previous declaration).

Also, if we were to put these in the implementation, it would make it very awkward to have doc strings, contracts, modifiers, etc.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: idea: defining class attributes via constructor?

Postby gauthier » Mon Mar 16, 2009 4:12 pm

Chuck, I follow your point.

nevermind, I was surely "bored" not having resharper helpness at hand :)
gauthier
 
Posts: 116


Return to Discussion

Who is online

Users browsing this forum: No registered users and 42 guests