From: Samuele P. <ped...@bl...> - 2004-03-05 00:48:16
|
At 00:38 05.03.2004 +0100, Michael Haggerty wrote: >Wouldn't it be nice if Runnable-like objects could be auto-instantiated >from callable Jython objects, similar to event listeners? I'm thinking >of something like > > def f(): > # do something here > > EventThread.invokeLater(Runnable(run=f)) > >or even > > EventThread.invokeLater(run=f) > >Obviously it would be easy to write a Jython wrapper class with a >callable as a constructor argument, but a "magical" solution that would >apply generally to other similar interfaces would be clearer and save a >lot of typing. > >I couldn't find a discussion of this sort of thing in the archives but I >can't be the first person to wish it were possible... > >Does such a thing sound practicable? If the experts see no obvious >problems I might poke around to see if I can figure out how to implement >this within Jython. the problem is that the syntax has already a meaning for concrete classes, for X a concrete Java class X(**kws) === mk(X,**kws) where def mk(cl,**kws): x = cl() for name,val in kws.items(): setattr(x,name,val) return x now your proposal is for A an interface (maybe an abstract class) to have: A(**kws) === subclassnmk(A,**kws) where subclassmk(A,**kws): class Anonym(A): pass a = Anonym() for name,val in kws.items(): setattr(a,name,val) return a now one can argue that right now A(**kws) and A() for an interface or abstract class are just an error, but the problem is that the subclassmk semantics would make sense also for a concrete class, so IMO the syntax cannot be reused directly because it would be confusing. It's easy enough to write new = subclassmk and use it as: EventThread.invokeLater(new(Runnable,run=f)) and this way it works unambiguously even for A concrete. regards. |