www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Bug or feature?

reply "Jack Applegame" <japplegame gmail.com> writes:
I don't see any reason why it should not compile.

import std.array;
import std.range;
import std.algorithm;

class Foo {
}

void main() {
	auto result = iota(3).map!(i => new immutable Foo).array();
}

/usr/include/dmd/phobos/std/conv.d(4028): Error: cannot 
implicitly convert expression (arg) of type immutable(Foo) to 
test.Foo
/usr/include/dmd/phobos/std/conv.d(3931): Error: template 
instance 
std.conv.emplaceImpl!(immutable(Foo)).emplaceImpl!(immutable(Foo)) error
instantiating
/usr/include/dmd/phobos/std/array.d(115):        instantiated 
from here: emplaceRef!(immutable(Foo), Foo, immutable(Foo))
test.d(9):        instantiated from here: 
array!(MapResult!(__lambda1, Result))
Jun 28 2015
next sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
I'd say bug, I think the array function is trying an optimization 
it shouldn't be trying for immutable classes.
Jun 28 2015
parent "Jack Applegame" <japplegame gmail.com> writes:
https://issues.dlang.org/show_bug.cgi?id=14751
Jun 30 2015
prev sibling next sibling parent reply Jonathan M Davis via Digitalmars-d-learn writes:
On Sunday, June 28, 2015 11:37:59 Jack Applegame via Digitalmars-d-learn wrote:
 I don't see any reason why it should not compile.

 import std.array;
 import std.range;
 import std.algorithm;

 class Foo {
 }

 void main() {
   auto result = iota(3).map!(i => new immutable Foo).array();
 }

 /usr/include/dmd/phobos/std/conv.d(4028): Error: cannot
 implicitly convert expression (arg) of type immutable(Foo) to
 test.Foo
 /usr/include/dmd/phobos/std/conv.d(3931): Error: template
 instance
 std.conv.emplaceImpl!(immutable(Foo)).emplaceImpl!(immutable(Foo)) error
instantiating
 /usr/include/dmd/phobos/std/array.d(115):        instantiated
 from here: emplaceRef!(immutable(Foo), Foo, immutable(Foo))
 test.d(9):        instantiated from here:
 array!(MapResult!(__lambda1, Result))
You haven't declared an immutable constructor, so you can't construct an immutable Foo. If the default constructor were pure, then it could be used to construct immutable objects even if it weren't immutable, but the default constructor is neither pure nor immutable, and purity isn't inferred for normal functions. It might make sense as a feature request to request that a class' default constructor be pure (assuming that its base class constructor is pure), since theoretically, it should be able to be pure, but that would be a change in the language. What you're seeing is not a bug. It's how the current design works. - Jonathan M Davis
Jun 29 2015
next sibling parent reply "anonymous" <anonymous example.com> writes:
On Monday, 29 June 2015 at 12:04:46 UTC, Jonathan M Davis wrote:
 You haven't declared an immutable constructor, so you can't 
 construct an immutable Foo.
That's not what's happening. Constructing an immutable Foo works just fine.
Jun 29 2015
parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Monday, 29 June 2015 at 14:28:06 UTC, anonymous wrote:
 On Monday, 29 June 2015 at 12:04:46 UTC, Jonathan M Davis wrote:
 You haven't declared an immutable constructor, so you can't 
 construct an immutable Foo.
That's not what's happening. Constructing an immutable Foo works just fine.
Then I stand corrected. - Jonathan M Davis
Jun 30 2015
prev sibling parent Daniel =?UTF-8?B?S296w6Fr?= <kozzi dlang.cz> writes:
On Mon, 29 Jun 2015 05:04:36 -0700
Jonathan M Davis via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> wrote:

 On Sunday, June 28, 2015 11:37:59 Jack Applegame via
 Digitalmars-d-learn wrote:
 I don't see any reason why it should not compile.

 import std.array;
 import std.range;
 import std.algorithm;

 class Foo {
 }

 void main() {
   auto result = iota(3).map!(i => new immutable Foo).array();
 }

 /usr/include/dmd/phobos/std/conv.d(4028): Error: cannot
 implicitly convert expression (arg) of type immutable(Foo) to
 test.Foo
 /usr/include/dmd/phobos/std/conv.d(3931): Error: template
 instance
 std.conv.emplaceImpl!(immutable(Foo)).emplaceImpl!(immutable(Foo))
 error
 instantiating /usr/include/dmd/phobos/std/array.d(115):
 instantiated from here: emplaceRef!(immutable(Foo), Foo,
 immutable(Foo)) test.d(9):        instantiated from here:
 array!(MapResult!(__lambda1, Result))
You haven't declared an immutable constructor, so you can't construct an immutable Foo. If the default constructor were pure, then it could be used to construct immutable objects even if it weren't immutable, but the default constructor is neither pure nor immutable, and purity isn't inferred for normal functions. It might make sense as a feature request to request that a class' default constructor be pure (assuming that its base class constructor is pure), since theoretically, it should be able to be pure, but that would be a change in the language. What you're seeing is not a bug. It's how the current design works. - Jonathan M Davis
No it is a bug in std.conv.emplaceRef or more probably in std.array.
Jun 29 2015
prev sibling parent "Jack Applegame" <japplegame gmail.com> writes:
fix - https://github.com/D-Programming-Language/phobos/pull/3524
Aug 04 2015