Re: Is "-init" really needed?

2017-08-10 Thread Jean-Daniel

> Le 10 août 2017 à 16:09, Charles Srstka  a écrit :
> 
>> On Aug 10, 2017, at 8:59 AM, Alastair Houghton 
>>  wrote:
>> 
>> On 10 Aug 2017, at 14:57, gerti-cocoa...@bitart.com 
>>  wrote:
>>> 
>>> On Aug 10, 2017, at 02:18, Alastair Houghton  
>>> wrote:
 
 Personally I *would* still discourage +new in favour of class-specific 
 convenience constructors because I think it’s less expressive and also 
 less consistent (e.g. +array is better, in my opinion, than +new, not 
 least because +arrayWithObjects: and others exist).
>>> 
>>> [NSArray new] := [[NSArray alloc]init]
>>> 
>>> [NSArray array] := [[[NSArray alloc]init]autorelease]
>>> 
>>> +array and friends came along with the introduction of autorelease pools, 
>>> to replace +new with something that didn't imply ownership (the oft 
>>> mentioned special meaning of "new" as prefix). So while with ARC they are 
>>> essentially equivalent, previously they were not.
>> 
>> Yes, I know that, thanks.
>> 
>> The point is, with ARC, they’re equivalent, and most new code uses ARC, so, 
>> again, I’d discourage +new in favour of class-specific convenience 
>> constructors.
>> 
>> Kind regards,
>> 
>> Alastair.
> 
> They’re equivalent syntactically, but performance-wise, +array and friends 
> will cause the object to be put into an autorelease pool. Therefore, +new is 
> better for performance.

Putting an object into an autorelease pool is very cheap. It is just a write of 
the pointer value in a thread local array. When using ARC, the object is still 
pushed in the autoreleased stack, but it may be immediately pop by the caller. 
So yes, +array still have a couple of additional operations to perform,  but it 
should be mesure to tell if this is significant performance wise.

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Is "-init" really needed?

2017-08-10 Thread John McCall
> On Aug 10, 2017, at 11:13 AM, Charles Srstka  wrote:
>> On Aug 10, 2017, at 9:44 AM, Alastair Houghton 
>>  wrote:
>> 
>> On 10 Aug 2017, at 15:24, Jeremy Hughes > > wrote:
>>> 
 On 10 Aug 2017, at 15:15, Alastair Houghton > wrote:
 
 On 10 Aug 2017, at 15:09, Charles Srstka > wrote:
> 
> They’re equivalent syntactically, but performance-wise, +array and 
> friends will cause the object to be put into an autorelease pool. 
> Therefore, +new is better for performance.
 
 Not with ARC they don’t.  The ARC logic circumvents the autorelease pool 
 in that case.
>>> 
>>> Are you sure?
>> 
>> Yes, I’m sure.  At the call site, ARC causes the compiler to emit a call to 
>> objc_retainAutoreleasedReturnValue() or 
>> objc_unsafeClaimAutoreleasedReturnValue(), while in the method itself, ARC 
>> will use objc_autoreleaseReturnValue() or 
>> objc_retainAutoreleaseReturnValue().  The latter looks at the code for the 
>> call site and, assuming it matches, it will *not* do the autorelease and 
>> will set a flag that causes objc_retainAutoreleasedReturnValue() to 
>> eliminate the retain.
> 
> The frameworks (and thus, the implementation of +array) are not built using 
> ARC. The -autorelease method is called manually, and the object is put in the 
> autorelease pool. You can see this for yourself by making a small test app 
> that calls [NSMutableArray array] and running it in Instruments, where the 
> autorelease will be clearly visible.

Autoreleases done by -autorelease can still be reclaimed by 
objc_retainAutoreleasedReturnValue.

The autorelease-reclaim optimization is not reliable, and there are a number of 
caveats that make testing it tricky.  That is why we generally refer to it as 
an optimization, rather than claiming it as a semantic guarantee.  In your test 
app, you are almost certainly running into a well-known problem where the first 
attempt to reclaim an autorelease fails on x86-64 (because of a detail of 
dynamic linking).

John.
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Is "-init" really needed?

2017-08-10 Thread Charles Srstka
> On Aug 10, 2017, at 9:44 AM, Alastair Houghton  
> wrote:
> 
> On 10 Aug 2017, at 15:24, Jeremy Hughes  > wrote:
>> 
>>> On 10 Aug 2017, at 15:15, Alastair Houghton >> > wrote:
>>> 
>>> On 10 Aug 2017, at 15:09, Charles Srstka >> > wrote:
 
 They’re equivalent syntactically, but performance-wise, +array and friends 
 will cause the object to be put into an autorelease pool. Therefore, +new 
 is better for performance.
