Thread overview
Bug or feature?
Jun 28, 2015
Jack Applegame
Jun 28, 2015
Adam D. Ruppe
Jun 30, 2015
Jack Applegame
Jun 29, 2015
Jonathan M Davis
Jun 29, 2015
anonymous
Jun 30, 2015
Jonathan M Davis
Jun 29, 2015
Daniel Kozák
Aug 04, 2015
Jack Applegame
June 28, 2015
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))
June 28, 2015
I'd say bug, I think the array function is trying an optimization it shouldn't be trying for immutable classes.
June 29, 2015
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

June 29, 2015
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.

June 29, 2015
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.

June 30, 2015
https://issues.dlang.org/show_bug.cgi?id=14751
June 30, 2015
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
August 04, 2015
fix - https://github.com/D-Programming-Language/phobos/pull/3524