Skip to content

Commit e46b9ab

Browse files
author
hartsantler
committed
allow new -> syntax to auto bind method's this.
1 parent 62dee2d commit e46b9ab

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ def setup_my_jquery_class( $ ):
9999
$.fn.someclass = myclass_init
100100
```
101101

102+
6. `->` can be used to as a special attribute operator for passing methods that will automatically bind
103+
the method's `this` calling context. This enables you to pass methods as callbacks to other objects,
104+
and not have to write `a.some_method.bind(a)`
105+
```
106+
b.set_callback( a->some_method )
107+
```
108+
102109
Speed
103110
---------------
104111
PythonJS gives you the option to optimize your program for speed with a new syntax for static typing, in some cases this results in code that is 20X faster.
@@ -349,6 +356,7 @@ pythonjs.configure(
349356
Gotchas
350357
---------
351358
1. The calling context of `this` must be taken into account when using fast javascript mode, code that comes after: `pythonjs.configure(javascript=True)` or is inside a `with javascript:` block. When in javascript mode, passing a method as a callback, or setting it as an attribute on another object, requires you call `f.bind(self)` to ensure that `self` within the method points to the class instance. This is not required when using classes defined normal mode, because the `this` calling context is automatically managed.
359+
Note: you can use the special `->` syntax in place of the attribute operator `.` to call `bind` automatically.
352360

353361
```
354362
class A:
@@ -365,10 +373,12 @@ with javascript:
365373
b = B()
366374
a.b_method1 = b.method
367375
a.b_method2 = b.method.bind(b)
376+
a.b_method3 = b->method
368377
369378
a.method() ## OK: prints a
370379
a.b_method1() ## FAILS: prints a, should have printed b
371380
a.b_method2() ## OK: prints b
381+
a.b_method3() ## OK: prints b
372382
373383
b.a_method = a.method
374384
b.a_method() ## OK: prints a

pythonjs/typedpython.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ def transform_source( source, strip=False ):
9191
c += ')' * c.count(' new ')
9292
c = c.replace(' new ', ' new(')
9393

94+
if '->' in c:
95+
a,b = c.split('->')
96+
this_name = a.split()[-1].split('=')[-1].split(':')[-1].split(',')[-1]
97+
method_name = b.split()[0].split('(')[0]
98+
c = c.replace('->'+method_name, '.'+method_name+'.bind(%s)'%this_name)
99+
94100
## jquery ##
95101
## TODO ensure this is not inside quoted text
96102
if '$(' in c:
@@ -117,6 +123,11 @@ def transform_source( source, strip=False ):
117123
if True:
118124
float* def Y():
119125
pass
126+
127+
A.callback = B->method
128+
A.do_something( x,y,z, B->method )
129+
A.do_something( x,y,z, callback=B->method )
130+
120131
'''
121132

122133
if __name__ == '__main__':

0 commit comments

Comments
 (0)