Comments (10)
@dimaqq The call would be:
UIWindow.alloc().initWithFrame(UIScreen.mainScreen.bounds)
The first argument is always a positional argument; all subsequent arguments are keywords.
from rubicon-objc.
Pythonista's objc_util
module supports calling Objective-C methods using keyword arguments like this, and it definitely helps with readability. Sounds like a good idea to me.
This would also be a good use case for PEP 468's order-preserving keyword arguments, which are available as of Python 3.6. Ordered keyword arguments are not necessary for this feature though, they are just an optimization - on older Python versions, the correct method name would need to be guessed, by trying every possible permutation of the keyword arguments, until a valid method name is found.
from rubicon-objc.
Completely agreed that this sounds like a good idea - it's just a matter of implementation. Since argument order isn't guaranteed (until Python 3.6, anyway), that means the lookup process will be (a) O(2^n) complexity for the number of arguments, and (b) potentially ambiguous, because the method method(arga=1, argb=2)
will match methodWithArga:Argb:
and methodWithArgb:Arga:
.
I'm not sure if the latter will be a problem in practice - and it becomes a non-issue under Python3.6 - but it's worth keeping in mind.
There's also a minor issue of how initWithArgA:ArgB
maps to a method call; it would be nice if the method was init(argA=..., argB=...)
; but we might need to compromise with initWith(argA=..., argB=...)
- unless of course, the "with" becomes a magic keyword in method names.
from rubicon-objc.
To keep it simple at first, maybe it would be good to require initWithArgA(..., argB=...)
. Then the only thing that needs to be done is inserting the :
s after each selector part (after initWithArgA
and argB
). More advanced "name guessing" could be added later. It would also halve the "guessing time" on Pythons with unordered kwargs.
from rubicon-objc.
Quick update - I've played around with this a little and implemented a basic version of this feature (in my local copy of the repo). The current implementation is not very good though. Right now, when any nonexistant attribute is accessed on an ObjCInstance
, it is always interpreted as a method call with kwargs, even if there is no method that starts with that name. This breaks a couple of tests that require nonexistant attributes to raise an AttributeError
. I also think it's not a good solution to magically make every attribute exist.
As a (hopefully better) alternative I'll try caching all instance methods when an ObjCClass
is created. This way, we know in advance which method names exist, and can create attributes for those specifically. I don't know how well this will perform, since the entire method list of the class and all superclasses needs to be looked through. At least it only needs to be done once per class, so if there is a noticeable performance impact, it will only be when each class is first loaded.
from rubicon-objc.
Okay, PR has been made. The way I implemented it now uses sets as dictionary keys to look up the method for a set of kwargs. This doesn't depend on PEP 468, so it should work equally well on all Python versions. I haven't tested the performance of the new syntax much, but the unit tests run in about the same time as before, so hopefully it shouldn't affect the speed of existing code much.
from rubicon-objc.
So, now that PR is merged, what's the new way to do the following?
UIWindow.alloc().initWithFrame_(UIScreen.mainScreen.bounds)
Or are we not there yet?
from rubicon-objc.
Oh, cool, I didn't realise that.
Would be nice to have that documented :)
Here's what I can do now (it works, I hope I understood why):
- rv = view.dequeueReusableCellWithReuseIdentifier_forIndexPath_("knob", path)
+ rv = view.dequeueReusableCellWithReuseIdentifier("knob", forIndexPath=path)
from rubicon-objc.
Documentation is definitely needed (but that's true of most of Rubicon...) - but AFAICT, this has now been implemented. If I'm mistaken, please re-open this ticket.
from rubicon-objc.
Reopening this for now - there are two suggestions from the OP that are not yet supported:
- Leaving out the
with
parts of theinit
selector:DGFoo.alloc().init(foo="spam", bar="ham")
translates to[[DGFoo alloc] initWithFoo:@"spam", bar:@"ham"]
- Leaving out the explicit
.alloc().init(...)
entirely:DGFoo(foo="spam", bar="ham")
also translates to the above
IMHO both of these would be good to have as they would make object creation shorter and more readable. This is also basically what Swift does when it imports init
methods from Objective-C.
from rubicon-objc.
Related Issues (20)
- Enforce `restype` and `argtypes` for `send_super` HOT 2
- I am passing the correct number of arguments, however stilling hitting "takes 7 arguments, but got 5 arguments" HOT 5
- Improve repr() calls for NSPoint/NSRect types
- Calling performSelector for a @objc_method created by rubicon from Objective C crashes in Python 3.9 HOT 3
- Blocks not fully working on M1 hardware HOT 5
- "ObjC Class 'b'WrappedPyObject'' couldn't be found." when running on iOS device HOT 7
- iOS event loop blocks when thread waits on socket.
- x86_64 machine architecture not identified correctly on iOS simulator
- Attribute intermittently returns the wrong object HOT 5
- Segmentation fault when exiting multi-threaded program
- Race condition when instantiating ObjCInstance objects
- Race condition when populating the ObjCClass method/attribute cache
- Update CI configuration to test release artefacts
- Improve correspondence between ObjectiveC objects and Python wrappers HOT 17
- New caching logic may inadvertently release objects that are still needed HOT 5
- ctypes.ArgumentError message is split into individual characters
- Warning about deprecated `pkg_resources` API
- Incompatibility with `from __future__ import annotations` HOT 1
- Incompatibility with Python 3.12.0b1 HOT 4
- Scroll blocks eventloop for ios HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rubicon-objc.