www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - alias this and constructor

reply "Jack Applegame" <japplegame gmail.com> writes:
Why this code doesn't want to compile?

import std.algorithm;
import std.array;

struct Foo {
	int a;
	this(int v) {}
	alias a this;
}

void main() {
	immutable(Foo)[] foo;
	auto arr = array(foo.filter!(o => true));
}

http://dpaste.dzfl.pl/25572b0f6d0b
Apr 12 2014
next sibling parent "Jack Applegame" <japplegame gmail.com> writes:
If you just comment out constructor, then it compiles.
Apr 12 2014
prev sibling next sibling parent reply "MrSmith" <mrsmith33 yandex.ru> writes:
On Saturday, 12 April 2014 at 11:48:36 UTC, Jack Applegame wrote:
 Why this code doesn't want to compile?

 import std.algorithm;
 import std.array;

 struct Foo {
 	int a;
 	this(int v) {}
 	alias a this;
 }

 void main() {
 	immutable(Foo)[] foo;
 	auto arr = array(foo.filter!(o => true));
 }

 http://dpaste.dzfl.pl/25572b0f6d0b
Maybe, because compiler cannot distinguish between default constructor which takes int and your one, which also takes int.
Apr 12 2014
parent reply "Jack Applegame" <japplegame gmail.com> writes:
On Saturday, 12 April 2014 at 12:47:02 UTC, MrSmith wrote:

 Maybe, because compiler cannot distinguish between default 
 constructor which takes int and your one, which also takes int.
I don't think so. Without alias this, but with constructor it compiles.
Apr 12 2014
parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Saturday, 12 April 2014 at 13:08:48 UTC, Jack Applegame wrote:
 On Saturday, 12 April 2014 at 12:47:02 UTC, MrSmith wrote:

 Maybe, because compiler cannot distinguish between default 
 constructor which takes int and your one, which also takes int.
I don't think so. Without alias this, but with constructor it compiles.
The issue is that a call that was essentially supposed to do nothing, accidentally calls your constructor. Because the call "that does nothing" was not expected to do much, it was marked as safe, pure and nothrow. This conflicts with your constructor, which is none of those. That's the explanation for the behavior you are seeing, it shouldn't be happening. You can work around it by marking your constructor as safe, pure and nothrow. https://github.com/D-Programming-Language/phobos/pull/1529#discussion_r7175502
Apr 12 2014
parent reply "Jack Applegame" <japplegame gmail.com> writes:
And this doesn't compile too
http://dpaste.dzfl.pl/1d48eed7e0fb

The same reason?
Apr 12 2014
parent "monarch_dodra" <monarchdodra gmail.com> writes:
On Saturday, 12 April 2014 at 14:00:06 UTC, Jack Applegame wrote:
 And this doesn't compile too
 http://dpaste.dzfl.pl/1d48eed7e0fb

 The same reason?
Looks like it's something different. I think I know the cause. Could you file it in the bug tracker? I will fix it.
Apr 12 2014
prev sibling parent "monarch_dodra" <monarchdodra gmail.com> writes:
On Saturday, 12 April 2014 at 11:48:36 UTC, Jack Applegame wrote:
 Why this code doesn't want to compile?
Aye! That's my bad! It's a bug I introduced, while fixing more bugs :/ The good news is it only affect 2.065.0. It's already fixed in HEAD. 1000 apologies.
Apr 12 2014