# New Ticket Created by  Richard A Hogaboom 
# Please include the string:  [perl #133246]
# in the subject line of all future correspondence about this issue. 
# <URL: https://rt.perl.org/Ticket/Display.html?id=133246 >


Attached is an executable file that demos a possible Perl 6 OO bug if 
attributes and method names are the same.  This includes some compile 
time errors and an infinite loop.

-- 
Richard A Hogaboom
16 Alprilla Farm Road
Hopkinton, MA 01748-1922
richard.hogab...@gmail.com
www.linkedin.com/in/richardhogaboom/
https://plus.google.com/+RichardHogaboom
https://github.com/rahogaboom
M508-330-3775

# Richard Hogaboom
# richard.hogab...@gmail.com

# Fedora 28
# Perl 6 2018.04
# uname -a
# Linux new-host-3 4.16.12-300.fc28.x86_64 #1 SMP Fri May 25 21:13:28 UTC 2018 
x86_64 x86_64 x86_64 GNU/Linux


# in Perl 6 OO if you have in a class an attribute name and a method name that 
are the same,
# some errors may result or not - including compile time errors and an infinite 
loop.  just
# comment in the various examples one by one to run them individually.

use v6;

=begin pod
# EXAMPLE 1
# this works

class Address {
    has $.var is rw;
}

my $addr = Address.new;
$addr.var = 'VAR';
say $addr.var;
=end pod

=begin pod
# EXAMPLE 2
# this works

class Address {
    has $.var is rw;

    multi method var() {
        return $!var;
    }

    multi method var(Str $v) {
        $!var = $v;
    }
}

my $addr = Address.new;
$addr.var('VAR');
say $addr.var();
=end pod

=begin pod
# EXAMPLE 3
# adding a method of the same name as the attribute gives:
# Cannot modify an immutable Str (Nil)
#   in block <unit> at t.p6 line 66

class Address {
    has $.var is rw;

    method var() {
    }
}

my $addr = Address.new;
$addr.var = 'VAR';
say $addr.var;

# is there a general prohibition on having an attribute and an explicit method 
of the same name?
=end pod

=begin pod
# EXAMPLE 4
# adding a return statement in the var() method gives a hang.
# using top shows the moar process at 100% CPU.

class Address {
    has $.var is rw;

    method var() {
        return $.var;
    }
}

my $addr = Address.new;
$addr.var = 'VAR';
say $addr.var;

# why does just adding a return statement eliminate the compile error? and hang?
# either this should not compile or a run time error should occur, not an 
infinite loop.
# adding 'multi' to 'method var() {' above does not help.
=end pod

=begin pod
# EXAMPLE 5
# changing only $.var to $!var in the var() method gives:
# Cannot assign to a readonly variable or a value
#   in block <unit> at t.p6 line 109

class Address {
    has $.var is rw;

    method var() {
        return $!var;
    }
}

my $addr = Address.new;
$addr.var = 'VAR';
say $addr.var;

# this eliminates the infinite loop , but returns to the previous readonly 
variable msg.
=end pod

# all this seems a caution against using the same name as an attribute and 
explicit method name,
# but shouldn't there be some type of prohibition/warning?

Reply via email to