FAQ
My patchwork readings lead me to believe I could test Perl 6's
tie-like feature with something like the below code, which I don't
expect to even compile, what with '???' in places. My question is:
am I on the right track? Obviously there are details I haven't nailed
down, and any guidance would be appreciated.

Thanks!

# Scalar is Any does Container does Order

class ScalarTester is Scalar {
has @.log;

multi STORE ( Any $thing ) {
@.log.push( STORE => $thing );
nextsame;
}
multi FETCH() {
@.log.push( 'FETCH' );
nextsame;
}
multi TEMP() {
@.log.push( 'TEMP' );
nextsame;
}

multi infix:<=> ( ??? ) {
@.log.push( 'infix:<=>' );
nextsame;
}
multi infix:<:=> ( ??? ) {
@.log.push( 'infix:<:=>' );
nextsame;
}
multi infix:<::=> ( ??? ) {
@.log.push( 'infix:<::=>' );
nextsame;
}
multi infix:<=:=> ( ??? ) {
@.log.push( 'infix:<=:=>' );
nextsame;
}

# since Scalar does Order, there are ~20 other methods to test.
}

my $t is ScalarTester;
is $t.log, (), 'log is empty';
lives_ok { $t = 'bughunt' }, 'can assign to test scalar';
is $t.log, ( 'STORE' => 'bughunt' ), 'log reflects assignment';
lives_ok { my $discard = $t }, 'can access test scalar';
is $t.log, ( 'STORE' => 'bughunt', 'FETCH' ), 'log reflects access';

Search Discussions

  • Moritz Lenz at Aug 2, 2009 at 5:11 pm
    Let's pick up this old mail before it gets completely warnocked ;-)

    For the record, this discussion only applies to scalar implementation
    types. For example for Arrays I expect things to work by overriding the
    method postcircumfix:<[ ]>.

    Also I'm far from being an expert on this field, so feel free to correct me.

    Kyle Hasselbacher wrote:
    My patchwork readings lead me to believe I could test Perl 6's
    tie-like feature with something like the below code, which I don't
    expect to even compile, what with '???' in places. My question is:
    am I on the right track? Obviously there are details I haven't nailed
    down, and any guidance would be appreciated.
    I think the you're on the right track, but I'm not sure about some details.
    For one it scares me to think that we do a full-blown multi dispatch on
    every FETCH/STORE operation, which might become quite expensive.
    # Scalar is Any does Container does Order

    class ScalarTester is Scalar {
    has @.log;

    multi STORE ( Any $thing ) {
    @.log.push( STORE => $thing );
    These non-operator routines should all be methods; 'multi' by itself
    defaults to subs, not methods (iirc).

    Also you'd have to quote STORE; otherwise it's interpreted as a named
    argument, which Array.push doesn't know how to handle.
    nextsame;
    }
    multi FETCH() {
    @.log.push( 'FETCH' );
    nextsame;
    }
    multi TEMP() {
    @.log.push( 'TEMP' );
    nextsame;
    }

    multi infix:<=> ( ??? ) {
    @.log.push( 'infix:<=>' );
    nextsame;
    }
    If we do FETCH/STORE, then the assignment will call the STORE method for
    you, so there's no reason to override infix:<=>.
    Also note that
    my $x = 3;
    foo($x);
    does dispatch based on the type of the stored thing (Int), not on the
    type of the Scalar container
    multi infix:<:=> ( ??? ) {
    @.log.push( 'infix:<:=>' );
    nextsame;
    }
    Binding replaces the container; the container implementation shouldn't
    see this. (Not sure though)
    multi infix:<::=> ( ??? ) {
    @.log.push( 'infix:<::=>' );
    nextsame;
    }
    Same here.
    multi infix:<=:=> ( ??? ) {
    @.log.push( 'infix:<=:=>' );
    nextsame;
    }
    I also don't expect =:= to be handled by the container; but rather as a
    very general sub that compares memory addresses at some point.
    # since Scalar does Order, there are ~20 other methods to test.
    }

    my $t is ScalarTester;
    is $t.log, (), 'log is empty';
    lives_ok { $t = 'bughunt' }, 'can assign to test scalar';
    is $t.log, ( 'STORE' => 'bughunt' ), 'log reflects assignment';
    lives_ok { my $discard = $t }, 'can access test scalar';
    is $t.log, ( 'STORE' => 'bughunt', 'FETCH' ), 'log reflects access';
    Cheers,
    Moritz
  • Brandon S. Allbery KF8NH at Aug 3, 2009 at 2:03 am

    On Aug 2, 2009, at 13:10 , Moritz Lenz wrote:
    Kyle Hasselbacher wrote:
    My patchwork readings lead me to believe I could test Perl 6's
    tie-like feature with something like the below code, which I don't
    expect to even compile, what with '???' in places. My question is:
    am I on the right track? Obviously there are details I haven't
    nailed
    down, and any guidance would be appreciated.
    I think the you're on the right track, but I'm not sure about some
    details.
    For one it scares me to think that we do a full-blown multi dispatch
    on
    every FETCH/STORE operation, which might become quite expensive.
    Last I heard the assumption was that the compiler would optimize way
    most such calls.

    --
    brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com
    system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu
    electrical and computer engineering, carnegie mellon university KF8NH
  • Aaron Sherman at Aug 4, 2009 at 4:57 pm

    On Sun, Aug 2, 2009 at 1:10 PM, Moritz Lenz wrote:

    Let's pick up this old mail before it gets completely warnocked ;-)

    For the record, this discussion only applies to scalar implementation
    types. For example for Arrays I expect things to work by overriding the
    method postcircumfix:<[ ]>.

    Really? What about:

    my ImplementationType @foo;
    @foo = 1..Inf;
  • Jonathan Worthington at Aug 8, 2009 at 11:07 pm

    Aaron Sherman wrote:
    On Sun, Aug 2, 2009 at 1:10 PM, Moritz Lenz wrote:

    Let's pick up this old mail before it gets completely warnocked ;-)

    For the record, this discussion only applies to scalar implementation
    types. For example for Arrays I expect things to work by overriding the
    method postcircumfix:<[ ]>.

    Really? What about:

    my ImplementationType @foo;
    @foo = 1..Inf;
    That would mean that you want an Array in which you could only store
    things of ImplementationType, rather than setting the implementation
    type of the array. Use 'is' for that (and the thing that you implement
    as an implementation type should do the Positional role and implement
    postcircumfix:<[ ]> unless you inherited one).

    Jonathan

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupperl6-language @
categoriesperl
postedJul 8, '09 at 3:58a
activeAug 8, '09 at 11:07p
posts5
users5
websiteperl6.org

People

Translate

site design / logo © 2021 Grokbase