www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - opDot == alias this?

reply "Namespace" <rswhite4 googlemail.com> writes:
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.
Jul 18 2012
next sibling parent reply "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Wed, 18 Jul 2012 16:58:34 +0200, Namespace <rswhite4 googlemail.com>  
wrote:

 First:
 Why is opDot not listed here: http://dlang.org/operatoroverloading.html ?
 How much other operators exists which are not listed there?
I believe it's being deprecated. As far as I know, no other operators are unmentioned.
 Is opDot the same as alias this? I think so.
They are related, but not the same. opDot allows access to the returned type's members and functions, but not the type itself.
 What are the advantages and disadvantages of both?
alias this allows implicit type conversions, opDot does not. That's about it, I think.
 Which of them should i use?
Depends. Look above. If you want a type to be usable as if another type, use alias this. If you just want to be able to access members of the other type, use opDot.
 Will one of them disappear in the future?
As stated above, I believe opDot is scheduled for deprecation, but I'm unsure if (not to mention when) it will happen.
 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:
TDPL states that multiple alias this should be supported, but it has not yet been implemented in the compiler. -- Simen
Jul 18 2012
parent "Namespace" <rswhite4 googlemail.com> writes:
On Wednesday, 18 July 2012 at 17:14:51 UTC, Simen Kjaeraas wrote:
 On Wed, 18 Jul 2012 16:58:34 +0200, Namespace 
 <rswhite4 googlemail.com> wrote:

 First:
 Why is opDot not listed here: 
 http://dlang.org/operatoroverloading.html ?
 How much other operators exists which are not listed there?
I believe it's being deprecated. As far as I know, no other operators are unmentioned.
And what about opStar? Are there any side in the documentation that describes _all_ the Operators?
Jul 24 2012
prev sibling parent reply "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Wednesday, July 18, 2012 16:58:34 Namespace wrote:
 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.
opDot is going to be deprecated and really should have been already ( http://d.puremagic.com/issues/show_bug.cgi?id=2327 ). Don't use it. alias this and opDispatch give you the same thing, only better. http://stackoverflow.com/questions/9880064/d2-what-are-semantics-of-opdot - Jonathan M Davis
Jul 18 2012
parent "Namespace" <rswhite4 googlemail.com> writes:
Understand.
Many thanks to you both.
Jul 18 2012