Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rules for barewords

Tags:

raku

Barewords can be used at the left hand side of Pair declarations (this is not documented yet, I'm addressing this issue right now, but I want to get everything right). However, I have not found what is and what's not going to be considered a bareword key anywhere.

This seems to work

say (foo'bar-baz => 3); # OUTPUT: «foo'bar-baz => 3␤»

This does not

say (foo-3 => 3); # OUTPUT: «(exit code 1) ===SORRY!=== Error while compiling /tmp/jorTNuKH9V␤Undeclared routine:␤    foo used at line 1␤␤»

So it apparently follows the same syntax as the ordinary identifiers. Is that correct? Am I missing something here?

like image 527
jjmerelo Avatar asked Dec 30 '18 12:12

jjmerelo


1 Answers

There are no barewords in Perl 6 in the sense that they exist in Perl 5, and the term isn't used in Perl 6 at all.

There are two cases that we might call a "bare identifier":

  1. An identifier immediately followed by zero or more horizontal whitespace characters (\h*), followed by the characters =>. This takes the identifier on the left as a pair key, and the term parsed after the => as a pair value. This is an entirely syntactic decision; the existence of, for example, a sub or type with that identifier will not have any influence.
  2. An identifier followed by whitespace (or some other statement separator or terminator). If there is already a type of that name, then it is compiled into a reference to the type object. Otherwise, it will always be taken as a sub call. If no sub declaration of that name exists yet, it will be considered a call to a post-declared sub, and an error produced at CHECK-time if a sub with that name isn't later declared.

These two cases are only related in the sense that they are both cases of terms in the Perl 6 grammar, and that they both look to parse an identifier, which follow the standard rules linked in the question. Which wins is determined by Longest Token Matching semantics; the restriction that there may only be horizontal whitespace between the identifier and => exists to make sure that the identifier, whitespace, and => will together be counted as the declarative prefix, and so case 1 will always win over case 2.

like image 105
Jonathan Worthington Avatar answered Nov 15 '22 17:11

Jonathan Worthington