Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nil vs Any confusion (trap) #1134

Closed
briandfoy opened this issue Jan 13, 2017 · 9 comments
Closed

Nil vs Any confusion (trap) #1134

briandfoy opened this issue Jan 13, 2017 · 9 comments
Labels
docs Documentation issue (primary issue type) trap

Comments

@briandfoy
Copy link
Contributor

Which should it be? Here's what it does now.

grammar Number {
	rule TOP { \d+ }
	}

my @strings = '137', '137 ', ' 137 ';

for @strings -> $string {
	my $result = Number.parse( $string );
	say "Result is { $result.^name }";
	given $result {
		when Match { put "<$string> worked!" }
		when Any   { put "<$string> failed!" }
		}
	}

And the result:

Result is Match
<137> worked!
Result is Match
<137 > worked!
Result is Any
< 137 > failed!
@briandfoy briandfoy added the docs Documentation issue (primary issue type) label Jan 13, 2017
@ronaldxs
Copy link
Contributor

I think there may be some confusion about the behavior of Nil here. From the docs https://docs.perl6.org/type/Nil

When assigned to a container, the Nil value (but not any subclass of Nil) will attempt to revert the container to its default value; if no such default is declared, Perl 6 assumes Any.

grammar G { token TOP { a+ } };
my Nil $n = G.parse("zz");
say $n.WHAT;
my $result = G.parse("zz");
say $result.WHAT;

prints

Nil
(Any)

@jonathanstowe
Copy link
Contributor

It does return Nil, what you are seeing there is the effect of assigning Nil to an Any typed variable:

grammar G { token TOP { a+ } };
say G.parse("zz").WHAT; #  -> Nil

@briandfoy
Copy link
Contributor Author

Duh. I'm an idiot. Thanks,

@AlexDaniel
Copy link
Member

AlexDaniel commented Jan 14, 2017

Do we have it explained somewhere in the docs? If yes, where?

For example, maybe it deserves a section in traps?

@AlexDaniel AlexDaniel reopened this Jan 14, 2017
@jonathanstowe
Copy link
Contributor

Though it's not specific to Grammar, it's just what happens when you assign Nil to a variable, I don't know what happens under the hood, but the nett effect is that the variable is the same type object that it would have been without the assignment, It thus allows you to unset a variable without knowing what type it is.

I'd go with making sure this is documented clearly in the variables page and make sure that Nil is searchable with a clear explanation somewhere.

@AlexDaniel AlexDaniel changed the title Grammar::parse() says it returns Nil on Failure, but actually returns Any Nil vs Any confusion (trap) Sep 3, 2017
@AlexDaniel
Copy link
Member

AlexDaniel commented Sep 3, 2017

I don't know where this could be documented, but I've seen this confusion a couple of times.
Another interesting point is that arrays can contain Nils:

my @a = 4, 8, 15, 16;
@a[2] = Nil;
say @a; # OUTPUT: «[4 8 (Any) 16]␤»
@a[3] := Nil;
say @a; # OUTPUT: «[4 8 (Any) Nil]␤»

Let's document it as a trap perhaps? (i.e. not understand what Any or Nil do)

@tisonkun
Copy link
Member

tisonkun commented Nov 4, 2017

https://irclog.perlgeek.de/perl6/2017-10-30#i_15374966

14:38 | lizmat | m: my %h is default(Nil) = a => Nil; dd %h
14:38 | camelia | rakudo-moar 497e0582e: OUTPUT: «Hash %h = {:a(Nil)}␤»
14:38 | lizmat | jdv79: ^^^
14:38 | jdv79 | oh, that makes sense
14:38 |            | thanks
14:40 | geekosaur | the core issue is that, in an assignment, Nil usually means use the default value for the type. so you need the default to also be Nil

@tisonkun
Copy link
Member

tisonkun commented Nov 8, 2017

Text about it can be found at https://docs.perl6.org/type/Nil#index-entry-Nil_assignment

@JJ
Copy link
Contributor

JJ commented Mar 30, 2018

@briandfoy , would the text linked by @W4anD0eR96 be enough?

@JJ JJ added the TPF Grant label Mar 30, 2018
@JJ JJ removed the JJ TPF Grant label May 14, 2018
@JJ JJ closed this as completed in 92d045b May 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation issue (primary issue type) trap
Projects
None yet
Development

No branches or pull requests

6 participants