dark icon indicating copy to clipboard operation
dark copied to clipboard

Allow versioned infix functions

Open pbiggar opened this issue 3 years ago • 3 comments
trafficstars

Right now, infix functions are stuck. We have code for Int::mod_v1 but we can't enable it because the % operator is for Int::mod_v0, and we don't know what to do with that.

I came up with a solution. Instead of % being literally Int::mod_v0, it would be an editor shortcut and nice display for whatever function it represents at the time, and we would also store what function it represents.

That means changing Expr from

| EBinOp(id, op, arg1, arg2)
| EBinOp(id, op, fnName, arg1, arg2)

Then we keep a list of the current op -> fnname mappings. If an EBinOp uses the old representation (eg, it uses % for Int::mod_v0 but we've updated it to Int::mod_v1), we convert the EBinOp to a EFnCall.

pbiggar avatar Jul 13 '22 16:07 pbiggar

I like the idea!

We could store the op->fnname mappings in the backend and expose them. Not sure any strategical advantage, but "feels right."


That means changing Expr from

| EBinOp(id, op, arg1, arg2)

to

| EBinOp(id, op, fnName, arg1, arg2)

I'm not sure why we'd need EBinOp any more, actually.

If we store these as normal fn calls, and the code recognizes that some fns can be displayed as bin ops, we should be able to kill EBinOp in favor of "back-translating" them into "bin ops" in the client?

Client code basically thinks: "if this is a fn call with 2 operands, and it's in the 'known binops' list, show the code with the bin op rather than the full fn name"

What am I missing?

StachuDotNet avatar Jul 13 '22 16:07 StachuDotNet

Users currently have the choice to call Int::add instead of +, so this allows to distinguish between them.

pbiggar avatar Jul 13 '22 23:07 pbiggar

@pbiggar @StachuDotNet I'm not sure that I understand all the nuances of the issue, but I would like to offer a couple approaches on review:

  1. Always using Result with automatically binding and unwrapping in last evaluation
  2. Int::add and + always return Result, but for + editor always unwrap a Result with showing a value or an error message.
  3. Using two different infix operator versions - with Result and unwrapped. E.g. 2 + 3 = Ok 5 but 2 +~ 3 = 5 or vice versa 2 + 3 = 5 and 2 +~ 3 = Ok 5

ditansu avatar Apr 15 '23 23:04 ditansu