April 26, 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 😀

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@79

April 25, 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

April 26, 2022

😀

So the problem is that the structure is not in the inter-module space

On Monday, 25 April 2022 at 16:11:47 UTC, rassoc wrote:

>

This works:

import std;

void main() {
    struct Foo { string s; }
    Foo[] arr = ["abc", "def", "ghi"].map!Foo.array;
    arr.writeln; // => [Foo("abc"), Foo("def"), Foo("ghi")]
}

If sruct Foo {} is not taken outside of main() it will fail for many possibilities!

For example:

>

Error: cannot access frame pointer of source.main.Foo

SDB@79

April 25, 2022
On 4/25/22 21:32, Salih Dincer wrote:

> So the problem is that the structure is not in the inter-module space

Nested structs carry an additional pointer to their containing context. When they don't need the context, we define them with 'static':

void foo() {
  static struct Bar { }
}

> If ```sruct Foo {}``` is not taken outside of ```main()``` it will fail
> for many possibilities!

Only if the struct needs the context. Otherwise, nested structs should be defined with 'static' and there is no issue with map...

Ali

1 2
Next ›   Last »