syntax icon indicating copy to clipboard operation
syntax copied to clipboard

Is there any proposal to add anything analogous to OCaml's `function` keyword or Haskell's `LambdaCase` extension?

Open alissa-tung opened this issue 4 years ago • 5 comments

In there

let optionBoolToBool = opt => {
  switch opt {
  | Some(trueOrFalse) => trueOrFalse
  | None => false
  }
}

seems redundant. Since there is no need for reference to opt.

alissa-tung avatar Jun 01 '21 14:06 alissa-tung

We used to have this in reason syntax with fun keyword. The reason behind removing this was reckless currying. @IwanKaramazow can explain better. I'm also missing this as i write more code in rescript but at the same time I don't like the fun syntax in reason. I was thinking about something like scala's case match expression.

 val f = {
   case Some(b) => b
   case None => false
}

so in rescript it would look something like this

let f = {
| Some(b) => b
| None => false
}

or

let f = switch {
| Some(b) => b
| None => false
}

but they are ambigious and may be hard to parse.

it may be good to think about it, there's also an issue when converting from ml/reason which can be solved if we introduce this feature #147 .

amiralies avatar Jun 07 '21 21:06 amiralies

I was curious if it's possible to repurpose switch for this lately. Hopefully, it could also work inline. Because sometimes instead of this:

switch
  value
  ->someOperation(a)
  ->someOtherOperation(b, c)
{
| Some(x) => ...
| None => ...
}

You really want this:

value
->someOperation(a)
->someOtherOperation(b, c)
->switch {
  | Some(x) => ...
  | None => ...
  }

hoichi avatar Jul 21 '21 11:07 hoichi

You could also consider adding an underscore lambda function:

let optionBoolToBool = switch _ {
  | Some(trueOrFalse) => trueOrFalse
  | None => false
}

Also useful for piped functions:

value
->someOperation(a)
->someOtherOperation(b, c)
->switch _ {
  | Some(x) => ...
  | None => ...
  }

noverby avatar Oct 17 '21 16:10 noverby

I like the switch _ syntax.

amiralies avatar Oct 17 '21 18:10 amiralies

I like the switch _ syntax.

Me too. It does not need an extra keyword and in contrast to the old fun keyword, the _ makes it clearer that there must be some shorthand magic going on.

fhammerschmidt avatar Oct 18 '21 09:10 fhammerschmidt

The rescript-lang/syntax repo is obsolete and will be archived soon. If this issue is still relevant, please reopen in the compiler repo (https://github.com/rescript-lang/rescript-compiler) or comment here to ask for it to be moved. Thank you for your contributions.

stale[bot] avatar May 28 '23 19:05 stale[bot]