>>> 
>>> Not with ARC they don’t.  The ARC logic circumvents the autorelease pool in 
>>> that case.
>> 
>> Are you sure?
> 
> Yes, I’m sure.  At the call site, ARC causes the compiler to emit a call to 
> objc_retainAutoreleasedReturnValue() or 
> objc_unsafeClaimAutoreleasedReturnValue(), while in the method itself, ARC 
> will use objc_autoreleaseReturnValue() or 
> objc_retainAutoreleaseReturnValue().  The latter looks at the code for the 
> call site and, assuming it matches, it will *not* do the autorelease and will 
> set a flag that causes objc_retainAutoreleasedReturnValue() to eliminate the 
> retain.

The frameworks (and thus, the implementation of +array) are not built using 
ARC. The -autorelease method is called manually, and the object is put in the 
autorelease pool. You can see this for yourself by making a small test app that 
calls [NSMutableArray array] and running it in Instruments, where the 
autorelease will be clearly visible.

Charles

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Is "-init" really needed?

2017-08-10 Thread Alastair Houghton
On 10 Aug 2017, at 15:24, Jeremy Hughes  wrote:
> 
>> On 10 Aug 2017, at 15:15, Alastair Houghton  
>> wrote:
>> 
>> On 10 Aug 2017, at 15:09, Charles Srstka  wrote:
>>> 
>>> They’re equivalent syntactically, but performance-wise, +array and friends 
>>> will cause the object to be put into an autorelease pool. Therefore, +new 
>>> is better for performance.
>> 
>> Not with ARC they don’t.  The ARC logic circumvents the autorelease pool in 
>> that case.
> 
> Are you sure?

Yes, I’m sure.  At the call site, ARC causes the compiler to emit a call to 
objc_retainAutoreleasedReturnValue() or 
objc_unsafeClaimAutoreleasedReturnValue(), while in the method itself, ARC will 
use objc_autoreleaseReturnValue() or objc_retainAutoreleaseReturnValue().  The 
latter looks at the code for the call site and, assuming it matches, it will 
*not* do the autorelease and will set a flag that causes 
objc_retainAutoreleasedReturnValue() to eliminate the retain.

> We had a discussion about autorelease pools recently, and it seems that 
> they’re still used by Cocoa APIs (to support ARC and non-ARC code).

Yes, autorelease pools still exist and are still used.  Not all code uses ARC, 
and for the above optimisation to happen, the code needs to use the functions 
mentioned, which will only happen if either (a) it’s compiled with ARC, or (b) 
those calls are used explicitly.  You can see the code for this in the 
Objective-C runtime code, https://opensource.apple.com/source/objc4/objc4-709/runtime/objc-object.h.auto.html>
 (search for SUPPORT_RETURN_AUTORELEASE).

Kind regards,

Alastair.

--
http://alastairs-place.net

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Is "-init" really needed?

2017-08-10 Thread Jeremy Hughes
> On 10 Aug 2017, at 15:15, Alastair Houghton  
> wrote:
> 
> On 10 Aug 2017, at 15:09, Charles Srstka  wrote:
>> 
>> They’re equivalent syntactically, but performance-wise, +array and friends 
>> will cause the object to be put into an autorelease pool. Therefore, +new is 
>> better for performance.
> 
> Not with ARC they don’t.  The ARC logic circumvents the autorelease pool in 
> that case.

Are you sure?

We had a discussion about autorelease pools recently, and it seems that they’re 
still used by Cocoa APIs (to support ARC and non-ARC code).

Jeremy

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Is "-init" really needed?

2017-08-10 Thread Alastair Houghton
On 10 Aug 2017, at 15:09, Charles Srstka  wrote:
> 
> They’re equivalent syntactically, but performance-wise, +array and friends 
> will cause the object to be put into an autorelease pool. Therefore, +new is 
> better for performance.

Not with ARC they don’t.  The ARC logic circumvents the autorelease pool in 
that case.

Kind regards,

Alastair.

--
http://alastairs-place.net

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Is "-init" really needed?

2017-08-10 Thread Charles Srstka
>  On Aug 10, 2017, at 8:59 AM, Alastair Houghton 
>  wrote:
> 
> On 10 Aug 2017, at 14:57, gerti-cocoa...@bitart.com 
>  wrote:
>> 
>> On Aug 10, 2017, at 02:18, Alastair Houghton  
>> wrote:
>>> 
>>> Personally I *would* still discourage +new in favour of class-specific 
>>> convenience constructors because I think it’s less expressive and also less 
>>> consistent (e.g. +array is better, in my opinion, than +new, not least 
>>> because +arrayWithObjects: and others exist).
>> 
>> [NSArray new] := [[NSArray alloc]init]
>> 
>> [NSArray array] := [[[NSArray alloc]init]autorelease]
>> 
>> +array and friends came along with the introduction of autorelease pools, to 
>> replace +new with something that didn't imply ownership (the oft mentioned 
>> special meaning of "new" as prefix). So while with ARC they are essentially 
>> equivalent, previously they were not.
> 
> Yes, I know that, thanks.
> 
> The point is, with ARC, they’re equivalent, and most new code uses ARC, so, 
> again, I’d discourage +new in favour of class-specific convenience 
> constructors.
> 
> Kind regards,
> 
> Alastair.

