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

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.

  • Comment on Re^8: Strong typing and Type Safety.A multilanguage approach (implicit)

Replies are listed 'Best First'.
Re^9: Strong typing and Type Safety.A multilanguage approach (implicit)
by chromatic (Archbishop) on Nov 22, 2010 at 02:23 UTC
    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.

        How can you coerce the choice of operators if the operator is monomorphic?

        I have no idea what you mean. The evaluation of the operands coerces an SV to an IV, of course.

        implicit conversion

        I don't understand this logic. How is explicitly using a monomorphic operator which operates on numeric values any less explicit than performing a manual conversion operation through a method?

        (I don't accept the "But it's easy to make a typo when using an operator!" argument because it's easy to make a typo when calling a method.)