Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between .Bool, .so, ? and so

I’m trying to figure out what the differences are between the above-mentioned routines, and if statements like

say $y.Bool;
say $y.so;
say ? $y;
say so $y;

would ever produce a different result.

So far the only difference that is apparent to me is that ? has a higher precedence than so. .Bool and .so seem to be completely synonymous. Is that correct and (practically speaking) the full story?

like image 434
ozzy Avatar asked Jun 23 '19 12:06

ozzy


1 Answers

What I've done to answer your question is to spelunk the Rakudo compiler source code.

As you note, one aspect that differs between the prefixes is parsing differences. The variations have different precedences and so is alphabetic whereas ? is punctuation. To see the precise code controlling this parsing, view Rakudo's Grammar.nqp and search within that page for prefix:sym<...> where the ... is ?, so, etc. It looks like ternary (... ?? ... !! ...) turns into an if. I see that none of these tokens have correspondingly named Actions.pm6 methods. As a somewhat wild guess perhaps the code generation that corresponds to them is handled by this part of method EXPR. (Anyone know, or care to follow the instructions in this blog post to find out?)

The definitions in Bool.pm6 and Mu.pm6 show that:

  • In Mu.pm6 the method .Bool returns False for an undefined object and .defined otherwise. In turn .defined returns False for an undefined object and True otherwise. So these are the default.

  • .defined is documented as overridden in two built in classes and .Bool in 19.

  • so, .so, and ? all call the same code that defers to Bool / .Bool. In theory classes/modules could override these instead of, or as well, as overriding .Bool or .defined, but I can't see why anyone would ever do that either in the built in classes/modules or userland ones.

  • not and ! are the same (except that use of ! with :exists dies) and both turn into calls to nqp::hllbool(nqp::not_i(nqp::istrue(...))). I presume the primary reason they don't go through the usual .Bool route is to avoid marking handling of Failures.

  • There are .so and .not methods defined in Mu.pm6. They just call .Bool.

  • There are boolean bitwise operators that include a ?. They are far adrift from your question but their code is included in the links above.

like image 132
raiph Avatar answered Oct 08 '22 12:10

raiph