V: .split().map() crashes

Created on 2 Dec 2019  ·  5Comments  ·  Source: vlang/v

V version: V 0.1.22 1f93bb5 (prebuilt from the site)
OS: Linux 4.7.0-040700rc3-generic x86_64

What did you do?

fn main() {
  str := '1,2,3'
  ints := str.split(',').map(it.int())
  for i in ints { print(i) }
}

What did you expect to see?
123

What did you see instead?

❯ v run test.v
warning: test.v:3:6: `ints` declared and not used
/tmp/v/test.tmp.c: In function ‘main__main’:
/tmp/v/test.tmp.c:3353:27: error: ‘string {aka struct string}’ has no member named ‘data’
  string it = ((string*)str.data)[i];
                           ...
(Use `v -g` to print the entire error message)

V error: C error. This should never happen.
Please create a GitHub issue: https://github.com/vlang/v/issues/new/choose
Bug Compiler

Most helpful comment

That's what I ended up going with!

It's not so bad, but would be nice if the compiler said as much.

All 5 comments

map and filter are magic functions from what I can tell :) kinda limited use right now, the following is also not allowed at the moment (having .map or .filter in a for expression):

fn main() {
    str := '1,2,3,4'.split(',')
    for num in str.map(it.int()) {
        println(num)
    }
}

gives the following error:

$ v run test.v
/tmp/v/test.tmp.c: In function ‘main__main’:
/tmp/v/test.tmp.c:3278:1: error: expected expression before ‘array’
 array tmp2 = new_array(0, str .len, sizeof(int));
 ^~~~~
/tmp/v/test.tmp.c:3281:9: err...
(Use `v -g` to print the entire error message)

V error: C error. This should never happen. 
Please create a GitHub issue: https://github.com/vlang/v/issues/new/choose

I guess it has to do with how those functions are translated into for loops during compilation?

Yes, for now map and filter better be used in isolation. That will be improved soon.
You can try this @zeddidragon :

fn main() {
  str := '1,2,3'
  splitted := str.split(',')
  ints := splitted.map(it.int())
  for i in ints { print(i) }
}

That's what I ended up going with!

It's not so bad, but would be nice if the compiler said as much.

Minimal repro:

fn main() {
  [1].map(it + 1)
}

Output with -cg: pastebin

Should work now.

fn main() {
  str := '1,2,3'
  ints := str.split(',').map(it.int())
  for i in ints { print(i) }
}
$ vrun test4.v
123
Was this page helpful?
0 / 5 - 0 ratings

Related issues

vtereshkov picture vtereshkov  ·  3Comments

XVilka picture XVilka  ·  3Comments

jtkirkpatrick picture jtkirkpatrick  ·  3Comments

oleg-kachan picture oleg-kachan  ·  3Comments

ArcDrake picture ArcDrake  ·  3Comments