Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^7: Strong typing and Type Safety.A multilanguage approach (implicit)

by chromatic (Archbishop)
on Nov 22, 2010 at 00:12 UTC ( [id://872840]=note: print w/replies, xml ) Need Help??


in reply to Re^6: Strong typing and Type Safety.A multilanguage approach (implicit)
in thread Strong typing and Type Safety.A multilanguage approach

1+$duration is not an explicit type conversion, it is explicit addition that implies a type conversion, if required.

+ is a monomorphic operator. So is .. Perl values are polymorphic.

Arguing that operator-enforced coercions in Perl are implicit is silly nonsense; to do so you must assume a priori that values should be monomorphic.

I can write 1+$duration with no intention of doing a conversion.

You can also write $string + $duration with no intention of performing addition, but that has nothing to do with typing and everything to do with you writing buggy (or poorly understood) code.

Replies are listed 'Best First'.
Re^8: Strong typing and Type Safety.A multilanguage approach (implicit)
by ikegami (Patriarch) on Nov 22, 2010 at 01:59 UTC

    Implicit doesn't mean you don't know about it. It means you don't have to take any action for it to happen. You don't convert the value before passing it to the addition operator, so any conversion that occurs is implicit.

    Personally, I don't really think about it as conversion (whether implicit or explicit) since conversion implies loss of the original value. I think of the addition operator as "getting the numeric value of the operand". For example, the numeric value of a scalar is "123" whether "123" is stored in the IV slot or "123" is stored in the PV slot, and the numeric value of a scalar is zero whether "0" is stored in the IV slot or "abc" is stored in the PV slot.

      You don't convert the value before passing it to the addition operator, so any conversion that occurs is implicit.

      You can argue it that way, but what's the difference between:

      $z = add( $x->to_int(), $y->to_int() );

      ... and:

      $z = $x + $y

      ... given that the + operator always performs numeric addition? If it's the presence of explicit type name hints, your argument gets awkward in the presence of type inference.

      ... conversion implies loss of the original value.

      I agree; that's why I try to prefer the term "coercion".

        [ Flawed premise: I was working from an incorrect definition of coercion. ]

        I agree; that's why I try to prefer the term "coercion".

        Coercion is not the same thing as conversion. How can you coerce the choice of operators if the operator is monomorphic? There can't be coercion for monomorphic operators since there is nothing to coerce.

        There are two "=" operators, and you can coerce the choice of assignment operators.

        $x = foo(); # scalar assignment operator ($x) = foo(); # list assignment operator

        but what's the difference between:

        In one, you used ->to_int (explicit conversion), in the other you didn't (implicit conversion). No coercion occurs since neither subs nor the "+" operator are polymorphic in Perl.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-04-23 07:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found