They’re equivalent syntactically, but performance-wise, +array and friends will 
cause the object to be put into an autorelease pool. Therefore, +new is better 
for performance.

Charles

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Is "-init" really needed?

2017-08-10 Thread Alastair Houghton
On 10 Aug 2017, at 14:57, gerti-cocoa...@bitart.com wrote:
> 
> On Aug 10, 2017, at 02:18, Alastair Houghton  
> wrote:
>> 
>> Personally I *would* still discourage +new in favour of class-specific 
>> convenience constructors because I think it’s less expressive and also less 
>> consistent (e.g. +array is better, in my opinion, than +new, not least 
>> because +arrayWithObjects: and others exist).
> 
> [NSArray new] := [[NSArray alloc]init]
> 
> [NSArray array] := [[[NSArray alloc]init]autorelease]
> 
> +array and friends came along with the introduction of autorelease pools, to 
> replace +new with something that didn't imply ownership (the oft mentioned 
> special meaning of "new" as prefix). So while with ARC they are essentially 
> equivalent, previously they were not.

Yes, I know that, thanks.

The point is, with ARC, they’re equivalent, and most new code uses ARC, so, 
again, I’d discourage +new in favour of class-specific convenience constructors.

Kind regards,

Alastair.

--
http://alastairs-place.net

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Is "-init" really needed?

2017-08-10 Thread gerti-cocoadev


> On Aug 10, 2017, at 02:18, Alastair Houghton  
> wrote:
> 
> Personally I *would* still discourage +new in favour of class-specific 
> convenience constructors because I think it’s less expressive and also less 
> consistent (e.g. +array is better, in my opinion, than +new, not least 
> because +arrayWithObjects: and others exist).

[NSArray new] := [[NSArray alloc]init]

[NSArray array] := [[[NSArray alloc]init]autorelease]

+array and friends came along with the introduction of autorelease pools, to 
replace +new with something that didn't imply ownership (the oft mentioned 
special meaning of "new" as prefix). So while with ARC they are essentially 
equivalent, previously they were not.

Gerd
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Is "-init" really needed?

2017-08-10 Thread Alastair Houghton
On 10 Aug 2017, at 00:28, Doug Hill  wrote:
> 
>> - Performance - it incurs an extra message send (which would have been an 
>> issue back in the day)
> 
> +new requires no extra message. It's just a shorthand for [[SomeClassname 
> alloc] init]

Er, yes it does.  It sends a +new message to the class object, which, in turn, 
sends -alloc and -init messages.  So that’s three messages rather than two.  
It’s no big deal on current machines, but it would have been back on the old 
68K boxes.  It isn’t just syntax.

>> - Clarity - [[… alloc] init] shows clearly that it’s a two step operation 
>> (some classes support being *re*-initialized, so you can call initialisers 
>> more than once; other classes don’t actually need initialising)
> 
> Some say that it's far more confusing and hard to read the alloc/init syntax.

:-D Some people say all kinds of things.  Anyway, you’ve misunderstood my list 
as *my* arguments.  I don’t necessarily agree with all of them - I was just 
pondering why it might have been discouraged.

>> - If +new was the way to go, you’d need variants of +new for each variant of 
>> -init (or you have to use [[… alloc] init] anyway)
> 
> There has never been an issue with this. +new saves you some typing for one 
> syntax but has no impact on anything else.

Except it isn’t just syntax, and having +new but then e.g. +stringWithFormat 
while not having +newWithFormat: makes little sense.

>> - The fact that convenience constructors were often written naming the 
>> object, e.g. [NSString stringWithFormat:…], [NSArray array].  +new would 
>> duplicate that, but isn’t as nice to read or look at.  OK, +new doesn’t 
>> autorelease, but still.
> 
> With ARC, autorelease behavior is essentially hidden from the developer and 
> doesn't really matter any more.

Agreed.  But I wasn’t talking about a time in which ARC even existed.  I was 
trying to suggest reasons why it might have been discouraged in the past.

Personally I *would* still discourage +new in favour of class-specific 
convenience constructors because I think it’s less expressive and also less 
consistent (e.g. +array is better, in my opinion, than +new, not least because 
+arrayWithObjects: and others exist).

