Page 1 of 1

idea: defining class attributes via constructor?

PostPosted: Fri Mar 06, 2009 3:05 pm
by gauthier
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?

Re: idea: defining class attributes via constructor?

PostPosted: Fri Mar 06, 2009 3:19 pm
by jonathandavid
What happens if I don't want properties generated for some of the constructor parameters?

Re: idea: defining class attributes via constructor?

PostPosted: Fri Mar 06, 2009 3:32 pm
by gauthier
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?

Re: idea: defining class attributes via constructor?

PostPosted: Sat Mar 07, 2009 11:03 am
by Charles
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.

Re: idea: defining class attributes via constructor?

PostPosted: Mon Mar 16, 2009 4:12 pm
by gauthier
Chuck, I follow your point.

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