Forums

Property syntax

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

Property syntax

Postby Charles » Sat Feb 26, 2011 4:36 pm

These days, all my property declarations tend to follow one of two forms.

1) Combine the "backing" variable declaration into the property declaration, typically for a one-liner like so:
class A

pro foo from var = List<of int>()


2) Declare the backing variable separately because the property has some logic:
class A

var _foo as List<of int>?

pro foo as List<of int>
get
if _foo is nil, _foo = List<of int>()
return _foo
set
_foo = value


There is an older approach that I no longer use:
class A

var _foo as List<of int>

# ...

pro foo from var

...except when maintaining older classes that use this approach.

Because that style has fallen out of use (AFAICT), and in the interest of keeping things succinct and convenient, I suggest that Cobra support:
class A

pro foo = List<of int>()

# and when you don't have an initial value:

pro bar as IDictionary<of String, int>?

# where the absence of the implementation of 'get' and 'set'
# implies storage by a backing variable


This is not an original suggestion. It was made earlier by users that were not attached to the original approach like I was, and consequently realized this much sooner than I did.

The "from var" variants would be left in place for legacy reasons. No code breakage. But samples, how-to's and wiki docs would emphasize the newer approach. Here is a more extensive test drive:
class Customer

pro name = '(noname)'

pro address as Address?

get orders = List<of Order>()

# or if you want the property type to be more general:
get orders as IList<of Order> = List<of Order>()

get balance as number
bal = 0.0
for order in orders, bal += order.bal
return bal


...so "from var" disappears and properties can have automatic backing vars regardless of how they are otherwise declared (initial expression (... = SomeValue) vs. type (... as SomeType)).

I think this reads and writes better.

Does anyone foresee any problems this would pose?

Does anyone object?

Do you like or dislike?
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Property syntax

Postby torial » Sat Feb 26, 2011 5:20 pm

Charles wrote:Does anyone foresee any problems this would pose?

Does anyone object?

Do you like or dislike?


I don't forsee any problems with this change, but I clearly haven't thought much about this particular issue.

No objection here.

Like. (or +1 if you prefer the comp.lang.python days)
torial
 
Posts: 229
Location: IA

Re: Property syntax

Postby hopscc » Mon Feb 28, 2011 7:24 am

Personally I prefer the original form with the explicit backing var decl and property decl explicitly referring to it.
For me it keeps in your face the idea of the non-public internal variable and the public property interface ( implementation vs interface)

Dont care regarding the marketing (samples,howto, wiki) emphasis so long as the old form is still supported and mentioned in doc.

The only issue I can see with dropping this is where you may want to access the backing var directly internal to the implementing class or subclass
but use the property form for 'external' access (interface) - for performance, varying constraint checking, ...
(or if want backing var with different access modifiers than default provided)

The proposed 2) form allows this but in a more verbose way than the existing shortcut syntax
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Property syntax

Postby Charles » Wed Mar 02, 2011 7:22 pm

Hops, you mentioned access to the backing var. The current form already allows this:
class A

pro foo from var = List<of int>()

def main
trace _foo

I'm only proposing a syntax change which would drop the "from var" in the above, and also allow property declarations that don't have initial values to get the same treatment.

I agree that if you're not satisfied with the default access level (protected) then you have to go explicit. Not sure if there is a good solution for that for cases where one wants, for example, private.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Property syntax

Postby hopscc » Thu Mar 03, 2011 11:39 pm

Yeah, I get the syntax change - no problems if all accesses are through the property (probably most common case)

If there any direct access to the backing variable then it appears you have a variable that appears from nowhere:
Its just that I look at the example ( similarly in the current form) and the first question is 'whats _foo and wheres its code declaration?'
- it isnt so now look for something that's implicitly creating it...
In the current form at least the 'from var' on the prop decl indicates that there's more happening than just the bare prop decl.

More obvious an issue when the prop decl is widely separated from the code twiddling the backing var

As I say I like it laid out explicitly so for props I don't do it like the current form example you give
( if theres any access to the backing variable directly)
I declare both bits explicitly - The prop is for external access - the 'from var' (or from <id>) ties it to the mechanism/implementing backing variable and any local manipulation of the backing variable is easily traced back to its explicit decl
and its all there (somewhere) in the code.

Thats my own (weird) preference - so long as the existing syntax is still available I dont really care/mind
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Property syntax

Postby torial » Fri Mar 04, 2011 9:18 pm

Does Cobra support mixed accessor properties, for instance in C#, I can do:

Code: Select all
int WordCount {get; private set;}


Looking at the Wiki, I am thinking this feature is not yet implemented.
torial
 
Posts: 229
Location: IA

Re: Property syntax

Postby hopscc » Fri Mar 04, 2011 9:46 pm

It was Requested/described/opened and logged on ticket:123 about 2 years ago.
No action as yet.
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Property syntax

Postby Charles » Thu Jun 06, 2013 6:38 pm

Bump. Re: both the proposal and ticket:123
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Property syntax

Postby hopscc » Fri Jun 07, 2013 1:08 am

Re the proposal - I still like having the explicit 'from var' available as an explicit indicator that theres more happening here than is immediately displayed/coded (synthesis of backing variable).

One other thought in reading this again was where a property value (return value) is entirely synthesized in code, if the backing variable was always inserted regardless, there is now an unused/functionless backing variable placed in the generated back end code ( a small space inefficiency)..
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Property syntax

Postby Charles » Fri Jun 07, 2013 1:13 pm

hopscc wrote:Re the proposal - I still like having the explicit 'from var' available as an explicit indicator that theres more happening here than is immediately displayed/coded (synthesis of backing variable).

I understand what you're saying: My proposed syntax is more implicit and now you have to know what's going on. But the repetition gets annoying after awhile to the point that I prefer the more succinct version. Also, this won't be our first implicit behavior. Garbage collection, Inheriting methods, initializers, etc. ... are implicit as well.

hopscc wrote:One other thought in reading this again was where a property value (return value) is entirely synthesized in code, if the backing variable was always inserted regardless, there is now an unused/functionless backing variable placed in the generated back end code ( a small space inefficiency)..

I didn't follow this. Can you restate or show an example?
Charles
 
Posts: 2515
Location: Los Angeles, CA

Next

Return to Discussion

Who is online

Users browsing this forum: No registered users and 113 guests

cron