objective c - Two class methods, same name, different signatures: How to force compiler to use intended one? -


how force compiler select desired method set of class methods sharing same name?

/* use +[myclass port](uint16 (*)(id, sel),  * not +[nsport port](nsport *(*)(id, sel)). */ uint16 port = [[self class] port]; 

i have objective-c class class method:

+ (uint16)port; 

nsport has convenience constructor signature conflicts this:

+ (nsport *)port; 

sending +port class results in compiler warning:

uint16 port = [[self class] port];     w: multiple methods named '+port' found     w: using '+(nsport *)port'     w: found '+(uint16)port' 

fail: compiler has chosen wrong method signature.

type inference fail: using [[(myclass *)self class] port] not cadge using right method.

eta: here workaround using now:

#import <objc/runtime.h>  class c = [self class]; sel s = @selector(port); typedef uint16 (*portimp)(id, sel); portimp myclassgetport = (portimp)class_getmethodimplementation(c, s); uint16 port = myclassgetport(c, s); 

it in that:

  • it handles dispatching subclass implementation correctly.
  • it confined implementation file, ugliness not inflicted on implementer.

it bad in won't else wants call method.

why not rename method? (lame, know) argue method named "port" should returning port object (which nsport does), , if wanted return primitive "port number", you'd call "portvalue" (a la "intvalue", "longlongvalue", etc).


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -