Property discussion wrap-up

Dmitry Olshansky dmitry.olsh at gmail.com
Wed Jan 30 09:35:16 PST 2013


30-Jan-2013 21:02, Zach the Mystic пишет:
> On Wednesday, 30 January 2013 at 06:46:01 UTC, H. S. Teoh wrote:
>> That page only states the structs nested inside *functions* have a
>> context pointer to the function's local variables. It says nothing about
>> structs nested inside *structs*. (And yes, I looked. I was actually in
>> the middle of writing something about using structs to simulate
>> properties, and decided to look it up to be sure, and found that the
>> spec actually doesn't say what I thought it said.)
>>
>>
>> T
>
> Okay, cool. Two questions remain: 1) How hard to implement structs
> nested in structs to mimic ones nested in functions?


IMO if property is to be implemented in the library it has to include 
the field itself. (And I suspect properties should enjoy compiler support).

Then something like:

struct A{
	Property!(int, filter, getter) prop;
private:
	void func()
	{
		...
		prop.raw = xxx; //direct write
		prop = yyy; //goes through setter
	}
}

where .raw is the field itself and there must be a way to let only 
struct itself have access to it. I have one method to get this but I 
don't like it - put this in each module:

mixin PropertyForModule!(my_module);

introducing a Property template in this module, with private .raw 
accessible thusly only in this module.

Getter is then just any function that maps T => T, with x => x  by 
default so can be omitted.

Filter is something new but in essence it works like the following setter:

void setter(T)(ref T val, T newVal)
{
	val =  filter(newVal); //filter may through
}

It's a bit more restrictive though so feel free to destroy.

> 2) How much code
> breakage?

A lot + subtly wasting memory.

-- 
Dmitry Olshansky


More information about the Digitalmars-d mailing list