adambard / learnxinyminutes-docs

Code documentation written as code! How novel and totally my idea!
https://learnxinyminutes.com/
Other
11.51k stars 3.35k forks source link

Objective-C: Object declarations #691

Open AndrewHuffman opened 10 years ago

AndrewHuffman commented 10 years ago

The page states the following:

// Object declarations
// Put the * in front of the variable names for strongly-typed object declarations
MyClass *myObject1 = nil;  // Strong typing
id       myObject2 = nil;  // Weak typing

However, this is misleading if not completely incorrect. The * is the same as it is in C, it means it's the variable is a pointer. It's not clear what is meant by 'strong' or 'weak' typing here, but it's my understanding that, like C, Objective-C is a weakly-typed language (though statically enforced at compile-time).

Rather, I think what is being said here is that myObject is referencing an explicit (statically-checked) object of type MyClass while myObject2 is referencing any Objective-C object.

I would fix this myself, but I'm just learning the language and would prefer to defer to someone with more expertise.

levibostian commented 10 years ago

Honestly, I have never been completely sure about how to tell if a language is strong or weakly typed. And from the looks of some searching, it is a hard concept for everyone.

adambard commented 10 years ago

There's some contention about the issue. For some languages there seems to be a consensus (Haskell is strongly typed, PHP is weakly typed). For my part, I think any language with what amounts to void pointers (e.g. C, ObjC, dynamic languages) must be considered weakly typed in general. Some would argue that a language with nullable variables (Java) is weakly typed as well.

In any case, it's clearly wrong that * is a magic identifier that makes things strongly typed in Objective-C. It's probably best to pull out any business about strong or weak typing and just say what it is. Of course, I'm not really qualified to do it. Does anyone know if id is effectively the same as void *?

vendethiel commented 10 years ago

weakly typed => has implicit conversions and "Object"-like (or void *-like) objects. strongly typed => doesn't.

Then there's just statically vs dynamically typed, which is very different, but nicely summed up here.

AndrewHuffman commented 10 years ago

I would agree that given the ambiguity of what is meant by "weakly" vs "strongly" typed it would make sense to remove references to those terms and more explicitly describe what is occurring here.

There's some differences between id and void *, namely how the GC handles them and that id has some advantages that prevent runtime errors.

levibostian commented 10 years ago

@AndrewHuffman, when you have a moment, you can go ahead and implement the changes you wish on the file and make a PR for it!