strong/weak typing and pointers

G

Gabriel Zachmann

Is it correct to say that strong/weak typing does not make a difference
if one does not use any pointers (or adress-taking operator)?

More concretely, I am thinking particularly of Python vs C++.
So, are there any examples (without pointers, references, or adress-taking),
which would have a different result in Python and in C++?

I would appreciate all insights or pointers to literature.

TIA,
gabriel.

--
/-------------------------------------------------------------------------\
| We act as though comfort and luxury |
| were the chief requirements of life, |
| when all that we need to make us happy |
| is something to be enthusiastic about. (Einstein) |
+-------------------------------------------------------------------------+
| (e-mail address removed)-bonn.de __@/' www.gabrielzachmann.org |
\-------------------------------------------------------------------------/
 
D

Diez B. Roggisch

Is it correct to say that strong/weak typing does not make a difference
if one does not use any pointers (or adress-taking operator)?

It seems that you mistake strong/weak typing with static/dynamic typing - a
completely different beast.

Python is in fact strong typed - in opposition to php or perl or even C,
this won't work:

a = "1" + 2

as "1" is a string and 2 an integer. And even though C is statically typed,
it won't complain - you just end up with an unexpected result.

And pointers are not evil in themselves - the are a neccessity to create
recursive structures. But deliberately casting pointers can be very harmful
- a reason why its forbidden in languages like java and AFAIK ada.
More concretely, I am thinking particularly of Python vs C++.
So, are there any examples (without pointers, references, or
adress-taking), which would have a different result in Python and in C++?

I have difficulties to understand what you want here. Please elaborate.
 
J

JCM

Gabriel Zachmann said:
Is it correct to say that strong/weak typing does not make a difference
if one does not use any pointers (or adress-taking operator)?

You'll find a lack of consensus here on what's meant by "strong/weak
typing". In Python there's no way to re-interpret the bits of a value
as if they were a different type. For example, code like this is
impossible in Python:

float x = 2.5;
printf("%d\n", *(int*)&x);
More concretely, I am thinking particularly of Python vs C++.
So, are there any examples (without pointers, references, or adress-taking),
which would have a different result in Python and in C++?

If I understand your question, I believe not; because Python doesn't
provide the low-level operators that would be necessary for it.
 
A

Andrea Griffini

It seems that you mistake strong/weak typing with static/dynamic typing - a
completely different beast.

Python is in fact strong typed - in opposition to php or perl or even C,
this won't work:

a = "1" + 2

as "1" is a string and 2 an integer. And even though C is statically typed,
it won't complain - you just end up with an unexpected result.

You didn't mention C++. Try this ...

std::string s = "Wow";
s += 3.141592654; // Perfectly valid
s = 3.141592654; // Also valid

Andrea
 
G

Grant Edwards

It seems that you mistake strong/weak typing with
static/dynamic typing - a completely different beast.

Python is in fact strong typed - in opposition to php or perl or even C,
this won't work:

a = "1" + 2

as "1" is a string and 2 an integer.

"1" is a pointer to a char.
And even though C is statically typed, it won't complain

That's because <pointer> + <integer> has a well-defined meaning
in C -- just like said:
- you just end up with an unexpected result.

Only people who don't know how C pointer arithmatic works will
get unexpected results. [That's probably a shockingly high
percentage of C programmers.]
 
A

Alex Martelli

JCM said:
You'll find a lack of consensus here on what's meant by "strong/weak
typing". In Python there's no way to re-interpret the bits of a value
as if they were a different type. For example, code like this is
impossible in Python:

float x = 2.5;
printf("%d\n", *(int*)&x);

True, but module struct lets you get the same effect, though the 4 bytes
get copied, not 'reinterpreted in place'.

If I understand your question, I believe not; because Python doesn't
provide the low-level operators that would be necessary for it.

Well... what about something like:

std::list<int> a, b;

....

a[2] = 45;
b = a;
b[2] = 23;

In C++, a[2] is still 45, because 'b = a;' COPIED the whole list over.

A similar case in Python would make no implicit copies, just give an
additional name 'b' to the same object which 'a' names, so assigning to
b[2] would also change a[2]. Nothing to do with dynamic vs static
typing, of course, because e.g. Java would work like Python here.

