Re: dynamic type checking - a pauline conversion?
From: Jeff Brooks (jeff_brooks_at_nospam.com)
Date: 03/19/04
- Next message: Cristiano Sadun: "Re: TDD: Test-Driven Design or Test-Driven Development?"
- Previous message: AndyW: "Re: fuzzy object question."
- In reply to: Robert C. Martin: "dynamic type checking - a pauline conversion?"
- Next in thread: Dave Harris: "Re: dynamic type checking - a pauline conversion?"
- Reply: Dave Harris: "Re: dynamic type checking - a pauline conversion?"
- Reply: Dmitry A. Kazakov: "Re: dynamic type checking - a pauline conversion?"
- Reply: thoff: "Re: dynamic type checking - a pauline conversion?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 19 Mar 2004 10:43:24 GMT
Hello everyone! This is a really interesting debate!
A few points keep coming up as arguments which i don't feel are valid.
Since its late i won't mention them all. One of the points i disagree
with is people seem to believe that because C++ is statically typed that
it is somehow more reliable then dynamically typed languages.
I wrote a small program in C++ to demonstrate my point (I used gcc 2.95.2).
--- a.c++ ---
#include <iostream.h>
class A
{
public:
int a;
int b;
void print();
};
class B
{
public:
int b;
int a;
void print();
void dumbTypeCall();
};
void A::print()
{
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
void B::print()
{
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
void B::dumbTypeCall()
{
cout << "Should this be possible?" << endl;
}
int main()
{
A *a = new A();
B *b = (B*)a;
b->a = 3;
b->b = 1;
a->print();
b->print();
b->dumbTypeCall();
return 0;
}
--- The output for this code (yes it successfully compiled! O.o) is: --- snip --- a = 1 b = 3 a = 3 b = 1 Should this be possible? --- I compiled this with all warnings on and it didn't give me any. Does anyone here believe 'b' (class B) should be able to reference to the same instance as 'a' (class A)? How is this safe? When i call print on 'a' it shows a=1, b=3. When i call print on 'b' it shows a=3, b=1. So the print that is dispatched doesn't depend on the object i'm refering to, it depends on the type of reference to the object. It wouldn't have mattered if i made the methods virtual because they don't share the same super class. This doesn't stop C++ from allowing me to assign 'a' to 'b'! I then call dumbTypeCall() on 'b' (which points to an instance of class A). This works without any problem even though class A doesn't have a method called dumbTypeCall. A lot of developers seem to think that because C++ verifies that the reference type has the appropriate methods at compile time this makes C++ reliable. This would be true if the reference type actually matched the type of the object being pointed to in all cases but C++ makes no such guarantee. In Smalltalk the reference doesn't have a type but the instance does have a type. All method invocation is based on the type of the instance which means the correct method is always called (this can be guaranteed at runtime). In Smalltalk calling a method that doesn't exist causes an exception to be thrown at runtime. Both Smalltalk, and C++ can not guarantee type correctness at compile time. Smalltalk handles runtime errors better then C++ does (most of the time C++ doesn't fail even though the code is broken). Jeff Brooks
- Next message: Cristiano Sadun: "Re: TDD: Test-Driven Design or Test-Driven Development?"
- Previous message: AndyW: "Re: fuzzy object question."
- In reply to: Robert C. Martin: "dynamic type checking - a pauline conversion?"
- Next in thread: Dave Harris: "Re: dynamic type checking - a pauline conversion?"
- Reply: Dave Harris: "Re: dynamic type checking - a pauline conversion?"
- Reply: Dmitry A. Kazakov: "Re: dynamic type checking - a pauline conversion?"
- Reply: thoff: "Re: dynamic type checking - a pauline conversion?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|