Skip to content

support function syntax #14

@christianparpart

Description

@christianparpart

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) != ""

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions