develooper Front page | perl.perl6.language | Postings from April 2001

Re: Thinking about scalars

Thread Previous
From:
Michael G Schwern
Date:
April 24, 2001 02:12
Subject:
Re: Thinking about scalars
Message ID:
20010424101216.B525@blackrider.blackstar.co.uk
This may or may not be related to Dave's post, but I just remembered
why I don't like a dual-natured +.  I'm sorry if this was gone through
before.

Consider the following...

        sub foo { $_[0] + $_[1] }

What do the following return?

        foo(1,1);               obviously 2
        foo("this", "that")     obvious "thisthat"

Those are simple enough.

        foo("1", 1);            is that "11" or 2?
        foo(undef, 1);          1 or "1"?

Those can be solved by definining precidence, but its still a bit
arbitrary.

        foo($foo, $bar);        who knows?

You can't know without examining the surrounding code.  I might be the
only one this bothers, dunno.

        $foo = 42;  print 42;
        $bar = 23;  print 23;  foo($foo, $bar);         "4223" or 65?

$foo and $bar are initially set to numbers, but then they're converted
to strings by the print operation.  So does + treat them as strings or
numbers?

        $foo = read_num_from_file;    $bar = read_num_from_file;
        foo($foo, $bar);        concatinates as read_num_from_file() probably
                                returns strings unless its author is very
                                careful

        sub concat { $_[0] + $_[1] };
        $foo = read_word_from_file;     $bar = read_word_from_file;
        concat($foo, $bar);     the user obviously intends for this to
                                concatinate, not add.
                                
Here's probably the cannonical confusing case.  read_num_from_file()
returns a number read in from some file.  That's probably going to be
pulled in as a string (that happens to look like a number).  What
would + do with that?  If you follow the rule "Perl will add two
strings if they both look like numbers" then concat() might do
confusing things.

Solve the final example and you can use + as both addition and
concatination.  If you can't, dont.


The reason why this works in languages like Java and C++ is because
they're typed languages.  There's little ambiguity about whether
something is a string or a number, you can tell just by its type.
This leads to all sorts of annoyances, but what it does let them do is
use the same operator for addition and concatination.



On Mon, Apr 23, 2001 at 03:41:49PM -0700, Dave wrote:
> The contention that '+' should be overloaded is based in machine
> details, expressed in other languages through 'types'.  In this
> model of the world, a 'string' is fundamentally different from a
> 'number', and as such each defines a specific context in which the
> operator exists: (string) + (string) or (number) + (number), or some
> horrid abberation thereof, which aught not to be.

-- 

Michael G. Schwern   <schwern@pobox.com>    http://www.pobox.com/~schwern/
Perl6 Quality Assurance     <perl-qa@perl.org>	     Kwalitee Is Job One
AY!  The ground beef, she is burning my groin!
	http://sluggy.com/d/990105.html

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About