Re: Protocols and the +initialize class method

2013-08-24 Thread Kevin Meaney
Thanks for the feedback everyone.

I was confusing how I was going about doing the necessary initialization to be 
able implement the protocol in a class. I've been using the +initialize method 
to do this, but this is as someone pointed out is an implementation detail. So 
adding +initialize to the protocol is not appropriate and to just leave it up 
to the developer implementing the class how to achieve the necessary 
initialization to be able to implement the protocol. I should not be foisting 
on them how I've been doing it, even when it is just me implementing the 
classes conforming to the protocol.

My take away is to be reminded to keep implementation details out of the 
protocol.

Kevin

On 23 Aug 2013, at 20:11, Glenn L. Austin  wrote:

> 
> On Aug 23, 2013, at 10:30 AM, David Duncan  wrote:
> 
>> On Aug 23, 2013, at 2:49 AM, Kevin Meaney  wrote:
>> 
>>> I have a protocol where I would like to include the class method 
>>> +(void)initialize as being required. This way all classes that conform to 
>>> the protocol have to implement  initialize as part of the protocol. I 
>>> suppose I'm wondering if anyone perceives any problem with doing this?
>>> 
>>> For example, will including +initialize as a required protocol method 
>>> interfere with objective-c automatically calling the initialize method 
>>> before the class is used for the first at runtime.
>> 
>> 
>> What is it that you are actually trying to achieve by doing this?
> 
> Especially since *every* class that derives from NSObject will have an 
> +initialize method...
> 
> 
> -- 
> Glenn L. Austin, Computer Wizard and Race Car Driver <><
> 
> 


___

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: Protocols and the +initialize class method

2013-08-23 Thread Glenn L. Austin

On Aug 23, 2013, at 10:30 AM, David Duncan  wrote:

> On Aug 23, 2013, at 2:49 AM, Kevin Meaney  wrote:
> 
>> I have a protocol where I would like to include the class method 
>> +(void)initialize as being required. This way all classes that conform to 
>> the protocol have to implement  initialize as part of the protocol. I 
>> suppose I'm wondering if anyone perceives any problem with doing this?
>> 
>> For example, will including +initialize as a required protocol method 
>> interfere with objective-c automatically calling the initialize method 
>> before the class is used for the first at runtime.
> 
> 
> What is it that you are actually trying to achieve by doing this?

Especially since *every* class that derives from NSObject will have an 
+initialize method...


-- 
Glenn L. Austin, Computer Wizard and Race Car Driver <><



___

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: Protocols and the +initialize class method

2013-08-23 Thread Jens Alfke

On Aug 23, 2013, at 10:30 AM, David Duncan  wrote:

> On Aug 23, 2013, at 2:49 AM, Kevin Meaney  wrote:
> 
>> I have a protocol where I would like to include the class method 
>> +(void)initialize as being required. This way all classes that conform to 
>> the protocol have to implement  initialize as part of the protocol. I 
>> suppose I'm wondering if anyone perceives any problem with doing this?
> 
> What is it that you are actually trying to achieve by doing this?

+1. This doesn’t make sense to me. The +initialize method is purely part of the 
internal implementation of a class; it has no external visibility or meaning. 
Protocols are all about external meaning, with no assumption of a particular 
implementation.

—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: Protocols and the +initialize class method

2013-08-23 Thread David Duncan
On Aug 23, 2013, at 2:49 AM, Kevin Meaney  wrote:

> I have a protocol where I would like to include the class method 
> +(void)initialize as being required. This way all classes that conform to the 
> protocol have to implement  initialize as part of the protocol. I suppose I'm 
> wondering if anyone perceives any problem with doing this?
> 
> For example, will including +initialize as a required protocol method 
> interfere with objective-c automatically calling the initialize method before 
> the class is used for the first at runtime.


What is it that you are actually trying to achieve by doing this?
--
David Duncan


___

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: Protocols and the +initialize class method

2013-08-23 Thread Fritz Anderson
On 23 Aug 2013, at 4:49 AM, Kevin Meaney  wrote:

> I have a protocol where I would like to include the class method 
> +(void)initialize as being required. This way all classes that conform to the 
> protocol have to implement  initialize as part of the protocol. I suppose I'm 
> wondering if anyone perceives any problem with doing this?
> 
> For example, will including +initialize as a required protocol method 
> interfere with objective-c automatically calling the initialize method before 
> the class is used for the first at runtime.

The threshold question is whether the compiler will enforce the protocol as you 
wish. This is easy to test. The following code passes analysis and runs with no 
problem:

-
#import 

@protocol NeedsInitialize 

+ (void) initialize;
- (void) mandatoryInstance: (NSString *) str;

@end

@interface NAImplementor : NSObject 

- (NSInteger) anotherMethod;

@end

@implementation NAImplementor

- (void) mandatoryInstance:(NSString *)str
{
NSLog(@"%s gets string %@", __PRETTY_FUNCTION__, str);
}

- (NSInteger) anotherMethod
{
NSLog(@"%s sending -mandatoryInstance:", __PRETTY_FUNCTION__);
[self mandatoryInstance: @"NAImplementor sends mandatory"];
return 12;
}

@end

int main(int argc, const char * argv[])
{
@autoreleasepool {
NAImplementor * instance = [[NAImplementor alloc] init];
NSLog(@"%s implementor returns %li", __PRETTY_FUNCTION__, 
(long)[instance anotherMethod]);
}
return 0;
}
-

As I understand it, you want to require NAImplementor to have +[NAImplementor 
initialize]. This code passes without this happening. It seems NSObject 
implements +initialize, which is good enough to satisfy the protocol.

"Incomplete Objective-C Protocols," GCC_WARN_ALLOW_INCOMPLETE_PROTOCOL (Warn if 
methods required by a protocol are not implemented in the class adopting it.  
Only applies to Objective-C.), which is the closest warning I can find in short 
order to what you want, is set YES, so if the warning worked as you hope, it 
should have triggered.

You might have the +load method of the class introspect to see if it implements 
+initialize, and throw an exception, but that requires a class hierarchy that 
defeats the purpose of a protocol.

— F


___

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