I've found this one tidbit to be the single biggest stumbling block for
experienced C or C++ programmers learning Java or Python. "without
references" isn't really true, because (in Python and Java) a and b
_are_ 'references' (aka names) to the same object -- but then, neither
in Java nor Python can you say that a name _isn't_ ``a reference''...
names always reference objects... ((Java makes exceptions to this rule
for some lowlevel types such as ints, Python doesn't)).

Templates may be another case in which C++ might act one way, and Java
and Python the other way, and may be more relevant to type issues.

E.g.,

template<typename T>
T foo(const T& bar)
{
static T baz;
T temp = baz;
baz = bar;
return temp;
}

now, if you make a series of calls such as foo(1), foo(1.2), foo(2),
foo(3.4), you should get as results 0, 0.0, 1, and 1.2 -- there are two
'foo's, one instantiated for T being int, another one for T being
double, so the 'delay register' baz also exists in two incarnations.

In the Python rough equivalent:

def foo(bar, _baz=[None]):
temp = _baz.pop()
_baz.append(bar)
return temp

(and the Java equivalent, too, with everything declared as Object to be
"generic"), the same calls would give None, 1, 1.2, 2 -- there is a
single 'incarnation' of foo, a single 'delay register' _baz. (Not sure
which way Java 1.5's generics go wrt statics; I'd expect the C++ way).


Not sure I've gotten the gist of what the OP was asking about, though.

Alex
 
J

Jorgen Grahn

Python is in fact strong typed - in opposition to php or perl or even C,
this won't work:

a = "1" + 2

as "1" is a string and 2 an integer. And even though C is statically typed,
it won't complain - you just end up with an unexpected result.

[slightly offtopic defense of C and C++]

That's true only if a is a 'char *' of course (and if you didn't expect
this unexpected result ;-).

In C++ 'char *' would have been invalid, but not 'const char *' or
(and this is worse) 'std::string'.
And pointers are not evil in themselves - the are a neccessity to create
recursive structures.

I'd say they are neccessary, period. But note that I count what Java and
Python call "references" as pointers ...
But deliberately casting pointers can be very harmful
- a reason why its forbidden in languages like java and AFAIK ada.

Yes; lots of casts in C code (or worse, in C++ code) is a very, very bad
sign. Note though, that in the absense of casts, C and in particular C++ are
pretty strongly typed for pointers. Strongly enough to keep me happy, at
least.
I have difficulties to understand what you want here. Please elaborate.

I think he means the static/dynamic typing, and if it makes a difference in
a simple C and a simple Python program, if we pretend that all names are
just "variables". Hard to come up with a meaningful answer, but how about:

a = 2 const int a = 2;
if something_rare_happens: if(something_rare_happens) {
return b return b;
a = 'hugo' }
std::string a("hugo");
bar(a) bar(a);

In the Python program, we might clobber 'a' by accidentaly reusing its name
for something of a different type. C++ is stricter about this (doesn't
allow the construct above, in fact) and you can look at the code
(statically) to see which names are in scope and which are not.

In Python, you can forget to give 'b' a value, and not notice until that
code executes. You can in C++ too, and the runtime effects will be more
subtle but worse. The compiler is more likely to catch pure typos, though.

In Python, it is often hard to look at a function such as 'bar' and say you
know it is always called with an integer argument, or a string, or a 'Foo'
object. It's not even enough to look at all places where 'bar' is called,
because the type of b may depend on the phase of the moon or other dynamic
things. In C++ they compiler makes the guarantees, unless someone has
willfully bypassed the type system.

Is all this caused by the static/dynamic typing difference? No, but it
certainly has to do with it. Both languages have made a decision here, and
that of course works together with the rest of the language design. Python
doesn't /have/ to declare variables and parameters to give them a type, so
Guido said you don't have to, and let functions take all kinds of flexible
arguments. C++ had to have declarations/definitions, so Bjarne used them to
add the 'const' keyword, to give values a scope and making it well-defined
when objects are destroyed. And so on.

For what it's worth, I think both kinds of typing are interesting and useful
tools. Neither of them are obsolete or inferior; neither of them will
disappear in the next ten years.

/Jorgen
 
M

Mel Wilson

Is it correct to say that strong/weak typing does not make a difference
if one does not use any pointers (or adress-taking operator)?

One effect of weak typing is to put more reliance on
operators. In Perl, for instance the string operator `lt`
does a string compare to find that "10" is less than 2
(lexically) and the numeric operator `<` finds that "10" is
not less than 2 (numerically). Nothing to do with pointers
at all.

Regards. Mel.
 
D

Duncan Booth

Gabriel said:
Is it correct to say that strong/weak typing does not make a
difference if one does not use any pointers (or adress-taking
operator)?

More concretely, I am thinking particularly of Python vs C++.
So, are there any examples (without pointers, references, or
adress-taking), which would have a different result in Python and in
C++?

Here's a trivial example that is almost identical in Python and C/C++ but
gives totally different results. In a weakly typed language such as C or
C++:

#include <stdio.h>

int main(int argc, char**argv)
{
float f = 3;
printf("value is %d", f);
}

I get the output (you may get different results):

value is 0

In a fairly strongly typed language such as Python:
value is 3

In a really strongly typed language I would expect an exception to
be thrown.
 
O

Oliver Fromme

Diez B. Roggisch said:
> And pointers are not evil in themselves - the are a neccessity to create
> recursive structures. But deliberately casting pointers can be very harmful
> - a reason why its forbidden in languages like java and AFAIK ada.

I agree. A language worth mentioning in this context might
be Cyclone. It's derived from C (and still has very much in
common with it, so it's easy to port C programs to Cyclone).
The difference is that "safe" features have been added to the
language. For example, you can't do arbitrary type casts on
pointers anymore, and you can't access strings (or other
allocated memory) beyond their end. The ultimate goal of
Cyclone is to make it impossible for programs to crash or
have security holes caused by buffer overflows or similar.

