Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: Definition of numerically equal and rationale for 'you' == 'me'

by moritz (Cardinal)
on Mar 02, 2012 at 07:21 UTC ( [id://957396]=note: print w/replies, xml ) Need Help??


in reply to Re: Definition of numerically equal and rationale for 'you' == 'me'
in thread Definition of numerically equal and rationale for 'you' == 'me'

And the root of that is that Perl 5 chooses to determine the operation by the verb (operator) instead of the type of the subjects, which is conveniently hidden behind the "scalar" which can be an integer, float, string or reference.

For example Javascript overloads + to mean either concatenation or addition, based on the type of the left operand, which forces people to write explicit coercions just to be save, ie 0 + a + b or '' + a + b. In Perl 5 that would just be $a + $b vs $a . $b. The same goes for the comparison operations: the operator determines the type of operation.

Replies are listed 'Best First'.
Re^3: Definition of numerically equal and rationale for 'you' == 'me'
by eyepopslikeamosquito (Archbishop) on Mar 02, 2012 at 11:01 UTC

    Yes, overloading + to mean either string concatenation or numeric addition is a language design blunder IMHO. I'm often saddened by how many language designers were seduced by this unfortunate misfeature. It explains why, for instance, you so often need ugly explicit coercion via the int function in Python and the to_i method in Ruby.

    The elegant dividend that Perl derives from its lack of operator ambiguity is clearly and tastefully described in Modern Perl in the "Numeric, String, and Boolean Context" section on page eight:

    In exchange for not having to declare (or at least track) explicitly what type of data a variable contains or a function produces, Perl offers specific type contexts that tell the compiler how to treat a given value during an operation ...

    The eq operator treats its operands as strings by enforcing string context on them. The == operator imposes numeric context ...

    Perl will do its best to coerce values to the proper type depending on the operators you use. Be sure to use the proper operator for the type of context you want.

      The elegant dividend that Perl derives from its lack of operator ambiguity

      Hm. If only that notion had been carried through to other operators like the bitwise boolean operators where it is all to frequent that you need to enforce the context by explicit coercions.

      Not a big criticism, but it still catches me out occasionally.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?

        Fully agreed. Other places where the context model has not been applied consistently:

        • The reverse operator does either string or list reversal depending on the calling context. Doesn't fit the pattern.
        • infix x does either string or list repetition, not by imposing context or by being sensitive to the outer context, but by introspecting the left-hand side. WRONG.

        Perl 6 has addressed those points, thought it took quite some time for some of them.

        Hm. If only that notion had been carried through to other operators...

        Indeed. It works so well with most of the operators that it stands out so strongly when it doesn't work... such as the smartmatch operator.

      The elegant dividend that Perl derives from its lack of operator ambiguity
      Really? Can you describe what &, |, ++ and <> do, without taking the value of their operands into account?
      Yes, overloading + to mean either string concatenation or numeric addition is a language design blunder IMHO. I'm often saddened by how many language designers were seduced by this unfortunate misfeature.
      I don't call it a misfeature. It's a design decision whether you allow multiple dispatch or not. There are pro and cons, and judgement should be made in context of the entire language. If your typing system is different, multiple dispatch makes more sense (perl6 will have multiple dispatch, although I don't know whether it will have it for build in operators). Perl5 doesn't have much of it, but do note the mentioned operators. And it allows operators to be overloaded. If I want to make + to concatenate strings, I can, quite simply:
      use 5.010; use overload '""' => sub {${$_[0]}}, '+' => sub {bless\do{my$o=${$_[$_[2]]}.$_[1-$_[2]]}}; BEGIN {overload::constant q => sub {bless \do {my $v = $_[0]}}} my $x = "Hello"; my $y = "world"; my $z = "" + uc $x; say $x + ", " + $y; say $z + $z; __END__ Hello, world HELLOHELLO
      (Implementation not complete)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://957396]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-03-29 00:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found