Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Word 'if' interpreted as 'if()' function call. Parens not allowed

So I discovered that writing an if statement with parentheses in Perl 6 results in it throwing this error at me:

===SORRY!===
Word 'if' interpreted as 'if()' function call; please use whitespace instead of parens
at C:/test.p6:8
------> if<HERE>(True) {
Unexpected block in infix position (two terms in a row)
at C:/test.p6:8
------> if(True)<HERE> {

This makes me assume that there is some sort of if() function? However, creating and running a script with if(); in it produces the following compiler error:

===SORRY!===
Undeclared routine:
    if used at line 15

So like what's the deal?

I read here https://en.wikibooks.org/wiki/Perl_6_Programming/Control_Structures#if.2Funless that parens are optional but that seems to not to be the case for me.

My if statements do work without parens just wondering why it would stop me from using them or why it would think that if is a subroutine because of them.

EDIT: Well aren't I a bit daft... looks like I wasn't reading well enough at the link I linked which I assume is why you are confused. The link I linked points out the following which was basically what I was asking:

if($x > 5) {   # Calls subroutine "if"
}

if ($x > 5) {  # An if conditional
}

I've accepted the below answer as it does provide some insight.

like image 770
Phyreprooph Avatar asked Nov 21 '25 11:11

Phyreprooph


1 Answers

Are you sure you created a sub with the name 'if'? If so, (no pun intended), you get the keyword if you use a space after the literal 'if', otherwise you get your pre-declared function if you use a paren after the literal 'if' - i.e. if your use of the term looks like a function call - and you have declared such a function - it will call it;

use@localhost:~$ perl6
> sub if(Str $s) { say "if sub says: arg = $s" };
sub if (Str $s) { #`(Sub|95001528) ... }
> if "Hello World";
===SORRY!=== Error while compiling <unknown file>
Missing block
at <unknown file>:1
------> if "Hello World"⏏;
    expecting any of:
        block or pointy block
> if("Hello World");
if sub says: arg = Hello World
>
> if 12 < 16 { say "Excellent!" }
Excellent!
>

You can see above, I've declared a function called 'if'.

if "Hello World"; errors as the space means I'm using the keyword and therefore we have a syntax error in trying to use the if keyword.

if("Hello World") successfully calls the pre-declared function.

if 12 < 18 { say "Excellent!" } works correctly as the space means 'if' is interpreted as the keyword and this time there is no syntax error.

So again, are you sure you have (or better - can you paste here) your pre-declared 'if' function?

The reference for keywords and whitespace (which co-incidentally uses the keyword 'if' as an example!) is here: SO2 - Keywords and whitespace

like image 197
Marty Avatar answered Nov 23 '25 19:11

Marty