Furthermore, Cyclone provides interesting features, such as
tagged unions, parametric polymorphism, pattern matching,
exceptions, even a somewhat limited implementation of type
inference.

http://www.research.att.com/projects/cyclone/

Best regards
Oliver
 
G

Gabriel Zachmann

It seems that you mistake strong/weak typing with static/dynamic typing - a

sorry, i don't think so.
completely different beast.

Python is in fact strong typed - in opposition to php or perl or even C,
this won't work:

a = "1" + 2

haven't tried that yet, but i guess it would at least evoke a warning in
ANSI C++.
as "1" is a string and 2 an integer. And even though C is statically typed,

In C++, "1" is a 'char const * const'.
it won't complain - you just end up with an unexpected result.

And pointers are not evil in themselves - the are a neccessity to create

i didn't say that.
In fact, they are everywhere, even in Python and Java, except that you
don't get to see them.
recursive structures. But deliberately casting pointers can be very harmful

i agree.
- a reason why its forbidden in languages like java and AFAIK ada.


I have difficulties to understand what you want here. Please elaborate.

i am just trying to come up with the best possible definition of "weak and
strong typing" ( "best" in the sense of completeness and objectiveness).
I've read up quite a bit about strong/weak typing, and static and dynamic
typing, and it seems to me that, while static/dynamic typing is a pretty
well-defined concept, the definition of strong/weak typing is not so
clear-cut.

Cheers,
Gab.

--
/-------------------------------------------------------------------------\
| There are works which wait, |
| and which one does not understand for a long time; [...] |
| for the question often arrives a terribly long time after the answer. |
| (Oscar Wilde) |
+-------------------------------------------------------------------------+
| (e-mail address removed)-bonn.de __@/' www.gabrielzachmann.org |
\-------------------------------------------------------------------------/
 
G

Gabriel Zachmann

You didn't mention C++. Try this ...
std::string s = "Wow";
s += 3.141592654; // Perfectly valid
s = 3.141592654; // Also valid

ah, good example.

So, would it be valid to say:
the more coercion (or automatic conversion) rules a language has, the
weaker the typing?

Best regards,
Gabriel.

--
/-------------------------------------------------------------------------\
| There are works which wait, |
| and which one does not understand for a long time; [...] |
| for the question often arrives a terribly long time after the answer. |
| (Oscar Wilde) |
+-------------------------------------------------------------------------+
| (e-mail address removed)-bonn.de __@/' www.gabrielzachmann.org |
\-------------------------------------------------------------------------/
 
G

Gabriel Zachmann

printf("value is %d", f);

This seems a very good example to me.

Note that this is also an example showing that C++ does contain a little
bit of dynamic typing, too, isn't it?

Cheers,
gab.

--
/-------------------------------------------------------------------------\
| There are works which wait, |
| and which one does not understand for a long time; [...] |
| for the question often arrives a terribly long time after the answer. |
| (Oscar Wilde) |
+-------------------------------------------------------------------------+
| (e-mail address removed)-bonn.de __@/' www.gabrielzachmann.org |
\-------------------------------------------------------------------------/
 
D

Diez B. Roggisch

Gabriel said:
This seems a very good example to me.

Note that this is also an example showing that C++ does contain a little
bit of dynamic typing, too, isn't it?

