Bug or feature?

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jun 29 05:04:36 PDT 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



More information about the Digitalmars-d-learn mailing list