Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Re: Re: There's Only One Way To Do It

by hardburn (Abbot)
on Apr 06, 2004 at 18:23 UTC ( [id://343056]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: There's Only One Way To Do It
in thread There's Only One Way To Do It

You have three data types -- scalar, array, hash.

There are a few other types, such as subroutines and filehandles, plus the umbrella glob type. Also, I would argue that array and hash should be considered subtypes of the type 'list', since you can coerce a hash into an array (which may or may not give a useful result). You could also consider blessed references to have their own type (that type being the package they were blessed into), but if you do, you have to remember that it's totally seperate from the rest of Perl's type system. It's also easy to subvert via re-blessing.

Java does the B&D thing with data types, leading to the ever-popular downcast. They may claim to be Strongly Typed, but you can't do anything useful without having to subvert the type system to get data moved around.

Java actually has a rather weak type system, preciely because of the subversion you mention (C and C++ exibit the same problem). A big mistake a lot of people have made is equating static typing = strong, and dynamic typing = weak. Perl is dynamically typed, but also very strong. C/C++/Java are statically typed, but are relatively weak.

Languages with a type system like C are usually only good for giving the compiler some optimization hints. Some would argue that it's more self-documenting, but given how easy it is to subvert, I think this could easily lead to more confusion than its worth.

----
: () { :|:& };:

Note: All code is untested, unless otherwise stated

Replies are listed 'Best First'.
Re: Re: Re: Re: There's Only One Way To Do It
by disciple (Pilgrim) on Apr 06, 2004 at 22:09 UTC
    Perl is dynamically typed, but also very strong.

    Can you explain this a bit more? First, are these definitions correct? Static typing means the type of the variable is known at compile time. Dynamic typing means the type of the variable is not known at compile time.

    If those definitions are correct then I assume that the aspect of Perl that makes it dynamic is that the value in a variable can be a number or string of any length (limited by memory) and therefore makes the language dynamically typed. And the aspect that makes it static is that the structure of the variable is known at compile time (scalar, array, hash) and therefore makes the language statically typed as well. However just as in C and C++ references can be used to avert this static typing.

    Is that right?

      Not quite. Realize that the definitions of "strong" and "static" typing are orthogonal, and I wish there was a completely different terminology used, since they are so easily confused. "Strong" means you can't convert between the types easily. "Static" means the compiler has some idea of what the type is. "Weak" is the opposite of "strong", and "dynamic" is the opposite of "static".

      Since you can't convert an array into a scalar, this aspect of Perl is strongly typed. Since a scalar can hold pretty much anything (strings, numbers, references, etc.), this aspect is dynamically typed.

      Static typing, IMHO, is extremely overrated. Since many statically typed languages make it easy to cast one type into another, you can't trust it to be anything more than an optimization hint.

      ----
      : () { :|:& };:

      Note: All code is untested, unless otherwise stated

        Thanks for the clarification. From your definition, isn't Perl weakly typed in addition to strongly typed? Because in a scalar you can store a string or number (and other things as well). And Perl easily converts between numbers and strings for you.

Re: Re: Re: Re: There's Only One Way To Do It
by herveus (Prior) on Apr 06, 2004 at 18:31 UTC
    Howdy!

    ...OK, I concede that I simplified a bit...but for most purposes, scalar/hash/array cover the field. I'd not consider blessed references as a distinct type, as they are a species of scalar with some "magic" (using "magic" to not necessarily mean Perl-magic).

    Type-as-associated-with-blessed-references is certainly a different dimension, not quite orthogonal to scalar/hash/array, and I was disregarding it in my screed.

    You appear to have read between my lines as I intended (instead of falling for what I will admit could be construed as lame flame bait)...

    yours,
    Michael

Log In?
Username:
Password:

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

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

    No recent polls found