contour-terminal / endo

7 stars 0 forks source link

support function syntax #14

Open christianparpart opened 6 months ago

christianparpart commented 6 months ago

Now it's getting interesting. Because we first need to figure out how we want to be able to declare a function.

I've let go of the idea to be bash-compatible, i.e. I do understand why fish shell decided to deviate here and there.

I very much like the way of how F# declares a function as well as strong typing. I think it might make sense to embrace strong typing in a shell as well. Even bash kind-of(?) does know about whether it deals with ints (for arithmetic) for example. Going explicit with types should improve readability and maintainability of scripts as well.

Syntax proposal by example:

// Define function f, receiving 3 string parameters, and returning one string
let f (a, b, c): str =
    echo "a: $a, b: $b, c: $c"
    return "($a,$b,$c)"

let y = f "foo" 3.1415 $VAR      // prints and returns something into $y
echo $y                          // prints "(foo,3.1415,VALUE)"

let g a b: str =
    echo "a is: $a, and b is: $b"
    if a = b then
        return "same"
    else
        return "not same"

let has_var name: bool =
    return (getenv $name) != ""

// single-line function definition
let has_var name: bool = (getenv $name) != ""
Yaraslaut commented 6 months ago

Also, nice addition would be to have creation in-place for example during piping, something like find -name "*.cpp" | cat $1 cp $1 path/$1 would cat files and copy them