Strong Typing?
Nov. 17th, 2005 08:34 amGeek question:
What is the feature of a language that you consider "strong typing"? How does strong typing manifest itself (or not) in your favourite language or two?
Geek question:
What is the feature of a language that you consider "strong typing"? How does strong typing manifest itself (or not) in your favourite language or two?
(no subject)
Date: 2005-11-17 04:30 pm (UTC)(no subject)
Date: 2005-11-17 04:46 pm (UTC)String foo = "a";
System.out.println(foo+1);
generates a1.
In Perl, the output from polyfrog's script is 1.
(no subject)
Date: 2005-11-17 04:51 pm (UTC)or...
um.
(no subject)
Date: 2005-11-17 05:28 pm (UTC)(no subject)
Date: 2005-11-17 05:13 pm (UTC)(no subject)
Date: 2005-11-17 05:30 pm (UTC)well, don't that just beat all...
(no subject)
Date: 2005-11-17 07:44 pm (UTC)It should either fail or do what I said (increment the character value by one). Doing what you're saying, it has to be implicitly casting my 1 into "1" and then concatenating. Which in turn implies that there's some typing happening in the background.
On the other hand, your example and mine are not the same: You are displaying the result of foo+1. I am incrementing foo and then displaying. At the end of yours, foo is still "a", at the end of mine its...well in any weakly-typed language I've used, its "b".
(no subject)
Date: 2005-11-17 08:42 pm (UTC)$ cat test.pl
$foo="a";
$foo=++$foo;
print $foo;
print "\n";
$ perl test.pl
b
But, that reveals a parallel ambiguity in expressing "incremement" versus "add 1" in most languages. Perl (and other languages borrowing heavily from C) distinguish between them; other languages don't, and some use a partial typing to "do what I mean". Javascript doesn't like incrementing a string at all,
javascript: var foo = "a"; document.write(++foo);
NaN
which kind of indicates that it's doing some DWIM with regard to the "+" operator.
At the opposite end of things, we've got bourne shell scripting where EVERYTHING is a string operation, unless specified otherwise:
$ foo=1+1
$ echo $foo
1+1
$
If you specify the operation to be a numerical calculation,
$ foo=$((1+1))
$ echo $foo
2
$ foo=$((a+1))
$ echo $foo
1
$ echo $a
$ a=a
$ echo $a
a
$ foo=$((a+1))
/bin/ksh: a: expression recurses on parameter `a'
$
(no subject)
Date: 2005-11-17 08:44 pm (UTC)$ foo=$(($a+1))
/bin/ksh: a: expression recurses on parameter `a'
$
(no subject)
Date: 2005-11-17 07:49 pm (UTC)(no subject)
Date: 2005-11-17 07:59 pm (UTC)(no subject)
Date: 2005-11-17 08:12 pm (UTC)(no subject)
Date: 2005-11-17 08:19 pm (UTC)Either I'm misremembering the multiple layers of casting as in your example (it was 25 years ago, after all) or maybe I was doing peek-poke things to get around it...or Commodore BASIC was different.
All are equally likely.
I know that TUTOR (the language I worked in professionally in the 80s) and original K&R C both do it my way. C especially I remember transitioning from K&R to ANSI C and running up against the typing restrictions.
(no subject)
Date: 2005-12-03 08:47 pm (UTC)If your character is represented as a char/byte, then yes, incrementing it shifts the letter up. But if it's a string then it depends on how "+" is interpreted: either it will concatenate a "1", or, horrors, it will increment your pointer and...
Some BASICs let you work with character codes. Commodore BASIC had tons of poke/peek/call things that were basically invoking magic machine code, so you would probably have been passing char/byte values around.