mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-04 20:28:02 +00:00
Properly type constrain all function types
This commit is contained in:
parent
20ba4a92de
commit
f857872903
4 changed files with 320 additions and 79 deletions
|
@ -108,6 +108,24 @@ fn early_return_solo() {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))]
|
||||
fn early_return_solo_annotated() {
|
||||
assert_evals_to!(
|
||||
r#"
|
||||
identity : Str -> Str
|
||||
identity = \x ->
|
||||
return x
|
||||
|
||||
identity "abc"
|
||||
"#,
|
||||
RocStr::from("abc"),
|
||||
RocStr,
|
||||
identity,
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))]
|
||||
fn early_return_annotated_function() {
|
||||
|
@ -149,3 +167,99 @@ fn early_return_annotated_function() {
|
|||
RocList<RocStr>
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))]
|
||||
fn early_return_nested_annotated_function() {
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
app "test" provides [main] to "./platform"
|
||||
|
||||
validateInput : Str -> Result U64 [InvalidNumStr, LessThanFive]
|
||||
validateInput = \str ->
|
||||
failIfLessThanFive : U64 -> Result {} [LessThanFive]
|
||||
failIfLessThanFive = \n ->
|
||||
if n < 5 then
|
||||
Err LessThanFive
|
||||
else
|
||||
Ok {}
|
||||
|
||||
num = try Str.toU64 str
|
||||
|
||||
when failIfLessThanFive num is
|
||||
Err err ->
|
||||
return Err err
|
||||
|
||||
Ok {} ->
|
||||
Ok num
|
||||
|
||||
main : List Str
|
||||
main =
|
||||
["abc", "3", "7"]
|
||||
|> List.map validateInput
|
||||
|> List.map Inspect.toStr
|
||||
"#
|
||||
),
|
||||
RocList::from_slice(&[
|
||||
RocStr::from("(Err InvalidNumStr)"),
|
||||
RocStr::from("(Err LessThanFive)"),
|
||||
RocStr::from("(Ok 7)")
|
||||
]),
|
||||
RocList<RocStr>
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))]
|
||||
fn early_return_annotated_recursive_function() {
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
app "test" provides [main] to "./platform"
|
||||
|
||||
mightCallSecond : U64 -> Result U64 _
|
||||
mightCallSecond = \num ->
|
||||
nextNum =
|
||||
if num < 5 then
|
||||
return Err LessThanFive
|
||||
else
|
||||
num - 1
|
||||
|
||||
mightCallFirst nextNum
|
||||
|
||||
mightCallFirst : U64 -> Result U64 _
|
||||
mightCallFirst = \num ->
|
||||
nextNum =
|
||||
if num < 10 then
|
||||
return Err LessThanTen
|
||||
else
|
||||
num * 2
|
||||
|
||||
if nextNum > 25 then
|
||||
Ok nextNum
|
||||
else
|
||||
mightCallSecond nextNum
|
||||
|
||||
main : List Str
|
||||
main =
|
||||
[
|
||||
mightCallSecond 3,
|
||||
mightCallSecond 7,
|
||||
mightCallSecond 20,
|
||||
mightCallFirst 7,
|
||||
mightCallFirst 15,
|
||||
]
|
||||
|> List.map Inspect.toStr
|
||||
"#
|
||||
),
|
||||
RocList::from_slice(&[
|
||||
RocStr::from("(Err LessThanFive)"),
|
||||
RocStr::from("(Err LessThanTen)"),
|
||||
RocStr::from("(Ok 38)"),
|
||||
RocStr::from("(Err LessThanTen)"),
|
||||
RocStr::from("(Ok 30)")
|
||||
]),
|
||||
RocList<RocStr>
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue