Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Cergoo opened this issue Aug 8, 2024 · 5 comments
Open

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

Cergoo opened this issue Aug 8, 2024 · 5 comments
Labels
Generics[T] Bugs/feature requests, that are related to the V generics. Result Type Bugs/feature requests, that are related to `!Type`. Unit: Checker Bugs/feature requests, that are related to the type checker.

Comments

@Cergoo
Copy link

Cergoo commented Aug 8, 2024

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: 

@Cergoo Cergoo changed the title error C too passing a function to a function as a parameter does not work Aug 8, 2024
@felipensp
Copy link
Member

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
Copy link
Member

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

@felipensp felipensp self-assigned this Aug 8, 2024
@felipensp felipensp added Generics[T] Bugs/feature requests, that are related to the V generics. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Result Type Bugs/feature requests, that are related to `!Type`. labels Aug 8, 2024
@Cergoo
Copy link
Author

Cergoo commented Aug 8, 2024

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
Copy link
Member

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

@Delta456
Copy link
Member

Yes, it should be a checker error

@felipensp felipensp removed their assignment Aug 12, 2024
@felipensp felipensp added Unit: Checker Bugs/feature requests, that are related to the type checker. and removed Unit: cgen Bugs/feature requests, that are related to the default C generating backend. labels Aug 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Generics[T] Bugs/feature requests, that are related to the V generics. Result Type Bugs/feature requests, that are related to `!Type`. Unit: Checker Bugs/feature requests, that are related to the type checker.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants