1. 16
  1. 6

    That’s not as exciting inside as the title makes it sound. Should be reproducible in any language that allows you to run code very early in compilation phase. You just inject a syntax error after checking for your triggering condition.

    C preprocessor is a good example that will allow you to replicate this easily.

    1. 12

      Yeah, although this one does use an interestingly “Perlish” way to break.

      In the Friday case where f is prototyped with (), you have f() / 1 followed by a comment.

      In the non-Friday case where f has no prototype, you have f( /1;#/ +, where /1;#/ is a regex literal, and then we’re trying to + something and… nope, EOF.

      So it’s dependent on / being both a binop when the parser is looking for an operator, and a quotelike when the parser is looking for a term, and also dependent on the fun ways that prototypes influence the parsing of function calls.

      1. 1

        So… anything with full macros, too, since macros are just compiler extensions.

      2. 2

        Can you do something similar with eval and barewords? Hmmm….looks like the colons throw things off:

        BEGIN {
            eval scalar localtime
        sub Wed {
            my @bits = @_;
            say("bits are [@_]");
        $ perl friday.pm 
        Number found where operator expected at (eval 1) line 1, near "16 20"
                (Missing operator before  20?)
        Number found where operator expected at (eval 1) line 1, near "18 2022"
                (Missing operator before  2022?)
        1. 1

          C++ can do it (conditional syntax errors) with templates, constexpr and of course macros (but is_friday() would not be implementable as a contant expression, since it requires IO, unless you count compiler arguments).

          SFINAE would be the template version. Here is a constexpr version:

          char* weekday() {
              if constexpr(is_friday()) {
                  return "friday";
              // Control reaches end of non-void function.
          1. 1

            Sorry, I don’t understand that reply. So does this code work as expected? Or can it be made to work as expected? How does is_friday look?

            1. 1

              (Edited it for clarity.) I just mean conditional syntax errror – is_friday() is just any constant expression in my example. A correct implementation of is_friday() would require passing information through compiler arguments, which isn’t the impressive part.

              constexpr bool is_friday() { return IS_FRIDAY; }