Forums

Assignment to properties

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

Assignment to properties

Postby torial » Fri Nov 26, 2010 12:35 am

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?
torial
 
Posts: 229
Location: IA

Re: Assignment to properties

Postby hopscc » Fri Nov 26, 2010 1:43 am

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)
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Assignment to properties

Postby torial » Fri Nov 26, 2010 6:50 am

if I try:
Code: Select all
    pro results as List<of String> = List<of String>


I get:
error: Expecting EOL, but got "=" (ASSIGN) instead.
torial
 
Posts: 229
Location: IA

Re: Assignment to properties

Postby Charles » Fri Nov 26, 2010 4:10 pm

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?
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Assignment to properties

Postby torial » Fri Nov 26, 2010 4:23 pm

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.
torial
 
Posts: 229
Location: IA


Return to Discussion

Who is online

Users browsing this forum: No registered users and 74 guests