| 1 | struct ParseResult[T] { |
| 2 | result T |
| 3 | rest string |
| 4 | } |
| 5 | |
| 6 | type ParseFunction[T] = fn (string) !ParseResult[T] |
| 7 | |
| 8 | fn literal(l string) ParseFunction[string] { |
| 9 | return fn [l] (input string) !ParseResult[string] { |
| 10 | if !input.starts_with(l) { |
| 11 | return error(input) |
| 12 | } |
| 13 | return ParseResult[string]{ |
| 14 | result: l |
| 15 | rest: input.all_after_first(l) |
| 16 | } |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | fn wrap_parser[T](parser ParseFunction[T]) ParseFunction[T] { |
| 21 | return fn [parser] [T](input string) !ParseResult[T] { |
| 22 | return parser[T](input) |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | fn test_function_parameter_inside_generic_closure() { |
| 27 | parser := literal('ab') |
| 28 | wrapped := wrap_parser[string](parser) |
| 29 | result := wrapped('abcd')! |
| 30 | assert result.result == 'ab' |
| 31 | assert result.rest == 'cd' |
| 32 | } |
| 33 | |