Where do you get that idea from? Modern compilers are aware of printf, and
have special type-checking rules built into them that verify that you pass
the right number and types of arguments for the format string. And thats
totally static, as it is done at compiletime!
 
D

Diez B. Roggisch

Note that this is also an example showing that C++ does contain a little
bit of dynamic typing, too, isn't it?

On a related note: c++ _can_ have some dynamic type information - when not
disabled with -fno-rtti (gcc) you get "real time type identification". That
allows for (guess why there called that way...) dynamic casts.
 
D

Diez B. Roggisch

It seems that you mistake strong/weak typing with static/dynamic typing
sorry, i don't think so.

I still do - as your other post regarding dynamic aspects of printf makes me
think so.

Dynamic typing means that at runtime, type-information is associated with
data and used e.g. to determine what implementation for certain operations
is to be taking. Static typing means that type information is used during
compiletime. Python is purely dynamic, java has both aspects and C++ is
mainly static - albeit rtti (and virtual methods somehow) are dynamic. So
one could say its mixed.

Strong/weak typing is about how much you care of types at all - in php, its
perfectly legal to add strings to numbers - the string simply gets
converted to a number beforehand, that conversion yielding 0 when there is
nothing useful and numberlike can be extracted from the string. So typing
is weak, as it doesn't constrain the possible operations on variables with
certain values.

haven't tried that yet, but i guess it would at least evoke a warning in
ANSI C++.

No, as others pointed out its a legal pointer operation
i am just trying to come up with the best possible definition of "weak and
strong typing" ( "best" in the sense of completeness and objectiveness).
I've read up quite a bit about strong/weak typing, and static and dynamic
typing, and it seems to me that, while static/dynamic typing is a pretty
well-defined concept, the definition of strong/weak typing is not so
clear-cut.

That might be - but you put python and c++ in the same sentence as weak and
strong typing - and that looked as if you think of python beeing weakly
typed, which it is not.
 
S

Scott David Daniels

Gabriel said:
I am just trying to come up with the best possible definition of "weak and
strong typing" ( "best" in the sense of completeness and objectiveness).
I've read up quite a bit about strong/weak typing, and static and dynamic
typing, and it seems to me that, while static/dynamic typing is a pretty
well-defined concept, the definition of strong/weak typing is not so
clear-cut.
How about something like this:

Strong typing respects data as invariant. There is no way to "sneak
around" a data type and "tweak" its representation. The reason C is
weakly typed is that you can easily get to the bits in a float (for
example), rather than always having to deal with a float with float
operations.

Strong types provide strong protection for data types as their
abstraction; weak types allow you to operate on data "behind the
wall of abstraction". A Smalltalk programmer would say that
Python is more weakly typed than Smalltalk for user-defined types.

-Scott David Daniels
(e-mail address removed)
 
M

Michael Hobbs

Andrea Griffini said:
You didn't mention C++. Try this ...

std::string s = "Wow";
s += 3.141592654; // Perfectly valid
s = 3.141592654; // Also valid

Those automatic coercions are always handy when writing code, but a
pain in the @$$ when reading it. I can't count the number of times
that I stepped through C++ code, saying "what the ...?", only to
realize that there was some perlish 'operator +=(const **' statement
tucked away in a header file somewhere. Good riddance, I say. (My
code only overrides the __or__ operator. ;-)

-- Mike
 
D

Diez B. Roggisch

Strong typing respects data as invariant. There is no way to "sneak
around" a data type and "tweak" its representation. The reason C is
weakly typed is that you can easily get to the bits in a float (for
example), rather than always having to deal with a float with float
operations.

Sounds good.
Strong types provide strong protection for data types as their
abstraction; weak types allow you to operate on data "behind the
wall of abstraction". A Smalltalk programmer would say that
Python is more weakly typed than Smalltalk for user-defined types.

Never used smalltalk, so help me with understanding this:
Do you mean by that that python allows alteration of members of an object,
thus circumvening possible methods that should be used for that? Or, to put
it in an other way: do you consider the lack of a privacy policy of members
as "weak"?

If this is the case: I personally wouldn't call that "weaker" - python just
allows for more default-messages to be accepted by an object - if you want
better privacy for your members, use slots and properties. But this is of
coures not totally safe - as it is not in java c++.
 
A

Aahz

Where do you get that idea from? Modern compilers are aware of printf,
and have special type-checking rules built into them that verify
that you pass the right number and types of arguments for the format
string. And thats totally static, as it is done at compiletime!

The format string has to be static? That's news to me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top