motoko icon indicating copy to clipboard operation
motoko copied to clipboard

Optional `shared` keyword for public actor methods with caller?

Open rvanasa opened this issue 1 year ago • 2 comments

For simplicity and consistency, it could make sense for the shared keyword to always be optional for public actor methods. This would increase readability and reduce confusion about when the keyword is needed, e.g. when adding a caller to an existing method.

For example, instead of the following:

public shared ({ caller }) func abc() { ... };

It could make sense to allow:

public ({ caller }) func abc() { ... };

This is an observation from reviewing the Motoko training samples. Is there anything from a technical standpoint that prevents us from making this change?

rvanasa avatar Dec 03 '24 20:12 rvanasa

Might be worth trying but my hunch is that this ambiguous with the special case of

public when is just i.e. public

Though that is a arguably useless (since wouldn't declare anything to make public) and could be factored out of the grammar.

The relevant bit of the grammar describing these modifiers is:

<shared_pat_opt> ::= 'shared' ? <pat_plain>? <pat_plain>?

crusso avatar Dec 04 '24 09:12 crusso

That's a good point. I remember coming across this a few years ago.

Example snippet (runnable):

module {
  let msg = 123;
  public (msg)
}

For anyone reading this, note that the module in the above example does not actually include any public fields despite the public keyword.

Since the current grammar allows statements such as public 5 (but seemingly only at the end of an object/module/class), any usage of this is likely unintentional, so we may want to factor this out either way.

rvanasa avatar Dec 04 '24 17:12 rvanasa