Assigning to array of structs with custom constructor

Ali Çehreli acehreli at yahoo.com
Tue Apr 26 03:03:56 UTC 2022


On 4/25/22 19:37, Salih Dincer wrote:

 > a lot of errors 😀

Hm. I can't reproduce any of that. I did two things:

1) Added necessary import directives

2) Moved all expressions into the main() function

I did not change anything else. The program below compiles and works 
with all these compilers:

- dmd 2.098.1
- dmd 2.099.1
- gdc 11.2.0
- ldc 1.28.1 (based on DMD v2.098.1 and LLVM 13.0.1)

Are you using a very old compiler?

import std.range;
import std.algorithm;
import std.conv;
import std.stdio;

struct Bar {
   string s;

   this(R)(R result) {
     import std.conv : to;
     this.s = result.to!string;
   }

   string toString() {
     return s;
   }
}

void main() {
   auto parts = "abcdefghi".chunks(3);
   auto compiled = parts.map!(a => Bar(a));


   // NOTE: PLEASE IGNORE THE ERROR MESSAGES BELOW.
   //       EVERYTHING COMPILES AND WORKS.


   auto notCompile1 = parts.map!Bar;
   /* Error 1: instantiated from here: `map!(Chunks!string)`*/

   auto notCompile2 = parts.map!(c => c.to!string)
                      .map!Bar;
   /* Error 1: 
/usr/src/dmd/linux/bin64/../../src/phobos/std/algorithm/iteration.d(604)
      : cannot access frame pointer of `source.main.Bar`

      Error 2: 
/usr/src/dmd/linux/bin64/../../src/phobos/std/algorithm/iteration.d(499)
      : template instance `std.algorithm.iteration.MapResult!(Bar, 
MapResult!(__lambda4, Chunks!string))` error instantiating

      Error 3: instantiated from here: `map!(MapResult!(__lambda4, 
Chunks!string))`
   */
   auto notCompile3 = parts.array.map!Bar;
   /* Error 1: 
/usr/src/dmd/linux/bin64/../../src/phobos/std/algorithm/iteration.d(604)
      : cannot access frame pointer of `source.main.Bar`

      Error 2: 
/usr/src/dmd/linux/bin64/../../src/phobos/std/algorithm/iteration.d(616)
      : cannot access frame pointer of `source.main.Bar`

      Error 3: 
/usr/src/dmd/linux/bin64/../../src/phobos/std/algorithm/iteration.d(499)
      : template instance `std.algorithm.iteration.MapResult!(Bar, 
Take!string[])` error instantiating

      Error 4: instantiated from here: `map!(Take!string
   */
   auto arr = compiled.array; /* [abc, def, ghi]

auto arr2 = str.chunks(3)
.map!(a => Bar(a))
.array;//*/

   arr.writeln(": ", typeof(arr).stringof);
}

Ali



More information about the Digitalmars-d-learn mailing list