Full Fledged Properties

js.mdnq js_adddot+mdng at gmail.com
Wed Dec 19 15:43:27 PST 2012


My method is simply defining complex value types with added 
functionality(methods) that do not cost memory(a ptr) to access 
their parent. It's really as simple as that. Unfortunately to do 
that in D requires a lot of boiler plate code. The main key here 
is "access to parent" as that is the issue at hand.

Since complex value types are structs already the issue is of 
nesting. I've taken care of the problem by doing the computation 
implicitly(which, unfortunately requires a lot of "metacode" to 
handle).

In your example, if you were only wrapping a string, then you 
could use my code to do so IF you wanted access to the parent 
without the cost of an additional pointer.

My original post was about codifying a way to deal with such 
structs(nested structs that have parents with zero waste).

In your case, it doesn't seem to apply, again, unless you want 
access the parent containing your struct which essentially wraps 
a string. (Although I guess one doesn't need to treat the structs 
as wrappers of types, that was my original reason)

Now, you do this

`
    @property void pSilly( Silly a_silly )
    {
       // maybe do some stuff.
        _Silly = a_silly; // Silly.opSssign is called
       // maybe do some more stuff.
    }
`

and if your do stuff does access members in the class, then my 
method would work for you and solve your problems! You can move 
all this code into the opAssign of Silly and it will work(by 
having to use Parent. for members of ReallySeally).

So, in some sense you seem to have the same issues that brought 
me to my solution. You essentially want an opAssign inside 
ReallySeally for Silly but you can't have that so you use a 
property.

The following code is similar to yours but we move all the 
getters and setters into Silly rather than having them in 
ReallySilly. Silly is essentially a parent aware complex value 
type that doesn't cost to be parent aware. (hence you can do 
whatever you do in ReallySilly inside Silly instead)

(I left out the templates mixin's to get this to work, they can 
be found in the original dpaste link)


import std.stdio;
import std.traits;


static template tp_Silly() {
	struct Silly(int _Offset = 0)
	{
		string _s;
		alias _s this;    // Simple getter and setter
  		void opAssign( string a_val )
		{
			// maybe do some stuff (can use Parent, might have to 
specialize on ReallySilly if used with other parents)
			_s = a_val;
			// maybe do more stuff
		}
	}
}

class _ReallySilly(bool _NestLevel = true)
{
	enum string __ClassNameFix = __traits(identifier, 
typeof(this))~"!("; enum string __NestLevelFix = "_NestLevel";

	public:
  		
		mixin tp_Silly; mixin(StructNestType!("Silly", "_Silly"));    
// i.e., Silly _Silly;
} alias _ReallySilly!() ReallySilly;



int main(string[] argv)
{

	auto vReallySilly = new ReallySilly;
	vReallySilly._Silly = "stringggg";
	writeln(vReallySilly._Silly);
}


More information about the Digitalmars-d mailing list