5

Statically typed vs dynamically typed has been asked repeatedly on stackoverflow, for example here.

The consensus seems to be (quoting from the top answer of the above link):

A language is statically typed if the type of a variable is known at compile time.

And a dynamic language:

A language is dynamically typed if the type is associated with run-time values, and not named variables/fields/etc.

Perl seems to be statically typed by this (or other common definitions of static/dynamic typing). It has 3 types: scalar, array, hash (ignoring things like references for simplicity's sake). Types are declared along with variables:

my $x = 10;                   # declares a scalar variable named x
my @y = (1, 2, 3);            # declares an array variable named y
my %z = (one => 1, two => 2); # declares a hash variable named z

The $, @ and % above tell Perl which type you want; I'd count this as a form of explicit typing.

Once x has been declared as a scalar, as above, it's impossible to store a non-scalar value in x:

$x = @y;                      # x is now 3

Will convert y to a scalar (in Perl, array to scalar conversion result in the length of the array). I blame this on weak typing (Perl very liberally allows conversions between its 3 types), rather than dynamic typing.

Whereas in most statically typed languages, such an assignment would be an error, in Perl it is ok because of implicit conversions (similar to how bool x = 1; is fine in C/C++, but not in Java: both are statically typed, but Java is more strongly typed in this case). The only reason this conversion happened at all in Perl is because of the type of x, which again suggests Perl is statically typed.

Another argument people have against Perl being statically typed is that floats, ints, and strings are all stored in the same type of variable (scalars). But this really has nothing to do with static or dynamic typing. Within Perl's type system (which has only 3 types), there is no difference between floats, ints and strings. These all have type scalar. This is similar to saying C89 isn't statically typed because it used the int type to represent both ints and bools.

Obviously, this line of reasoning is ridiculous. Perl has very little in common with what most people think of as statically typed languages like C/C++, Java, OCaml, etc.

My question is, what's wrong with this line of reasoning?

CoffeeTableEspresso
  • 2,614
  • 1
  • 12
  • 30
  • I am of course aware that you could pretend a language like Python is "statically typed" with one type (let's call it Py_Object), but Perl is different from Python in this regard in that Perl will convert values based on the type of variables (which it knows at compile time). – CoffeeTableEspresso Jul 04 '19 at 20:50
  • In typed languages, you can create your own types or classes, and use them as type restrictions. In Perl, you can create classes (packages), but you can't use them as types. – choroba Jul 04 '19 at 21:01
  • 1
    First of all, there's no such consensus. And if there is a consensus, then it only goes to show that the terms "statically-typed language" and "dynamically-typed language" are **useless**. Too many languages would qualify as both/neither (including all languages supporting virtual methods, such as Perl, Java and C++). – ikegami Jul 04 '19 at 21:18
  • @choroba I don't think that really has anything to do with static/dynamic typing. Early pre-standardized versions of C didn't have structs, but no one would claim early C wasn't statically typed.. – CoffeeTableEspresso Jul 04 '19 at 21:32
  • @ikegami i'd say statically and dynamically typed have a widely accepted meaning in industry, even if the terms are meaningless. – CoffeeTableEspresso Jul 04 '19 at 21:33
  • I don't accept that a big part of the industry are morons. (Who else would accept contradictory definitions?) – ikegami Jul 04 '19 at 21:35
  • Even C doesn't fit the posted definition of statically-typed perfectly. – ikegami Jul 04 '19 at 21:39
  • @ikegami that's a bold claim about C, could you give an example? – CoffeeTableEspresso Jul 04 '19 at 21:41
  • Casting says the type of something isn't actually the type of the variable it's in. – ikegami Jul 04 '19 at 21:42
  • The problem with your example (`$x = @y`) is that perl will not error out on it at compile time, but sneakily convert it to `$x = av_len(@y)+1`. So, perl's types are more like floats and ints in C, not like different structs/objects and pointers to them. Maybe there's a way to restrict which values can be assigned to scalars in perl (eg. with `Variable::Magic`) but I fail to see how that could be easily enforced at compile time (short of adding another tree analyzer to be called from `CHECK`), so saying that perl is statically typed is, really, kind of a hand-waving and nothing more. –  Jul 04 '19 at 21:43
  • @mosvy I don't agree with that. You wouldn't give `int x = 1.0;` as an example of C not being statically typed. In fact, C knowing at compile time that `1.0` should be converted to `1` stems from the fact that C is statically typed. If you have a better, alternative definition of static typing than the one I used, feel free to provide it. – CoffeeTableEspresso Jul 04 '19 at 21:50
  • Re "*You wouldn't give int x = 1.0; as an example of C not being statically typed*", Quite so. That is indeed NOT the example I gave. – ikegami Jul 04 '19 at 22:09
  • Your definition is trying to empty the concept of any content: of course `int foo(double bar){ return bar / 2; }` is an example of C **not** being completely statically typed, because it leaves a lot of unpredictable behavior for the runtime. –  Jul 04 '19 at 22:10
  • The concept **is** empty of any content. There is no benefit to knowing whether a language is statically-typed or dynamtically-typed, no matter how they are defined. – ikegami Jul 04 '19 at 22:11
  • @ikegami I gave that example in response to mosvy saying Perl wasn't statically typed because it allowed type coercions, not in response to you saying casting. – CoffeeTableEspresso Jul 04 '19 at 22:31
  • @mosvy `int foo(double bar){ return bar / 2; }` doesn't leave anything up to the runtime. This is very well defined behaviour. – CoffeeTableEspresso Jul 04 '19 at 22:32
  • @CoffeeTableEspresso that's an absolutely ridiculous statement. And it's exactly the problem with your definition: if C allowed pointers to different types to be freely assigned to one another, and different structs to be assigned to one another by just doing a memcpy with the size of the shorter one, you could still claim that it's statically typed because, duh, those operations are "very well defined". –  Jul 04 '19 at 22:47
  • @mosvy I'm not sure why you think the example code you posted is an example of dynamic typing. RE your response to me: I wasn't saying it's an example of static typing because it's well defined, I was criticizing your claim that that "it leaves a lot of unpredictable behaviour for the runtime". – CoffeeTableEspresso Jul 04 '19 at 22:59
  • Re "*there is no difference between floats, ints and strings*", No compile-time differences. You might even say these types are associated with the run-time value... – ikegami Jul 04 '19 at 23:28
  • @ikegami you're right about that. This would be an example of dynamic typing in Perl. It very rarely makes a difference though. `10` and `"10"` behave the same in almost all cases in Perl. – CoffeeTableEspresso Jul 05 '19 at 00:21
  • Ir rarely makes a difference for strings vs numbers, but whether a language is able to convert between types implicitly or not is not part of either definitions you posted. And what about a number vs a reference to an object of class Foo? – ikegami Jul 05 '19 at 02:33
  • "I'm not sure why you think the example code you posted is an example of dynamic typing". Because that's what it is. There's no conceptual difference between C automatically (and destructively) converting doubles to ints and perl converting strings which [look like a number](https://perldoc.perl.org/perlapi.html#looks_like_number) to numbers. –  Jul 05 '19 at 08:18
  • @mosvy I think you're confused about dynamic typing. Dynamic typing has nothing to do with whether or not you convert values of one type to another. – CoffeeTableEspresso Jul 05 '19 at 13:56
  • I'm not confused. That's exactly what dynamic typing is: automatic, *non-explicit*, silent, *run-time* conversions between different values based on more or less surprising and arcane rules baked into the language/compiler. I'm just not buying into your definition, which is far from being some industry consensus, despite what you think. I don't want to continue this discussion further. –  Jul 05 '19 at 14:05
  • @mosvy can you give me a source that defines dynamic typing that way? I've only ever heard that referred to as _weak typing_. BTW, when you disagree with my definition, it's nice to say what definition you're using at the start, rather than not mentioning it all. – CoffeeTableEspresso Jul 05 '19 at 14:08
  • and yes, _if you define dynamic typing like that_, (which again, wasn't clear to me earlier because you never mentioned it,) your C example is dynamic typing. Not really sure how it's relevant since that's not the concept I was referring I to (no matter what name you give it). – CoffeeTableEspresso Jul 05 '19 at 14:10

1 Answers1

7

I disagree on there being a consensus on the definitions you posted. But like your claim, that's opinion-based, and thus off-topic.

The posted definitions of "statically-typed language" and "dynamically-typed language" are useless. These are imaginary buckets into which very few languages fit.


According to the definition of statically-typed language you posted, Perl is a statically-typed language.

  • The type of $a is known to be a scalar at compile-time.
  • The type of @a is known to be an array at compile-time.

According to the definition of statically-typed language you posted, Perl isn't a statically-typed language.

  • $a could contain a signed integer (IV).
  • $a could contain a string (PV).
  • $a could contain a reference (RV) to an object of class Foo.

According to the definition of dynamically-typed language you posted, Perl is a dynamically-typed language.

  • $a could contain a signed integer (IV).
  • $a could contain a string (PV).
  • $a could contain a reference (RV) to an object of class Foo.

According to the definition of dynamically-typed language you posted, Perl isn't a dynamically-typed language.

  • The type of $a is known to be a scalar at compile-time.
  • The type of @a is known to be an array at compile-time.

Similarly, C++, C#, Java, BASIC and assembler languages are both/neither statically-typed and dynamically-typed. Even C doesn't fit the posted definition of statically-typed perfectly.

ikegami
  • 367,544
  • 15
  • 269
  • 518