Kind regards,

Alastair.

--
http://alastairs-place.net

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Is "-init" really needed?

2017-08-09 Thread Leo

I love "new".

Why type more and clutter your code when you don't have to.


Cheers,
Leo



On 8/8/17 12:45 PM, Alex Zavatone wrote:

I see it creeping back in to use with some people, but IMO, new bears too many 
references to other languages’ use of new.

It seems too close to how it would be used in other languages and may imply 
things that aren’t the best.

I think it’s a case of, “well in the olden days, new was used for stuff that 
was different.  We’ll use another word now.”

This just seems like a case of, “yeah, you can do it, but it’s probably better 
to let it rest and use another approach.”

Thoughts?  Agree?  Disagree?

- Alex Zavatone


On Aug 8, 2017, at 5:13 AM, Uli Kusterer  wrote:

On 8. Aug 2017, at 02:23, Carl Hoefs  wrote:

Is the use of +new discouraged also?

Apple have gone back and forth on this AFAIR. +new was actually the 
pre-retain/release way to create an object. So it has been discouraged since 
... OpenStep, I think? But it was never formally deprecated, and I'm seeing it 
used more and more in ARC code these days.

Cheers,
-- Uli Kusterer
"The Witnesses of TeachText are everywhere..."
http://www.zathras.de

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/zav%40mac.com

This email sent to z...@mac.com

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/leo.r%40rogers.com

This email sent to le...@rogers.com


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Is "-init" really needed?

2017-08-09 Thread Doug Hill

> On Aug 8, 2017, at 2:15 PM, Uli Kusterer  wrote:
> 
> On 8. Aug 2017, at 18:38, Doug Hill  wrote:
>> As others have mentioned, I too have never seen any evidence or statements 
>> from Apple that discourages +new or -init. Or designated initializers.
> 
> I never said anything about init or designated initializers. In fact, ObjC 
> gained support for marking designated initializers fairly recently.

I was responding to a lot of different issues in my email, not necessarily just 
yours. My apologies if it came off that way.

But, that said, designated initializers have been in the Objective-C 
Programming Guide from Apple since at least 2003 (the earliest version of the 
document I can find). And this pattern probably originated in the NeXT days. 
What are the recent developments in designated initializers?

Doug Hill
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Is "-init" really needed?

2017-08-09 Thread Doug Hill
> On Aug 9, 2017, at 3:01 AM, Alastair Houghton  
> wrote:
> 
> On 8 Aug 2017, at 17:38, Doug Hill  wrote:
>> 
>> As others have mentioned, I too have never seen any evidence or statements 
>> from Apple that discourages +new or -init.
> 
> I suspect it was ObjC programmers themselves rather than Apple/NeXT that 
> discouraged it.  As for why, well I can imagine a few reasons:
> 
> - Performance - it incurs an extra message send (which would have been an 
> issue back in the day)

+new requires no extra message. It's just a shorthand for [[SomeClassname 
alloc] init]

> - Clarity - [[… alloc] init] shows clearly that it’s a two step operation 
> (some classes support being *re*-initialized, so you can call initialisers 
> more than once; other classes don’t actually need initialising)

Some say that it's far more confusing and hard to read the alloc/init syntax.

> - If +new was the way to go, you’d need variants of +new for each variant of 
> -init (or you have to use [[… alloc] init] anyway)

There has never been an issue with this. +new saves you some typing for one 
syntax but has no impact on anything else.

> - The fact that convenience constructors were often written naming the 
> object, e.g. [NSString stringWithFormat:…], [NSArray array].  +new would 
> duplicate that, but isn’t as nice to read or look at.  OK, +new doesn’t 
> autorelease, but still.

With ARC, autorelease behavior is essentially hidden from the developer and 
doesn't really matter any more. Again, +new is unrelated to all the other class 
methods.

Doug Hill 
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Is "-init" really needed?

2017-08-09 Thread Alastair Houghton
On 8 Aug 2017, at 17:38, Doug Hill  wrote:
> 
> As others have mentioned, I too have never seen any evidence or statements 
> from Apple that discourages +new or -init.

I suspect it was ObjC programmers themselves rather than Apple/NeXT that 
discouraged it.  As for why, well I can imagine a few reasons:

- Performance - it incurs an extra message send (which would have been an issue 
back in the day)

- Clarity - [[… alloc] init] shows clearly that it’s a two step operation (some 
classes support being *re*-initialized, so you can call initialisers more than 
once; other classes don’t actually need initialising)

- If +new was the way to go, you’d need variants of +new for each variant of 
-init (or you have to use [[… alloc] init] anyway)

