Amber icon indicating copy to clipboard operation
Amber copied to clipboard

[BUG] function with no return generate useless bash code

Open Mte90 opened this issue 1 year ago • 4 comments

Take this code:

fun something() {
    echo "yes"
}

something()

It will generated:

#!/usr/bin/env bash
# Written in [Amber](https://amber-lang.com/)
# version: 0.3.4-alpha
# date: 2024-07-30 12:41:18
function something__0_v0 {
    echo "yes"
}
something__0_v0
__AF_something0_v0__5_1="$__AF_something0_v0"
echo "$__AF_something0_v0__5_1" >/dev/null 2>&1

As we can see the function doesn't have any return so the last 2 lines are not needed.

Mte90 avatar Jul 30 '24 10:07 Mte90

The code involved is https://github.com/amber-lang/amber/blob/435d91c4aabce2bd016db77313b221eec38a6a65/src/modules/function/invocation.rs#L158

Basically print that also if the function doesn't have any return.

Mte90 avatar Jul 30 '24 10:07 Mte90

i dont know if we can do this without breaking apart the whole compiler. its kind of like google translate, really

b1ek avatar Jul 31 '24 00:07 b1ek

Yeah this is something for @Ph0enixKM that knows how works.

Mte90 avatar Jul 31 '24 08:07 Mte90

I think that this is the moment when we should add that optimization I explained before:

https://www.youtube.com/watch?v=_caXIMxrYj8&feature=youtu.be

This would solve this issue too.

Ph0enixKM avatar Aug 08 '24 15:08 Ph0enixKM

This is fixed by #706. The amber code is now compiled to:

#!/usr/bin/env bash
# Written in [Amber](https://amber-lang.com/)
# version: 0.4.0-alpha-23-g06c34fe
# date: 2025-06-22 07:47:51
something__0_v0() {
    echo "yes"
}
something__0_v0

lens0021 avatar Jun 21 '25 22:06 lens0021