Home > Raku > Introspective labeling

Introspective labeling

IRC is a good place to find answers. Often I find the questions found there to be even more enlightening.

12:26 < notandinus> do other languages have this Label thing? apart from Perl and Raku, i don't seem to find it for python, nim
12:28 < gfldex> notandinus: C/C++ does, but with different semantics (read: you can mess up the stack).
12:30 < sortiz> Javascript also, with semantics similar to Raku.
12:32 < El_Che> isn't label extremely commom to break out of the correct loop?
12:34 < El_Che> Besides python, all other languages probably have it
12:34 < sortiz> But only in Raku they are also objects, like almost everything.
12:35 < notandinus> is it being object better?

So are objects better? That depends on the needs and the attributes or methods provided. Let’s have a look what Label can do for us.

LABEL: Nil;
say LABEL.^attributes;
# OUTPUT: (Str $!name Str $!file Int $!line Str $!prematch Str $!postmatch)
          (new name goto leave Int next redo last gist BUILDALL)

So we get the label name, what means we can label things with them. There are also attributes with the line number and file providing a location for human consumption. With .Str and .gist we get some strings.

Label<94051339353632>
Label<LABEL>(at /home/dex/tmp/tmp2.raku:99, ' ~ self.line } });

⏏LABEL: Nil;

say LABEL.^a')

We do get the line number and file but in a piece of text that we don’t want to parse. Stringification is not helpful and there are no accessors for $!line and $!file. Not to worry, Lord Hanuman is with us.

use MONKEY-TYPING;
augment class Label {
    method Str { $!file ~ ':' ~ $!line }
}

sub documented {
    BEWARE: sub hax { 0x1ee7 }

    warn „Beware of the leet hax at {BEWARE} !“;
}

documented;
# OUTPUT: Beware of the leet hax at /home/dex/tmp/tmp2.raku:113 !
            in sub documented at /home/dex/tmp/tmp2.raku line 115

The class Label feels unfinished to me. With better .Str or access to all attributes it could be a nice tool to refer to spots in source code. This in turn could lead to better error messages and allow folk more creative then me to come up with something clever. I doubt it would be a hard change. I shall query Rakudo’s source code the coming week.

Categories: Raku
  1. No comments yet.
  1. January 18, 2021 at 17:24

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: