Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
5 changed files
with
50 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
struct ParseResult[T] { | ||
result T | ||
rest string | ||
} | ||
|
||
type ParseFunction[T] = fn (string) !ParseResult[T] | ||
|
||
fn literal(l string) ParseFunction[string] { | ||
return fn [l] (input string) !ParseResult[string] { | ||
if !input.starts_with(l) { | ||
return error(input) | ||
} | ||
|
||
return ParseResult[string]{ | ||
result: l | ||
rest: input.all_after_first(l) | ||
} | ||
} | ||
} | ||
|
||
fn test_concrete_function_type_as_generic_type() { | ||
l_func := literal('start') | ||
val1 := l_func('start test') or { ParseResult[string]{} } | ||
val2 := l_func('test') or { ParseResult[string]{} } | ||
|
||
println(val1) | ||
assert val1.result == 'start' | ||
assert val1.rest == ' test' | ||
|
||
println(val2) | ||
assert val2.result == '' | ||
assert val2.rest == '' | ||
} |