Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: AGC was: Re: inner classes not possible,(in objc) right?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: AGC was: Re: inner classes not possible,(in objc) right?




On Apr 4, 2005, at 6:03 PM, Marcel Weiher wrote:

C and its descendants are statically typed languages. They don't have the concept of a dynamic type.

While statically typed, they are not strongly typed but weakly typed (due to casts). You absolutely *can* have a value that does not agree with its static type, though you have no way of determining this type mismatch other than that the value will not work for you.


For example:

float a=3.14159;
float *b=&a;
int **c=(int**)b;
int d = **c;

This will probably segfault on you quite badly, because you are using the in-memory bit pattern of the float 3.14159 as a pointer.

What is it, Marcel, that you want to show with this example ? I can't follow you any more, in fact I can't follow the logic behind this whole sub-thread.


I talk about the static type at compile time and thats what I always have. You, however, insist on talking about values, actual types and dynamic types. I'm talking about A while you are talking about B, which implies that we will never be able to have any meaningful discussion about this topic.

Now lets clarify what the purpose of the above example is:


Is it about showing that a compiler for a statically typed language doesn't know what the type of every variable is at any time ?


If so, why are you posing this question to me ? Compile the example with a C compiler. Either it compiles, then you have the prove that the compiler knew exactly what the type of every variable in that program was at any time, or it doesn't compile. The later case is a prove that the program is not a valid C program.


Is it about the fact that you can write programs that compile but do not work as expect ?


You lied to the compiler, what did you expect ? that it would be somehow able to invoke magic and make the program work ?

You can write programs which are syntactically correct and semantically meaningless in any language, no matter what language model it uses and no matter if it is strongly or weakly typed. You don't even need to come up with such a complicated example, its much easier:

double a = sqrt(-1.0);

There you have it. I needed just one line to create a program which is syntactically correct but semantically meaningless. Or how about this:

static float foo() {}

void main() {
	float a = foo();
}

Perfectly valid C, but still meaningless.

How about this:

NSRect rect = [nil bounds];

Perfectly valid ObjC, but meaningless.

It even works in Java:

public int foo() {
	Vector	a = null;
	return a.size();
}

It even applies to natural languages, too:

"I go tree"

Perfectly valid English, but meaningless.


Or is it about showing that somehow it wouldn't be possible to add an AGC to ObjC ?


Yeah, its indeed very common to bring up contrived examples like yours above, in order to try to show that memory management via an AGC would not be possible in this context. You know, there is however one thing that I will never understand about those examples: why are they so complicated ? why do the invoke so much casting magic when the real point, the really real point, is the question: why do you lie to the compiler ?

Anyway, those example can not be used to show anything about memory management:

void *ptr = realloc((void *) 1234, sizeof(struct foo));
free((void *) 1234);

[((id) 1234) retain];
[((id) 1234) release];

id test = (id)1234;

Do you see the common theme in the above examples ? They all lie to the compiler. They all claim one thing when in reality they do a different thing. They will all crash at runtime. The question of which memory management technique I was using in the above examples is completely irrelevant. The reason why they all crash is because I claimed to the compiler that I would work with an object pointer, when in reality I worked with an integer.

Thus follows the most important questions of all: Why lie to the compiler ?

Another example closer to home:

typedef struct {
    Class isa;
    int a;
} _MyObject;

typedef struct {
    Class isa;
    float a;
} _AnotherObject;

_MyObject a = { objc_getClass( "MyObject" ), 1 };
_AnotherObject b = {  objc_getClass( "AnotherObject" ), 2.0 };

_MyObject *c = &a;
_AnotherObject *d = &b;
d = (_AnotherObject)c;

Are you going to argue that this is NOT a case where the dynamic type of the "object" disagrees with the static type?

At no point in time is the ObjC compiler ever concerned with the dynamic type of an object, while it is compiling the above code.


Why can you not accept that I'm exclusively talking about the _static_ type of a variable / expression _not_ its dynamic type ?


Regards,

Dietmar Planitzer

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Objc-language mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Re: AGC was: Re: inner classes not possible,(in objc) right? (From: Dietmar Planitzer <email@hidden>)
 >Re: AGC was: Re: inner classes not possible,(in objc) right? (From: Marcel Weiher <email@hidden>)
 >Re: AGC was: Re: inner classes not possible,(in objc) right? (From: Dietmar Planitzer <email@hidden>)
 >Re: AGC was: Re: inner classes not possible,(in objc) right? (From: Marcel Weiher <email@hidden>)
 >Re: AGC was: Re: inner classes not possible,(in objc) right? (From: Dietmar Planitzer <email@hidden>)
 >Re: AGC was: Re: inner classes not possible,(in objc) right? (From: Prachi Gauriar <email@hidden>)
 >Re: AGC was: Re: inner classes not possible,(in objc) right? (From: Helge Hess <email@hidden>)
 >Re: AGC was: Re: inner classes not possible,(in objc) right? (From: Marcel Weiher <email@hidden>)
 >Re: AGC was: Re: inner classes not possible,(in objc) right? (From: Dietmar Planitzer <email@hidden>)
 >Re: AGC was: Re: inner classes not possible,(in objc) right? (From: Marcel Weiher <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.