Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dangling topic (or any other) variable does not fail

Tags:

syntax

raku

This works:

$_ =

say "hi";

That is, you can put any amount of whitespace between an assignment and stuff that's behind, it will simply ignore it. You can use any variable (with my) too. Effectively, $_ will be assigned the result of the say, which is True.

Is this surprising, but up to spec, or simply surprising?

like image 262
jjmerelo Avatar asked Jun 29 '19 08:06

jjmerelo


2 Answers

There may be any amount of whitespace either side of an operator. Thus:

say 1
    + 2

    + 3;

Is, so far as the compiler sees it, entirely the same as:

say 1 + 2 + 3;

Assignment (=) is just another operator, so also follows these rules.

Further, say is just a normal built-in subroutine, so it's just like:

my $answer = flip '24';
say $answer; # 42

Except with more whitespace:

my $answer = 

    flip '24';
say $answer; # 42

There are some places in Perl 6 where whitespace is significant, but the whitespace between infix operators is not one of them.

like image 67
Jonathan Worthington Avatar answered Nov 11 '22 19:11

Jonathan Worthington


TL;DR P6 syntax is freeform.

Dangling topic (or any other) variable does not fail

The issue you describe is a very general one. It's most definitely not merely about variable declaration/assignment!

In P6, parsing of a statement -- a single imperative unit ("do this") -- generally just keeps on going until it reaches an explicit statement separator -- ; -- that brings a statement to its end, just like the period (aka full stop) at the end of this English sentence.

you can put any amount of whitespace

Like many programming languages, standard P6 is generally freeform. That is to say, wherever some whitespace is valid, then generally any amount of whitespace -- horizontal and vertical whitespace -- is syntactically equivalent.

$_ =

say "hi";

The above works exactly as would be expected if someone is applying the freeform principle -- it's a single statement assigning the value of the say to the $_ variable.

Is this surprising, but up to spec, or simply surprising?

I like inventing (hopefully pithy) sayings. I just invented "Surprise follows surmise".

It's up to spec. I know to expect it. It doesn't surprise me.

If someone embraces the fact that P6 is generally freeform and has semicolon statement separation then it will, I predict, (eventually -- likely quickly) stop being surprising.


The foregoing is a direct answer to your question. See also Jonathan's answer for more along the same lines. Feel free to ignore the rest of this answer.

For the rest of this answer I use "freeform" to refer to P6's combination of freeform syntax, semicolon statement separation, and braced blocks ({...}).

The rest of this answer is in three sections:

  • P6 exceptions to freeform syntax

  • Freeform vs line oriented

  • Freeform and line oriented?

P6 exceptions to freeform syntax

@Larry concluded that intuition, aesthetics, convenience, and/or other factors justified an exception from a pure freeform syntax in standard P6 in a few cases.

Statements may omit the trailing semicolon if they:

  • Are the last statement in a source file, or in a block;

  • End in a block whose closing curly is followed by a newline (ignoring comments).

Thus none of the three statements below (the if and two says) need a closing semicolon:

if 42 {
  say 'no semicolon needed before the closing curly'
}    # no semicolon needed after the closing curly
say   'no semicolon needed if last statement in source file'

Sometimes this may not be what's wanted:

{ ... }   # the closing } ends the statement (block)
.()       # call is invoked on $_

One way to change that is to use parentheses:

({ ... })
.()       # calls the block on prior line

For some constructs spaces are either required or disallowed. For example, some postfixes must directly follow the values they apply to and some infixes must not. These are both syntax errors:

foo ++
foo«+»bar

Freeform vs line oriented

For some coding scenarios P6's freeform syntax is arguably a strong net positive, eg:

  • One liners can use blocks;

  • FP code is natural (one can write non-trivial closures);

  • More straight-forward editing/refactoring/copying/pasting.

But there are downsides:

  • The writing and reading overhead of freeform syntax -- semicolons and block braces.

  • Ignoring the intuition that presumably led you to post your question.

The latter is potent. All of the following could lead someone to think that the say in your example is part of a new statement that isn't a continuation of the $_ = statement:

  • The newline after the =;

  • The blank line after that;

  • The lack of an indent at the start of the say line relative to the $_ = line;

  • The nature of say (it might seem like say must be the start of a new statement).

An upshot of the above intuitions is that some programming languages adopt a "line-oriented" syntax rather than a freeform one, with the most famous being Python.1

Freeform and line oriented syntax

Some languages, eg Haskell, allow use of either line oriented or freeform syntax (at least for some language constructs).

P6 supports slangs, userland modules that mutate the language. Imagine a slang that supported both freeform and line-oriented code so:

  • Those learning P6 encountered more familiarity and less surprises as they learned the language's basics by focusing on either line-oriented or freeform code based on their preference;

  • Those familiar with P6 could write better code by using either line-oriented or freeform syntax.

At the risk of over-complicating things, imagine a slang that adopts not only line orientation but also the off-side rule that Python supports, and implements no strict; for untyped sigil-free variables (which drops declarators and sigils and promotes immutability). Here's a fragment of some code written in said imagined slang that I posted in a reddit comment a few weeks ago:

sub egg(bar)
  put bar

data = ["Iron man", "is", "Tony Stark"]
callbacks = []

Perhaps something like the above is too difficult to pull off? (I don't currently see why.)

Footnotes

1 The remainder of this section compares P6 and Python using the Wikipedia section on Programming language statements as our guide:

A statement separator is used to demarcate boundaries between two separate statements.

In P6 it's ; or the end of blocks.

In Python ; is available to separate statements. But it's primarily line-oriented.

Languages that interpret the end of line to be the end of a statement are called "line-oriented" languages.

In Python a line end terminates a statement unless the next line is suitably indented (in which case it's the start of an associated sub-block) or an explicit line continuation character appears at the end of a line.

P6 is not line-oriented. (At least, not standard P6. I'll posit a P6 slang at the end of this answer that supports both freeform and line-oriented syntax.)

"Line continuation" is a convention in line-oriented languages [that] allows a single statement to span more than just one line.

Python has line continuation features; see the Wikipedia article for details.

Standard P6 also has line continuation features despite not being line-oriented.2

2 P6 supports line continuation. Continuing with quotes from Wikipedia:

a newline normally results in a token being added to the token stream, unless line continuation is detected.

(A token is the smallest fragment of code -- beyond an individual character -- that's treated as an atomic unit by the parser.)

Standard P6 always assumes a token break if it encounters a newline with the sole exception of a string written across lines like this:

say
'The
 quick
 fox';

This will compile OK and display The, quick, and fox on three separate lines.

The equivalent in Python will generate a syntax error.

Backslash as last character of line

In P6 a backslash:

  • Cannot appear in the middle of a token;

  • Can be used to introduce whitespace in sourcecode that is ignored by the parser to avoid what would otherwise be a syntax error. For example:

        say @foo\
            »++
  • Is actually a more general concept of "unspace" that can be used anywhere within a line not just at the end:
        say @foo\            »++

Some form of inline comment serves as line continuation

This works:

say {;}#`( An embedded
comment ).signature

An embedded comment:

  • Cannot appear in the middle of a token;

  • Isn't as general as a backslash (say @foo#`[...]»++ doesn't work).

like image 35
raiph Avatar answered Nov 11 '22 21:11

raiph