- The fact that convenience constructors were often written naming the object, 
e.g. [NSString stringWithFormat:…], [NSArray array].  +new would duplicate 
that, but isn’t as nice to read or look at.  OK, +new doesn’t autorelease, but 
still.

Kind regards,

Alastair.

--
http://alastairs-place.net

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Is "-init" really needed?

2017-08-08 Thread Uli Kusterer
On 8. Aug 2017, at 18:38, Doug Hill  wrote:
> As others have mentioned, I too have never seen any evidence or statements 
> from Apple that discourages +new or -init. Or designated initializers.

 I never said anything about init or designated initializers. In fact, ObjC 
gained support for marking designated initializers fairly recently.

Cheers,
-- Uli Kusterer
"The Witnesses of TeachText are everywhere..."
http://www.zathras.de

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Is "-init" really needed?

2017-08-08 Thread Steve Mills

On Aug 08, 2017, at 12:35 PM, Jeff Szuhay  wrote:

On Aug 8, 2017, at 10:12 AM, Steve Mills  wrote:
It's a term that's part of the language, so use it. I know what language I'm typing in, 
so I'm not going to confuse [NSThing new] with "new CPlusPlusClass". Even if I 
did lose my mind and not understand what I'm typing, the compiler would point out my 
mistake.

Completely agree.

With me or with the message I replied to, but you didn't quote?
 
When I see [foo new] I think C++.

Why? That's not C++ syntax.
 
I recently had a case where I needed alloc alone and init’ed the object later. 

Yes, and that's not a place where you'd use new, obviously. new is a handy 
shortcut, not a replacement for all version of alloc/init*.

Sent from iCloud's ridiculous UI, so, sorry about the formatting

 
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Is "-init" really needed?

2017-08-08 Thread Alex Zavatone

> On Aug 8, 2017, at 12:35 PM, Quincey Morris 
>  wrote:
> 
> I don’t have a problem with using “new” vs. alloc/init, but there are a 
> couple of technical issues hiding behind this, though in current practice 
> they make little difference.
> 
> 1. NSObject has “new” with a standard meaning. That means you can use “new” 
> on any subclass where it’s OK to use the simple “init” without parameters. 
> However, for classes that need parameters, or have multiple inits, there’s no 
> standard “newWithX:…” class method unless you provide it yourself.
> 
> You can’t really provide this for Cocoa classes (you could try adding it via 
> a category, but this seems like a bad idea), which means your code uses a 
> mixture of strategies to create instances, even of the same class, depending 
> on the parameters needed. If that doesn’t bother you, fine. This is purely a 
> matter of preference.
> 
> 2. In modern Obj-C, a class method beginning with “new…” is specially 
> meaningful to the compiler under ARC. By default, it has +1 ownership 
> semantics for the returned reference. Using Jens’ earlier example, that means 
> that the following are subtly different:
> 
>>  [NSArray array] // +0 semantics
>>  [NSArray new]   // +1 semantics
> 
> For custom “new…” methods, the declaration can override the default semantics 
> with a compile-time attribute, which means you cannot in general be certain 
> of the semantics without looking at the declaration.
> 
> Of course, if the *calling* code is also ARC, then it doesn’t matter, because 
> ARC keeps track of the semantics for you.


Thank you.  These concerns are exactly what I was looking for.  Thanks for 
expressing them much better than I could.





___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Is "-init" really needed?

2017-08-08 Thread Quincey Morris
I don’t have a problem with using “new” vs. alloc/init, but there are a couple 
of technical issues hiding behind this, though in current practice they make 
little difference.

1. NSObject has “new” with a standard meaning. That means you can use “new” on 
any subclass where it’s OK to use the simple “init” without parameters. 
However, for classes that need parameters, or have multiple inits, there’s no 
standard “newWithX:…” class method unless you provide it yourself.

You can’t really provide this for Cocoa classes (you could try adding it via a 
category, but this seems like a bad idea), which means your code uses a mixture 
of strategies to create instances, even of the same class, depending on the 
parameters needed. If that doesn’t bother you, fine. This is purely a matter of 
preference.

2. In modern Obj-C, a class method beginning with “new…” is specially 
meaningful to the compiler under ARC. By default, it has +1 ownership semantics 
for the returned reference. Using Jens’ earlier example, that means that the 
following are subtly different:

>   [NSArray array] // +0 semantics
>   [NSArray new]   // +1 semantics

For custom “new…” methods, the declaration can override the default semantics 
with a compile-time attribute, which means you cannot in general be certain of 
the semantics without looking at the declaration.

Of course, if the *calling* code is also ARC, then it doesn’t matter, because 
ARC keeps track of the semantics for you.

So, as I said, little practical difference.
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Is "-init" really needed?

2017-08-08 Thread Jeff Szuhay

