js-proposal-algebraic-effects icon indicating copy to clipboard operation
js-proposal-algebraic-effects copied to clipboard

Alternative Syntax

Open thysultan opened this issue 4 years ago • 7 comments

In an effort to reduce the amount of new keywords this would pose to introduce (effect, handle and resume), the following can serve as a possible alternative, piggybacking on the return keyword.

try {
	print(return < 'ask_name')
} return (type) {
	switch (type) {
		case 'ask_name': return > 'Arya Stark'
	}
}

Where < and > respectively represent the direction of data flow.

thysultan avatar Dec 24 '19 23:12 thysultan

Hi @thysultan, I am not so sure that would work, have you tried it?

both operators > and < are already implemented as binary operators, meaning that in the JS syntax they expect an expression in both lhs and rhs, e.g. expr < expr. Also, both cases above (fn call for print and case statement) expect an expression, and I am not so sure return < 'ask_name' and return > 'Arya Stark' would eval to an expr since return is a statement, and it will probably be ambiguous to treat it both as a statement and expression.

Finally, remember that JS also doesn't need ;, so when you write:

try {
} return (type) {
}

You are actually writing:

try {
} // no catch nor effect handler

return (type);

{
  // escoped block
}

luizperes avatar Dec 24 '19 23:12 luizperes

If nothing else, the syntax should be

try {
  // ...
} handle (effect) {
  // ...
}

So that there aren't any implicit/phantom variables in the code. The current syntax also makes effect conditionally a reserved word which is odd.

Similarly, everyone knows the catch deals with an error, but we still define/receive it in a declarative way.

try {
  // ...
} catch (err) {
  // err or whatever variable name i want to give it
}

lukeed avatar Dec 24 '19 23:12 lukeed

Totally agree @lukeed

luizperes avatar Dec 25 '19 00:12 luizperes

On the issue of return (type); that should be a non-issue since try... is always coupled with a secondary block?

Throwing other suggestions on the wall:

try {
	yield 'ask_name'
} catch* (type) {
	switch (type) {
		case 'ask_name': yield 'Arya Stark'
	}
}

The keyword catch in catch* will be sub-leased like function is for function*.

thysultan avatar Dec 25 '19 00:12 thysultan

@luizperes I don't think this is accurate.

try {
} // no catch nor effect handler

return (type);

{
  // escoped block
}

You can even try it in your browser. It doesn't work. try always has a secondary block catch or finally. so try/return would work, since it's not currently valid syntax. I don't think it's a good idea though because return is really confusing.

taylor-cedar avatar Dec 26 '19 15:12 taylor-cedar

True that @thysultan and @taylor-cedar

luizperes avatar Dec 30 '19 21:12 luizperes

@lukeed Conditional keyword are fine. We already have some, like async.

const async = 1; // OK

reverofevil avatar Jun 15 '21 00:06 reverofevil