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

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".

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

    [ 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.)

        How is explicitly using a monomorphic operator which operates on numeric values any less explicit than performing a manual conversion operation through a method?

        The addition operator operators on scalars, not numeric values; you can pass it a string if you so desire. The operator must coerce the string into a number in order to do the addition.

        The latter is explicit by definition. "Manual" is synonymous with "explicit".

        Implicit conversion is less explicit than explicit conversion.

        ok, it seems I had the wrong definition for coercion. According to wikipedia, "coercion" is simply another way of saying "implicit conversion" ("in most languages", it adds).