> On Aug 8, 2017, at 10:12 AM, Steve Mills  wrote:
> 
> It's a term that's part of the language, so use it. I know what language I'm 
> typing in, so I'm not going to confuse [NSThing new] with "new 
> CPlusPlusClass". Even if I did lose my mind and not understand what I'm 
> typing, the compiler would point out my mistake.

Completely agree. 
When I see [foo new] I think C++.
When I see [[foo alloc] init], I think obj-C.

I recently had a case where I needed alloc alone and init’ed the object later. 
The necessity arose out of some cruft with querying bundle instances and I had 
to 
manage my own list of bundles. Just querying a bundle by ID gave a new instance.
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Is "-init" really needed?

2017-08-08 Thread Steve Mills
> On Aug 8, 2017, at 11:45, Alex Zavatone  wrote:
> 
> I see it creeping back in to use with some people, but IMO, new bears too 
> many references to other languages’ use of new.
> 
> It seems too close to how it would be used in other languages and may imply 
> things that aren’t the best.  
> 
> I think it’s a case of, “well in the olden days, new was used for stuff that 
> was different.  We’ll use another word now.”
> 
> This just seems like a case of, “yeah, you can do it, but it’s probably 
> better to let it rest and use another approach.”
> 
> Thoughts?  Agree?  Disagree?

I disagree. It's a term that's part of the language, so use it. I know what 
language I'm typing in, so I'm not going to confuse [NSThing new] with "new 
CPlusPlusClass". Even if I did lose my mind and not understand what I'm typing, 
the compiler would point out my mistake.

Steve via iPad

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Is "-init" really needed?

2017-08-08 Thread Alex Zavatone
I see it creeping back in to use with some people, but IMO, new bears too many 
references to other languages’ use of new.

It seems too close to how it would be used in other languages and may imply 
things that aren’t the best.  

I think it’s a case of, “well in the olden days, new was used for stuff that 
was different.  We’ll use another word now.”

This just seems like a case of, “yeah, you can do it, but it’s probably better 
to let it rest and use another approach.”

Thoughts?  Agree?  Disagree?

- Alex Zavatone

> On Aug 8, 2017, at 5:13 AM, Uli Kusterer  wrote:
> 
> On 8. Aug 2017, at 02:23, Carl Hoefs  wrote:
>> Is the use of +new discouraged also?
> 
> Apple have gone back and forth on this AFAIR. +new was actually the 
> pre-retain/release way to create an object. So it has been discouraged since 
> ... OpenStep, I think? But it was never formally deprecated, and I'm seeing 
> it used more and more in ARC code these days.
> 
> Cheers,
> -- Uli Kusterer
> "The Witnesses of TeachText are everywhere..."
> http://www.zathras.de
> 
> ___
> 
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
> 
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
> 
> Help/Unsubscribe/Update your Subscription:
> https://lists.apple.com/mailman/options/cocoa-dev/zav%40mac.com
> 
> This email sent to z...@mac.com

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Is "-init" really needed?

2017-08-08 Thread Doug Hill
As others have mentioned, I too have never seen any evidence or statements from 
Apple that discourages +new or -init. Or designated initializers. The 
Objective-C Programming Guide from Apple describes very well all of the above 
and the reasoning behind using them. Please point to evidence, such as 
documentation or statements by Apple. My fear is that further speculation 
without backup is confusing people.

Getting back to the original question…you still need -init. And on a related 
note, it’s a great shorthand to use +new.

Doug Hill


> On Aug 8, 2017, at 6:46 AM, gerti-cocoa...@bitart.com wrote:
> 
> Unlike with Swift, in Objective-C it is to no small part the developers who 
> drive how the language evolves.
> 
> +new used to be the canonical initializer in the very olden days. But then 
> folks wanted a better distinction between object allocation and object 
> initialization to make memory management a bit more mechanical and less 
> hidden. They also came up with the concept of the "designated initializer". 
> That was in the very early 80s IIRC.
> 
> This worked, but two things happened:
> 
> - having to remember designated initializers, and chaining them correctly 
> when subclassing, is a tad cumbersome. It's like having to know a secret 
> handshake to use an object. Further Objective-C never formally introduced 
> syntactic support for a designated initializer, so eventually some people 
> resorted to some weird hinting with macros. Yuk.
> 
> - ARC
> 
> So "designated initializers" became de-emphasized over time, most modern 
> objects rarely use them. And ARC now makes it less important to distinguish 
> between object allocation and object initialization. Hence the renaissance of 
> +new. And with the advent of dot notation some (like me) even started to 
> further de-emphasize that by using "MyClass.new". This is debatable I guess, 
> but I like it, because it visually distracts less from the purpose of the 
> surrounding code.
> 
> Just my 2 cents, and I may be completely wrong.
> 
> Gerd
> 
> 
>> On Aug 8, 2017, at 05:13, Uli Kusterer > > wrote:
>> 
>> On 8. Aug 2017, at 02:23, Carl Hoefs  wrote:
>>> Is the use of +new discouraged also?
>> 
>> Apple have gone back and forth on this AFAIR. +new was actually the 
>> pre-retain/release way to create an object. So it has been discouraged since 
>> ... OpenStep, I think? But it was never formally deprecated, and I'm seeing 
>> it used more and more in ARC code these days.
>> 
>> Cheers,
>> -- Uli Kusterer
>> "The Witnesses of TeachText are everywhere..."
>> http://www.zathras.de
> 

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Is "-init" really needed?

