Assigning to array of structs with custom constructor

Salih Dincer salihdb at hotmail.com
Tue Apr 26 02:37:41 UTC 2022


On Tuesday, 26 April 2022 at 00:57:54 UTC, Ali Çehreli wrote:
> On 4/25/22 16:59, Salih Dincer wrote:
>
> > Because it cannot be used with other possibilities such as
> > ```chunks()``` and ```take()```.
>
> There must be something wrong. map is commonly used with 
> chunks(), take(), etc.
>
> > Also it cannot be customized with
> > ```toString()```.
>
> Can you show examples of these please?

Of course, I have a few friends here:
and a lot of errors 😀

```d
struct Bar {
     string s;

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

     string toString() {
       return s;
     }
   }
   auto parts = "abcdefghi".chunks(3);
   auto compiled = parts.map!(a => Bar(a));

   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);
```

SDB at 79


More information about the Digitalmars-d-learn mailing list