diff --git a/src/cli/test/fx_platform_test.zig b/src/cli/test/fx_platform_test.zig index 426a4804dd..6d6d4fe363 100644 --- a/src/cli/test/fx_platform_test.zig +++ b/src/cli/test/fx_platform_test.zig @@ -246,6 +246,7 @@ test "fx platform all_syntax_test.roc prints expected output" { "{ diff: 5, div: 2, div_trunc: 2, eq: False, gt: True, gteq: True, lt: False, lteq: False, neg: -10, neq: True, prod: 50, rem: 0, sum: 15 }\n" ++ "{ bool_and_keyword: False, bool_or_keyword: True, not_a: False }\n" ++ "\"One Two\"\n" ++ + "\"Three Four\"\n" ++ "The color is red.\n" ++ "78\n" ++ "Success\n" ++ @@ -256,13 +257,16 @@ test "fx platform all_syntax_test.roc prints expected output" { "This is an effectful function!\n" ++ "15\n" ++ "42\n" ++ - "NotOneTwo\n" ++ + "NotOneTwoNotFive\n" ++ "(\"Roc\", 1)\n" ++ "Builtin.List.[\"a\", \"b\"]\n" ++ + "(\"Roc\", 1, 1, \"Roc\")\n" ++ + "10\n" ++ "{ age: 31, name: \"Alice\" }\n" ++ "{ binary: 5, explicit_dec: 5, explicit_f32: 5, explicit_f64: 5, explicit_i128: 5, explicit_i16: 5, explicit_i32: 5, explicit_i64: 5, explicit_i8: 5, explicit_u128: 5, explicit_u16: 5, explicit_u32: 5, explicit_u64: 5, explicit_u8: 5, hex: 5, octal: 5, usage_based: 5 }\n" ++ "False\n" ++ - "99\n"; + "99\n" ++ + "\"12345.0\"\n"; try testing.expectEqualStrings(expected_stdout, run_result.stdout); try testing.expectEqualStrings("ROC DBG: 42\n", run_result.stderr); diff --git a/test/fx/all_syntax_test.roc b/test/fx/all_syntax_test.roc index f4707cd915..0574aa16ca 100644 --- a/test/fx/all_syntax_test.roc +++ b/test/fx/all_syntax_test.roc @@ -146,24 +146,23 @@ if_demo = |num| { two_line_if = if num == 2 "Two" - else - "NotTwo" + else + "NotTwo" - # thread 336429 panic - # with_curlies = - # if num == 5 { - # "Five" - # } else { - # "NotFive" - # } + with_curlies = + if num == 5 { + "Five" + } else { + "NotFive" + } # else if if num == 3 "Three" - else if num == 4 - "Four" - else - one_line_if.concat(two_line_if) + else if num == 4 + "Four" + else + one_line_if.concat(two_line_if).concat(with_curlies) } tuple_demo = @@ -174,16 +173,15 @@ tuple_demo = type_var : List(a) -> List(a) type_var = |lst| lst -# TODO issue #8647 -# destructuring = || { -# tup = ("Roc", 1) -# (str, num) = tup +destructuring = || { + tup = ("Roc", 1) + (str, num) = tup -# rec = { x: 1, y: str } # TODO implement tuple access with `.index` ? -# { x, y } = rec + rec = { x: 1, y: str } # TODO implement tuple access with `.index` ? + { x, y } = rec -# (str, num, x, y) -# } + (str, num, x, y) +} # TODO not sure if still planned for implementation # record_update = { @@ -241,6 +239,12 @@ early_return = |arg| { Str.count_utf8_bytes(first) } +my_concat = Str.concat + +# If you want to define a function that works for any type that has a specific method, you can use `where`: +stringify : a -> Str where [a.to_str : a -> Str] +stringify = |value| value.to_str() + main! = || { Stdout.line!("Hello, world!") StdoutAlias.line!("Hello, world! (using alias)") @@ -248,9 +252,13 @@ main! = || { Stdout.line!(Str.inspect(number_operators(10, 5))) print!(boolean_operators(Bool.True, Bool.False)) - # pizza operator (|>) is gone, we now have static dispatch instead: + # pizza operator (|>) is gone, we now have static dispatch instead. + # It allows you to call methods that are defined on the type (like `Animal.is_eq` above). print!("One".concat(" Two")) + # If you want a very similar style for a function that is not defined on the type but is in scope, you can use `->`: + print!("Three"->my_concat(" Four")) + Stdout.line!(simple_match(Red)) print!(match_list_patterns([1, 10])) Stdout.line!(match_tag_union_advanced(Ok({}))) @@ -275,12 +283,11 @@ main! = || { print!(type_var(["a", "b"])) - # print!(destructuring()) + print!(destructuring()) # print!(record_update) - # TODO Roc crashed: Error evaluating: TypeMismatch - # print!({ x: 10, y: 20 }.x) + print!({ x: 10, y: 20 }.x) print!(record_update_2({ name: "Alice", age: 30 })) @@ -298,6 +305,8 @@ main! = || { print!(early_return(Bool.False)) + print!(stringify(12345)) + # TODO Stdout.line!(readme); # Commented out so CI tests can pass