2017-08-08 Thread gerti-cocoadev
Unlike with Swift, in Objective-C it is to no small part the developers who 
drive how the language evolves.

+new used to be the canonical initializer in the very olden days. But then 
folks wanted a better distinction between object allocation and object 
initialization to make memory management a bit more mechanical and less hidden. 
They also came up with the concept of the "designated initializer". That was in 
the very early 80s IIRC.

This worked, but two things happened:

- having to remember designated initializers, and chaining them correctly when 
subclassing, is a tad cumbersome. It's like having to know a secret handshake 
to use an object. Further Objective-C never formally introduced syntactic 
support for a designated initializer, so eventually some people resorted to 
some weird hinting with macros. Yuk.

- ARC

So "designated initializers" became de-emphasized over time, most modern 
objects rarely use them. And ARC now makes it less important to distinguish 
between object allocation and object initialization. Hence the renaissance of 
+new. And with the advent of dot notation some (like me) even started to 
further de-emphasize that by using "MyClass.new". This is debatable I guess, 
but I like it, because it visually distracts less from the purpose of the 
surrounding code.

Just my 2 cents, and I may be completely wrong.

Gerd


> On Aug 8, 2017, at 05:13, Uli Kusterer  wrote:
> 
> On 8. Aug 2017, at 02:23, Carl Hoefs  wrote:
>> Is the use of +new discouraged also?
> 
> Apple have gone back and forth on this AFAIR. +new was actually the 
> pre-retain/release way to create an object. So it has been discouraged since 
> ... OpenStep, I think? But it was never formally deprecated, and I'm seeing 
> it used more and more in ARC code these days.
> 
> Cheers,
> -- Uli Kusterer
> "The Witnesses of TeachText are everywhere..."
> http://www.zathras.de
> 
> ___
> 
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
> 
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
> 
> Help/Unsubscribe/Update your Subscription:
> https://lists.apple.com/mailman/options/cocoa-dev/gerti-cocoadev%40bitart.com
> 
> This email sent to gerti-cocoa...@bitart.com

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Is "-init" really needed?

2017-08-08 Thread Steve Mills
> On Aug 8, 2017, at 05:13, Uli Kusterer  wrote:
> 
> Apple have gone back and forth on this AFAIR. +new was actually the 
> pre-retain/release way to create an object. So it has been discouraged since 
> ... OpenStep, I think? But it was never formally deprecated, and I'm seeing 
> it used more and more in ARC code these days.

Discouraged? Where? I have never seen proof of that. It's equivalent to 
alloc/init, so why would it be discouraged?

Steve via iPad

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Is "-init" really needed?

2017-08-08 Thread Uli Kusterer
On 8. Aug 2017, at 02:23, Carl Hoefs  wrote:
> Is the use of +new discouraged also?

 Apple have gone back and forth on this AFAIR. +new was actually the 
pre-retain/release way to create an object. So it has been discouraged since 
... OpenStep, I think? But it was never formally deprecated, and I'm seeing it 
used more and more in ARC code these days.

Cheers,
-- Uli Kusterer
"The Witnesses of TeachText are everywhere..."
http://www.zathras.de

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Is "-init" really needed?

2017-08-07 Thread Jens Alfke

> On Aug 7, 2017, at 5:23 PM, Carl Hoefs  wrote:
> 
> Is the use of +new discouraged also?

No, I use it all the time. It’s simply shorthand for [[XXX alloc] init].

These days with ARC, I tend to use +new instead of factory class methods — e.g. 
[NSMutableArray new] instead of [NSMutableArray array] — on the assumption that 
this avoids adding the new object to the autorelease pool. However, it’s 
possible the compiler & runtime are smart enough to optimize the autorelease 
out of the second call. (Greg?)

—Jens
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Is "-init" really needed?

2017-08-07 Thread Carl Hoefs

