Skip to content

Commit

Permalink
Work around lexical import limits hit by T::P::Widget
Browse files Browse the repository at this point in the history
Use jnthn++'s suggestion* and vector cross-package reference to the T
singleton through PROCESS::<$TERMINAL>.

* https://irclog.perlgeek.de/perl6-dev/2016-12-20#i_13775229
  • Loading branch information
Geoffrey Broadwell committed Dec 20, 2016
1 parent b4f4abc commit 5b2452f
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/Terminal/Print.pm6
Expand Up @@ -295,7 +295,7 @@ but the following features seemed to fulfill a 'necessary minimum' set of golfin
=end Golfing

our $T = Terminal::Print.new;
our $T = PROCESS::<$TERMINAL> = Terminal::Print.new;

sub draw(Callable $block) is export {
my $drawn-promise = Promise.new;
Expand Down
4 changes: 2 additions & 2 deletions lib/Terminal/Print/Widget.pm6
Expand Up @@ -35,15 +35,15 @@ class Terminal::Print::Widget {
given $!parent {
when Terminal::Print::Grid { $_ }
when Terminal::Print::Widget { .grid }
default { $Terminal::Print::T.current-grid }
default { $*TERMINAL.current-grid }
}
}

#| Composite this widget onto a target grid, optionally printing to screen
# For now, simply copies widget contents (effects such as alpha blend NYI).
# Default behavior is to print iff the widget's parent is the screen grid.
method composite(Terminal::Print::Grid :$to = self.target-grid,
Bool :$print = $to === $Terminal::Print::T.current-grid) {
Bool :$print = $to === $*TERMINAL.current-grid) {

# Skip copy if target is own backing grid, e.g. screen's root widget
if $to === $!grid {
Expand Down

4 comments on commit 5b2452f

@ab5tract
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a really useful demonstration of the use of dynamic variables!

@Xliff
Copy link
Contributor

@Xliff Xliff commented on 5b2452f Dec 25, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So how does this work?

Does PROCESS::<$TERMINAL> become $*TERMINAL?

I've been having issues understanding how Dynamic Variables are created.

@japhb
Copy link
Collaborator

@japhb japhb commented on 5b2452f Dec 25, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Xliff: Essentially, in any scope you can declare my $*foo; and then any other routine (or chain of routines) you call from that scope will be able to see $*foo as well. So if you define:

sub foo() { my $*foo = 5; bar() }
sub bar() { say $*foo }
foo();    # print '5'

it will Just Work. The way that this plays out in practice is that when a dynamic variable is declared, it is installed in the local lexical scope; when it is referenced, the runtime will do a search through the call stack (starting at the local scope) searching for that dynamic variable. If it can't find it in any lexical scope, it then strips the * twigil and searches next in the interpreter globals (GLOBAL::) and then finally the process globals (PROCESS::). Failing that, it will give up and error.

@Xliff
Copy link
Contributor

@Xliff Xliff commented on 5b2452f Dec 25, 2016 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.