Re: [Pyobjc-dev] Default to returning (void)?
Brought to you by:
ronaldoussoren
From: Bob I. <bo...@re...> - 2004-02-16 04:37:15
|
On Feb 15, 2004, at 10:38 PM, b.bum wrote: > > This is going to break a few things, but better to rip off the > band-aid now than wait.... > > Currently, if I declare a standard IB action or set method (such as > the following).... > > def setContent_(self, someContent): > print "Setting content...." > self._content = someContent > > .... I have to immediately follow the declaration with the following > ... > > setContent_ = selector(setContent_, signature='v@:@') > > ... if I wait the method to be compatible with Key/Value Observation > or otherwise match the signature of the typical setter or IBAction. > > When PyObjC was originally written -- 1994 or so -- the NeXTSTEP > standard was for Obj-C methods to default to returning (id) -- to end > with 'return self;'. The goal was to support a Lisp Like method > chaining of the [foo doBar] doBaz] doBob] sayFred] form. > > When OpenStep rolled around, the ObjC world shifted to methods > defaulting to not returning anything. That is, to be declared as > returning (void). This has many advantages for very little cost. > In particular, it means that remote invocation models-- cross thread > or inter-process-- can actually do one-way calls without having to > carry a proxy back across the wire. It also means that apps didn't > explode because a developer assumes 'return self' when a method really > returns something else (something that happened a number of times!). > > Of course, with Python we have no notion of "return type". A method > may or may not return anything. Right now, PyObjC assumes a return > of (id). In ObjC parlance, the assumed signature prefix is '@@:'. > > Is there any way that we can make it a bit more natural for the > developer to declare IBActions or setters in PyObjC such that the > resulting methods have signature 'v@:@'? The only way I can think of is to analyze the bytecode before wrapping functions.. >>> import dis >>> def setContent_(self, someContent): ... print "Setting content..." ... self._content = someContent ... >>> dis.dis(setContent_) 2 0 LOAD_CONST 1 ('Setting content...') 3 PRINT_ITEM 4 PRINT_NEWLINE 3 5 LOAD_FAST 1 (someContent) 8 LOAD_FAST 0 (self) 11 STORE_ATTR 2 (_content) 14 LOAD_CONST 0 (None) 17 RETURN_VALUE At least in Python 2.3.x, the only way to generate "LOAD_CONST 0 (None) / RETURN_VALUE" is either a bare return, or no return at all. If you analyze the bytecode for a function, and it has no other RETURN_VALUE pairs.. then you can safely assume that it should default to void. -bob |