vlang / v

Simple, fast, safe, compiled language for developing maintainable software. Compiles itself in <1s with zero library dependencies. Supports automatic C => V translation. https://vlang.io
MIT License
35.79k stars 2.16k forks source link

passing a function to a function as a parameter does not work #22008

Open Cergoo opened 3 months ago

Cergoo commented 3 months ago
module parselona

pub struct ParserResult[O] {
    i []u8
    o O
}

type TParser[O] = fn([]u8) !ParserResult[O]
type TF[O] = fn([]u8) O

fn take_part(count u32) TParser[[]u8] {
    return fn [count] (i []u8) !ParserResult[[]u8] {
        if i.len<count { return error('error len: ${i}') }
                return ParserResult[[]u8] {
                i: i[count..],
                o: i[..count],
               }
    }
}

fn map<O>(p TParser[u8], f TF[O]) TParser[O] {
    return fn [p, f] <O>(i []u8) !ParserResult[O] {
             r:=p(i)!
             return ParserResult{i:r.i, o:f(r.o)}
    }
} 

fn test_1() {
input := '10_ttt_uuuu_qqq_'.bytes()

  nnn:=take_part(2)  
  println(' it is: ${nnn(input)!}')
  fnf := fn(i[]u8) int { return 1 }
  n := map[int](nnn, fnf)

assert ['nn'] == ['ttt_', 'uuuu_', 'qqq_'] 
}
--- Testing... --------------------------------------------------------------------------------------------------------------------------------------
 FAIL     0.000 ms /home/prof/projects/v/parselona/src/parselona_test.v
 compilation failed:
================== C compilation error (from tcc): ==============
cc: /tmp/v_1000/tsession_704f1d69f740_01J4Q5YZCTYXCVAK9ESWAGAX7W/parselona_test.01J4Q5Z0T9ZS2Q9JCXM6E0YW4D.tmp.c:106: error: identifier expected
=================================================================
(You can pass `-cg`, or `-show-c-output` as well, to print all the C error messages).
builder error: 
felipensp commented 3 months ago

We have a parse issue here. map[int](nnn, fnf) is not about your map function, but it is trying to create V's map with int key and (type1, type2) multi-return type.

felipensp commented 3 months ago

The compiler error about generating resul type is being treated in the referenced PR.

Cergoo commented 3 months ago

We have a parse issue here. map[int](nnn, fnf) is not about your map function, but it is trying to create V's map with int key and (type1, type2) multi-return type.

o, thenks. I rename func.

spytheman commented 2 months ago

Imho fn map<O>( should be a parse error, @felipensp, @medvednikov what do you think?

Delta456 commented 2 months ago

Yes, it should be a checker error