Skip to content

cannot return a function from a generic function #22011

Closed

Description

@Cergoo

Describe the bug

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 int) 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 fmap<O>(p TParser[[]u8], f TF[O]) TParser[O] {
	return fn [p, f] <O>(i []u8) !ParserResult[O] {
    	r:=p(i)!
    	return ParserResult[O]{i:r.i, o:f<O>(r.o)}
	}
}

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

  f := ParserResult[int] {
    i: [u8(1),u8(2)]
    o: 1
  }

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

  f1 := n(input) or { println('errr') return }
 println('hhhhh ${f1}')

  assert ['nn'] == ['ttt_', 'uuuu_', 'qqq_'] 

}

Reproduction Steps

v test parselona_test.v

Expected Behavior

complit test

Current Behavior

FAIL     0.000 ms /home/prof/projects/v/parselona/src/parselona_test.v
 compilation failed:
parselona.v:49:9: error: cannot use `fn ([]u8) !parselona.ParserResult[O]` as type `fn ([]u8) !parselona.ParserResult[int]` in return argument
   47 | 
   48 | fn fmap<O>(p TParser[[]u8], f TF[O]) TParser[O] {
   49 |     return fn [p, f] <O>(i []u8) !ParserResult[O] {
      |            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   50 |         r:=p(i)!
   51 |         return ParserResult[O]{i:r.i, o:f<O>(r.o)}

Possible Solution

for some reason type parametr not expansion

Additional Information/Context

No response

V version

V 0.4.7 3ca5bc3

Environment details (OS name and version, etc.)

Linux Mint 22

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

Activity

  1. added
    BugThis tag is applied to issues which reports bugs.
    on Aug 8, 2024
  2. Cergoo commented on Aug 8, 2024

    @Cergoo
    Author

    see #13253

  3. medvednikov commented on Aug 9, 2024

    @medvednikov
    Member

    A simple reproducible example:

    fn closure<T>(input T) fn () []T {
     return fn [input] <T>() []T {
      return [input, input]
     }
    }
    
    fn main() {
     f := closure(123)
     println(f())
    }
  4. medvednikov commented on Aug 9, 2024

    @medvednikov
    Member

    It's related to @yuyi98's fix #15529

  5. added
    Generics[T]Bugs/feature requests, that are related to the V generics.
    Unit: CheckerBugs/feature requests, that are related to the type checker.
    on Aug 9, 2024
  6. added 2 commits that reference this issue on Mar 11, 2026
    e89f766
    17fc15f
  7. added 2 commits that reference this issue on Apr 3, 2026
    4e32475
    d214c5e
  8. added a commit that references this issue on Apr 4, 2026
    da1f22c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugThis tag is applied to issues which reports bugs.Generics[T]Bugs/feature requests, that are related to the V generics.Unit: CheckerBugs/feature requests, that are related to the type checker.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions