Thread overview
properties as lvalues
May 14, 2006
cschueler
May 14, 2006
Ivan Kazmenko
May 14, 2006
Ivan Senji
May 15, 2006
Daniel Keep
May 15, 2006
BCS
May 14, 2006
From the specs:

Note: Properties currently cannot be the lvalue of an op=, ++, or -- operator.

This strikes me, as I thought this was very easy to do - just have the access function return a reference to the value. Oh wait, there are no references in D (at least I didn't find them in the specs)? I think C++ has explicit references for a reason, and this might be a hint why this is so.


May 14, 2006
"cschueler" <cschueler_member@pathlink.com> wrote in message news:e45s4n$2sf8$1@digitaldaemon.com...
> From the specs:
>
> Note: Properties currently cannot be the lvalue of an op=, ++, or -- operator.
>
> This strikes me, as I thought this was very easy to do - just have the
> access
> function return a reference to the value. Oh wait, there are no references
> in D
> (at least I didn't find them in the specs)? I think C++ has explicit
> references
> for a reason, and this might be a hint why this is so.

Personally I think nice-looking reference types would be useful as well (great for returning references to container elements, such as the return value of 'in' for associative arrays), but for properties, I've got another idea: explicit property syntax.  Tell the compiler what is "get" and what is "set," and with that info, it can determine how to compile something like

a.prop += 8;

In this case, it'd make sure it has both a "get" and "set" for the property "prop," and then it'd just generate

a.prop(a.prop() + 8);

The problem here is that there's no guarantee that the semantics of "get" and "set" match up, but there's no guarantee with the current method either (although it'd be bad programming practice if someone did that).


May 14, 2006
cschueler:
>Note: Properties currently cannot be the lvalue of an op=, ++, or -- operator. This strikes me, as I thought this was very easy to do - just have the access function return a reference to the value. Oh wait, there are no references in D (at least I didn't find them in the specs)? I think C++ has explicit references for a reason, and this might be a hint why this is so.

There's another situation I'd like to have references for. Consider a multi-sized array, with part of the program heavily working on a fixed part of the array. In C++, one may write, for example:
-----
typedef int arr [4] [4]; // 2D array

arr a [4] [4]; // 4D array

int main (void)
{
int i, j, k, l, m;
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
{
arr & b = a[i][j]; // 2D array currently used
for (k = 0; k < 4; k++)
for (l = 0; l < 4; l++)
for (m = 0; m < 4; m++)
b[k][l] += b[(k + m) & 3][(l + m) & 3]; // no particular sense here :)
}
return 0;
}
-----
That works perfectly in C++ but I didn't find a way to achieve similar effect in D. This could be another use of alias statement (most preferrable), or even with statement (though it will probably look weird). But so far, it seems neither can do the trick.

Ivan Kazmenko.
May 14, 2006
cschueler wrote:
> From the specs:
> 
> Note: Properties currently cannot be the lvalue of an op=, ++, or -- operator.
> 

It would also be nice if things like this worked:

class A
{
  int a { return 3; } //get
  int a(int value){}  //set
}

void func(ionut int x);

auto z = new A;

func(z.a);

wich would be equivalent to:

auto za = z.a(); //get
func(za);
z.a(za);         //set
May 15, 2006

Ivan Senji wrote:
> cschueler wrote:
>> From the specs:
>>
>> Note: Properties currently cannot be the lvalue of an op=, ++, or -- operator.
>>
> 
> It would also be nice if things like this worked:
> 
> class A
> {
>   int a { return 3; } //get
>   int a(int value){}  //set
> }
> 
> void func(ionut int x);
> 
> auto z = new A;
> 
> func(z.a);
> 
> wich would be equivalent to:
> 
> auto za = z.a(); //get
> func(za);
> z.a(za);         //set

One of the weird things I remember about VB6 was that it would allow you to pass an intermediate value ByRef to another function.  Imagine my surprise when I starting playing with "real" programming languages like C++, and absolutely none of them were capable of this.

I never really understood why it was seemingly so hard to implement. After all, even if it *is* an intermediate value, it's got to be stored somewhere in memory, right?

	-- Daniel

-- 

v1sw5+8Yhw5ln4+5pr6OFma8u6+7Lw4Tm6+7l6+7D a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP    http://hackerkey.com/
May 15, 2006
"Daniel Keep" <daniel.keep.lists@gmail.com> wrote in message news:e49krb$1eih$2@digitaldaemon.com...
> One of the weird things I remember about VB6 was that it would allow you to pass an intermediate value ByRef to another function.  Imagine my surprise when I starting playing with "real" programming languages like C++, and absolutely none of them were capable of this.
>
> I never really understood why it was seemingly so hard to implement. After all, even if it *is* an intermediate value, it's got to be stored somewhere in memory, right?

I think this was discussed last month or so, with the suggestion to allow operator overloads for temporary values.  I think it was reported as a suggestion on BugZilla as well.


May 15, 2006
cschueler wrote:
> From the specs:
> 
> Note: Properties currently cannot be the lvalue of an op=, ++, or -- operator. 
> 
> This strikes me, as I thought this was very easy to do - just have the access
> function return a reference to the value. Oh wait, there are no references in D
> (at least I didn't find them in the specs)? I think C++ has explicit references
> for a reason, and this might be a hint why this is so.
> 
>

another hitch on allowing get/set on a property is the case where the value is not actually part of the object or is checked on the set


class Accept
{
	bool[10] bools;

	int count()
	{
		int ret = 0;
		foreach(b; bools)
			b ? ret++: ret;
		return ret;
	}

	void count(int c)
	{
		if(c >= bools.length) throw new Error("Bad");

		int i;
		for(i=0; i<bools.length; i++)
			if(!(bools[i] ? c--; c) break;

		while(i<bools.length)
			bools[i--] = false;
	}

}