Page 1 of 1

Assignment to properties

PostPosted: Fri Nov 26, 2010 12:35 am
by torial
I had tried to assign an initial value to a property (can't leave off type definition, since it implements against an interface and complains if I leave it off), but I get an error. When I read the properties page in the Wiki and the How To example, I am under the impression that this is legal:

Code: Select all
    pro results from var as List<of String> = List<of String>


But I get the following error instead:
error: Incompatible types. Cannot assign value of type Type on the right to List<of String> on the left.


Am I doing something wrong?

Re: Assignment to properties

PostPosted: Fri Nov 26, 2010 1:43 am
by hopscc
Hmm
My understanding of the intended use of this is that its either deferred to the var
var _result = List<of String>()
pro result as var

or it uses the property definition to silently infer the backing variable
pro results as List<of String> = List<of String>
# creates backing var _result initialised to List<of String>()




Probably your combination case while (currently and wrongly) syntactically valid is semantically garbage ( i.e should be a syntax error)

Re: Assignment to properties

PostPosted: Fri Nov 26, 2010 6:50 am
by torial
if I try:
Code: Select all
    pro results as List<of String> = List<of String>


I get:
error: Expecting EOL, but got "=" (ASSIGN) instead.

Re: Assignment to properties

PostPosted: Fri Nov 26, 2010 4:10 pm
by Charles
torial wrote:Am I doing something wrong?


Your overall syntax is fine. And the error message is correct. Look more closely at your expression; it is lacking parens to instantiate the list which would be: List<of String>()

Without the parens a type in an expression such as "List<of String>" or "Object" is a reference to a type. Therefore it's type is System.Type.

As a matter of style, you may want to use IList for the type so that any kind of list can be used:
class X

pro results from var as IList<of String> = List<of String>()


But if you're not going to then you can eliminate the explicit type like so:
class X

pro results from var = List<of String>()


Both of the above work with the current compiler out of the repository. If you have any problems with them, let me know along with your version of Cobra.

Also maybe that error message can be improved to give a hint... Something like "Perhaps you need to instantiate the type?" Would that help?

Re: Assignment to properties

PostPosted: Fri Nov 26, 2010 4:23 pm
by torial
Thank you.. that fixed it.

I had instantiation messed up in my head, and for whatever reason I was being inconsistent w/ generics when I wasn't with other instantiations.