Closed llagerlof closed 1 week ago
std
is embedded into the Amber executable. This weird documentation error should be fixed. You can handle it this way:
import { parse } from "std"
let a = "12"
let b = parse(a) failed {
echo "Variable `a` is not a number."
}
echo b + 12
@llagerlof fixed: https://docs.amber-lang.com/advanced_syntax/as_cast
@Ph0enixKM Thank you for the update. I have some notes to share.
Some notes:
After using failed
, I received a message indicating that I needed to install bc
. I installed it, and now it's working. It's strange that the lang didn't show this dependency error before adding the failed
to the code.
A question:
I'm not convinced I fully understood failed
. According to the docs:
Failed - the recommended way to handle failing, enabling you to write specific logic to run when a command fails.
Assuming failed
is for exception handling (although I could be wrong), when I run the new code (the example you provided above), the output is 12
. If I don't use failed
, that error occurs. I mean, if the parse
isn't failing, why doesn't it work without using failed
?
If I don't use
failed
, that error occurs. I mean, if theparse
isn't failing, why doesn't it work without usingfailed
?
The function doesn't know what if the bash command invoked inside will fail or not. Hence we have to handle the potential failure. The parse
function is a failing function (something can fail in the execution process) so we have to potentially handle the failure even if we know it's not going to fail. For the times when you know that it's not going to fail - you can use unsafe
keyword before the function call. This however results currently in undefined behaviour and is fixed in this PR: https://github.com/Ph0enixKM/Amber/pull/203. Stay tuned for that fix.
Thank you.
I have one suggestion (I don't want to add this as a new "suggestion issue", isn't so important):
When the code is using a failing function without failed
, the error message could be clearer.
Instead:
[user@pc test]$ amber program1.ab
[src/modules/condition/failed.rs:64:21] "FAILED" = "FAILED"
ERROR
Failed expression must be followed by a block or statement
at program1.ab:6:1
Could be:
[user@pc test]$ amber program1.ab
ERROR
The function 'parse' requires a 'failed' block or statement to handle errors
at program1.ab:6:1
But it's just a detail.
@llagerlof That's definitely a good point. I think that you can indeed create an issue for that!
I saw that exists the issue 208 for the same error. I got the code below from the docs (https://docs.amber-lang.com/advanced_syntax/as_cast):
Another thing (or related?), I am not sure about the
std
import
though. Do I need to download somestd
file or it is already embedded in Amber executable?