> On Aug 7, 2017, at 5:17 PM, じょいすじょん  
> wrote:
> 
>> 
>> On Aug 8, 2017, at 9:09, Jens Alfke  wrote:
>> 
>> 
>>> On Aug 7, 2017, at 5:02 PM, David Hoerl  wrote:
>>> 
>>> But then I though - heck, if Foo has NSObject as its super class, gee, 
>>> maybe -init isn't really need. I mean, if all of Foo's ivars and properties 
>>> are initialized, its a shortcut, right.
>> 
>> -[NSObject init] happens to be a no-op empty method. So if a direct subclass 
>> of NSObject has no -init method of its own, you could get by with just 
>> calling +alloc. However, I think this would be a really bad idea. If at some 
>> point you needed to add an -init method to class Foo, like to initialize an 
>> ivar, you’d have to go and fix all this code that wasn’t calling -init, or 
>> else you’d suddenly have a number of bugs in your code. Even worse, if 
>> someone else added the -init method and didn’t know about this quirk of how 
>> callers initialized Foo, they might have  no idea why their method didn’t 
>> get called. Yuck.
>> 
>> —Jens
>> ___
>> 
> It definitely should never pass in a code review for exactly these reasons 
> and should be fixed by either adding the init call or changing the alloc call 
> to a new call (since new is a synonym for alloc init).
> If you saw it pre-existing in code that was being checked in, require it to 
> be fixed. Refusal to type a few characters is absolutely a shortcut to 
> trouble later (Y2K).
> ___
> 

Is the use of +new discouraged also?

-Carl


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Is "-init" really needed?

2017-08-07 Thread じょいすじょん

> On Aug 8, 2017, at 9:09, Jens Alfke  wrote:
> 
> 
>> On Aug 7, 2017, at 5:02 PM, David Hoerl  wrote:
>> 
>> But then I though - heck, if Foo has NSObject as its super class, gee, maybe 
>> -init isn't really need. I mean, if all of Foo's ivars and properties are 
>> initialized, its a shortcut, right.
> 
> -[NSObject init] happens to be a no-op empty method. So if a direct subclass 
> of NSObject has no -init method of its own, you could get by with just 
> calling +alloc. However, I think this would be a really bad idea. If at some 
> point you needed to add an -init method to class Foo, like to initialize an 
> ivar, you’d have to go and fix all this code that wasn’t calling -init, or 
> else you’d suddenly have a number of bugs in your code. Even worse, if 
> someone else added the -init method and didn’t know about this quirk of how 
> callers initialized Foo, they might have  no idea why their method didn’t get 
> called. Yuck.
> 
> —Jens
> ___
> 
It definitely should never pass in a code review for exactly these reasons and 
should be fixed by either adding the init call or changing the alloc call to a 
new call (since new is a synonym for alloc init).
If you saw it pre-existing in code that was being checked in, require it to be 
fixed. Refusal to type a few characters is absolutely a shortcut to trouble 
later (Y2K).
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Is "-init" really needed?

2017-08-07 Thread Greg Parker

> On Aug 7, 2017, at 5:02 PM, David Hoerl  wrote:
> 
> I recently saw some code where an object was alloced but there was no init:
> 
>  Foo *foo = [Foo alloc];
>  foo.bar = ...
> 
> My blood pressure soared! My pulse quickened! I started breathing rapidly!
> 
> But then I though - heck, if Foo has NSObject as its super class, gee, maybe 
> -init isn't really need. I mean, if all of Foo's ivars and properties are 
> initialized, its a shortcut, right.

Pro:
* -[NSObject init] is in fact guaranteed to do nothing.

Con:
* As you noticed, independent of its correctness, the code above *looks* wrong. 
That alone is bad. It ought either to call -init or to have a big scary comment 
describing why it is safe not to.
* If Foo is ever changed to have a superclass other than NSObject, the code 
above is likely to be wrong.


-- 
Greg Parker gpar...@apple.com  Runtime 
Wrangler


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Is "-init" really needed?

2017-08-07 Thread Jens Alfke

> On Aug 7, 2017, at 5:02 PM, David Hoerl  wrote:
> 
> But then I though - heck, if Foo has NSObject as its super class, gee, maybe 
> -init isn't really need. I mean, if all of Foo's ivars and properties are 
> initialized, its a shortcut, right.

-[NSObject init] happens to be a no-op empty method. So if a direct subclass of 
NSObject has no -init method of its own, you could get by with just calling 
+alloc. However, I think this would be a really bad idea. If at some point you 
needed to add an -init method to class Foo, like to initialize an ivar, you’d 
have to go and fix all this code that wasn’t calling -init, or else you’d 
suddenly have a number of bugs in your code. Even worse, if someone else added 
the -init method and didn’t know about this quirk of how callers initialized 
Foo, they might have  no idea why their method didn’t get called. Yuck.

—Jens
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com