[Rubycocoa-devel 296] Re: message dispatch syntaxes

アーカイブの一覧に戻る

kimura wataru kimur****@i*****
Mon Jun 19 23:29:08 JST 2006


Hi,

On Mon, 19 Jun 2006 14:25:12 +0200, Laurent Sansonetti wrote:
> Hi all,
> 
> I recently had a long discussion with colleagues (that are using  
> PyObjC) about the current messages dispatchers in RubyCocoa.  You  
> know that right now you can do:
> 
> 1/ obj.aaa_bbb_(123, 456)
> 2/ obj.aaa_bbb(123, 456)
> 3/ obj.aaa(123, :bbb, 456)
> 4/ obj.aaa(123, :bbb => 456)	# only in the unstable branch
> 
> One of the problems about 2/ 3/ and 4/ is that they can lead to  
> ambiguities in (very rare) cases [1]. Some people are afraid about  
> this, and would prefer to see only 1/ supported. I did my best to  
> defend the current syntaxes...
> 

I prefer 2/ rather than 1/. 1/ is ugly.

> [1]:
> 
> Omitting the trailing underscore can lead to confusions, for ex:
> 
>    [MyClass +foo]
>    [MyClass +foo:(id)foo]
> 
> Calling MyClass.foo() can be ambiguous for the user, which method is  
> he calling? Basing on the given number of arguments is also  
> ambiguous, we could pass an exploded array (*[]) and we would call  
> different methods based on the array value.
> 

An ObjC method that takes variable number of arguments requires one 
argument at least to know the number of arguments. All of Cocoa variadic 
methods belong to either of the following types.

(A)format string (such as [NSPredicate predicateWithFormat:])
   needs format string for first argument
(B)array of values (such as [NSArray arrayWithObjects:])
   needs nil for last argument

So I believe that 2/ works without any problem.

(A)====
Foundation.framework/Headers/NSException.h:+ (void)raise:(NSString *)name format:(NSString *)format, ...;
Foundation.framework/Headers/NSPredicate.h:+ (NSPredicate *)predicateWithFormat:(NSString *)predicateFormat, ...;
Foundation.framework/Headers/NSString.h:- (NSString *)stringByAppendingFormat:(NSString *)format, ...;
Foundation.framework/Headers/NSString.h:- (id)initWithFormat:(NSString *)format, ...;
Foundation.framework/Headers/NSString.h:- (id)initWithFormat:(NSString *)format locale:(NSDictionary *)dict, ...;
Foundation.framework/Headers/NSString.h:+ (id)stringWithFormat:(NSString *)format, ...;
Foundation.framework/Headers/NSString.h:+ (id)localizedStringWithFormat:(NSString *)format, ...;
Foundation.framework/Headers/NSString.h:- (void)appendFormat:(NSString *)format, ...;
AppKit.framework/Headers/NSAlert.h:+ (NSAlert *)alertWithMessageText:(NSString *)message defaultButton:(NSString *)defaultButton alternateButton:(NSString *)alternateButton otherButton:(NSString *)otherButton informativeTextWithFormat:(NSString *)format, ...;

Foundation.framework/Headers/NSCoder.h:- (void)encodeValuesOfObjCTypes:(const char *)types, ...;
Foundation.framework/Headers/NSCoder.h:- (void)decodeValuesOfObjCTypes:(const char *)types, ...;

(B)====
Foundation.framework/Headers/NSArray.h:+ (id)arrayWithObjects:(id)firstObj, ...;
Foundation.framework/Headers/NSArray.h:- (id)initWithObjects:(id)firstObj, ...;
Foundation.framework/Headers/NSDictionary.h:+ (id)dictionaryWithObjectsAndKeys:(id)firstObject, ...;
Foundation.framework/Headers/NSDictionary.h:- (id)initWithObjectsAndKeys:(id)firstObject, ...;
Foundation.framework/Headers/NSSet.h:+ (id)setWithObjects:(id)firstObj, ...;
Foundation.framework/Headers/NSSet.h:- (id)initWithObjects:(id)firstObj, ...;

-- 
kimura wataru



More information about the Rubycocoa-devel mailing list
アーカイブの一覧に戻る