TropicSapling/triforce

(Implicit) Currying?

Opened this issue · 0 comments

Would it be a good idea to make P+ functions curried by default, and would it even be possible?

Pros:

Cons:

  • Might be hard to implement (if even possible)
  • Could cause confusing error msgs when you give too few args to a function
  • Might decrease performance if not properly implemented
  • May cause confusion
  • Can already be done explicitly with func and in another way with macro func

Note that this wouldn't really affect the syntax especially much, since Haskell-style syntax is pretty much incompatible with P+ anyway so it's better to make currying "hidden". Here's an example program:

func (a) times (b) -> int {
    a * b
}

let double = 2 times;
let twenty = double 10;
println twenty; // 20

let times4 = times 4;
let forty = 10 times4;
println forty; // 40

This would be the equivalent code without (implicit) currying:

func (a) times -> func (b) -> int {
    func (b) -> int {
        a * b
    }
}

func times (a) -> func (b) _ -> int {
    func (b) _ -> int { // also just discovered this issue; it may be impossible to have anonymous functions in some situations because the compiler needs to know where the parameters are
        a * b
    }
}

let double = 2 times;
let twenty = double 10;
println twenty; // 20

let times4 = times 4;
let forty = 10 times4;
println forty; // 40

As you can see, the second version can be a lot more verbose. It gets slightly less verbose using macro func though:

macro func (a) times {
    a *
}

macro func times (a) {
    * a
}

macro double = 2 times; // note that we need to use 'macro' here instead of 'let' because else the compiler would complain about 'a *' missing a second argument.
let twenty = double 10;
println twenty; // 20

macro times4 = times 4;
let forty = 10 times4;
println forty; // 40