Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^5: Strong typing and Type Safety.A multilanguage approach

by eyepopslikeamosquito (Archbishop)
on Nov 20, 2010 at 10:47 UTC ( [id://872660]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Strong typing and Type Safety.A multilanguage approach
in thread Strong typing and Type Safety.A multilanguage approach

No. By using the + operator, you are explicitly asking for the string to be converted to a number. Why do you say it is implicit? (This thread may be heading towards Room 12A).

Replies are listed 'Best First'.
Re^6: Strong typing and Type Safety.A multilanguage approach (implicit)
by tye (Sage) on Nov 20, 2010 at 21:09 UTC

    Sure, I can "explicitly" do 0+ with the intent of converting a string into a number. "Explicit" describes my motivation. But, no, 1+$duration is not an explicit type conversion, it is explicit addition that implies a type conversion, if required.

    I can write 1+$duration with no intention of doing a conversion. If I am mistaken in thinking that $duration holds a numeric value and write 1+$duration, since the explicit request for addition includes in it an implicit possibility of type conversion, the compiler can't tell that I didn't mean for a conversion to happen. The type conversion is called "implicit". Having to write 1+(int)$duration is much more explicit. Type conversion is only done when I explicitly say "do a type conversion" not when I say "do addition" and can't separately specify that I want a conversion.

    - tye        

      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.

        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.

Re^6: Strong typing and Type Safety.A multilanguage approach
by nikosv (Deacon) on Nov 20, 2010 at 15:40 UTC

    Quoting Modern Perl Books page 15

    Perl will do its best to coerce values to the proper type (see Coercion, page 47), depending on the operators you use. Be sure to use the proper operator for the type of context you want.

    Quoting Wikipedia

    Type Conversion In most languages, the word coercion is used to denote an implicit conversion, either during compilation or during run time.

      Allow me to assure you that the author of Modern Perl disagrees with your interpretation of his words.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2024-04-16 08:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found