opDot == alias this?

Namespace rswhite4 at googlemail.com
Wed Jul 18 07:58:34 PDT 2012


First:
Why is opDot not listed here: 
http://dlang.org/operatoroverloading.html ?
How much other operators exists which are not listed there?

And:
Is opDot the same as alias this? I think so.

This code

[code]
class Foo {
public:
	void echo() const {
		writeln("Foo");
	}
}

class Test {
private:
	Foo _f;

public:
	this(ref Foo f) {
		this._f = f;
	}

	@property
	ref Foo opDot() {
		return this._f;
	}
}
[/code]

Works exactly as these:

[code]
class Test2 {
private:
	Foo _f;

public:
	this(ref Foo f) {
		this._f = f;
	}

	@property
	ref Foo Get() {
		return this._f;
	}

	alias Get this;
}
[/code]

But with opDot it is shorter. What are the advantages and 
disadvantages of both?

Which of them should i use? Will one of them disappear in the 
future?
I think opDot is more flexible as alias this. You can have one 
alias this for one class/struct, but various opDot's, as this 
example shows:

[code]
class Foo {
public:
	void echo() const {
		writeln("Foo");
	}
}

class Bar {
public:
	void echo() const {
		writeln("Bar");
	}
}

class Test3 {
private:
	Foo _f;
	Bar _b;

public:
	this(ref Foo f, ref Bar b) {
		this._f = f;
		this._b = b;
	}

	@property
	ref Foo opDot() {
		return this._f;
	}

	@property
	const(Bar) opDot() const {
		return this._b;
	}
}
[/code]

If Test3 is unconst, it prints with Foo, otherwise with Bar.


More information about the Digitalmars-d-learn mailing list