Don't forget about "new"

40 views
Skip to first unread message

Val K

unread,
Apr 9, 2016, 7:34:27 PM4/9/16
to RapydScript
Hi!
First of all, I want to thank Alexander for RapydScript, don't stop!
There isn't any example of creating RapydScript-class object from JS in the Нelp.
 So, here is my little experience.

# example.pyj
class Foo:
 
def  __init__(self, ...):
       
...

 
def method(self, ...):
     
...

# now, we want to store class (not object) in $  
def main():
  $
.foo_cls = Foo # - OK,  but if you're Python-fan - don't forget about "new"!, i.e. JS-call:  foo_obj= new $.foo_cls(...)

 
# The trick is that if class hasn't any method except __init__, just "foo_obj= $.foo_cls(...)" will work too!
 
# I wasted some hours to catch it ;)
 
$
(document).ready(main)



As variant (for  more Pythonic JS-call), it  could be


def main():
  $.foo_cls = def (*args):
                return new Foo(args) #  - consider args without *, JS-call just: foo_obj = $.foo_cls(...)   
  
$
(document).ready(main)

 


Alexander Tsepkov

unread,
Apr 9, 2016, 7:48:09 PM4/9/16
to Val K, RapydScript
Hi Val, glad you like the language. I'm a bit confused, are you saying that the compiler did not auto-insert the `new` keyword?

In your first example, it looks like you do not have parentheses, which is how the compiler determines to insert it (otherwise you're assigning the constructor itself to the variable rather than an instance of the created object). Or are you saying that now every call to $.foo_cls should be preceeded with `new`? That is unfortunate side-effect of how the compiler "magically" adds the "new" keyword (it basically checks to see if there is a class with that name either in local or outer scope (that has not been shadowed by a local non-class with same name)). It does not do the same magic on assignment, although it's possible to extend the language to do so. However, you would still be able to reproduce the same issue by returning the variable outside of scope, so the fix would not be trivial:

def returnFoo():
     return Foo

newIsMissingAgain = returnFoo()

Val K

unread,
Apr 10, 2016, 8:35:30 AM4/10/16
to RapydScript, valq...@gmail.com
Hi Alexander!
My message isn't about issue (compiler is fine!). It's just a "sticker-reminder" for me and may be for anyone else. It's about switching from Python (after writing some hundreds lines) to JS - you have to call to mind about braces, semicolon and "new". The problem is that the latter is hard to catch, because JS doesn't say "Hey man! you forget about `new`",  it  could be  "your object.method isn't a function" or something else, or even nothing at all!
Trouble happen when a class hasn't any method or  all methods are assigned in the __init__, in this case call $.RS_class()  causes to  self = this = $! So, you don't receive any error  but result  will be "Surprise!";).

P.S. There is an error in my post  - it should be
return new Foo(args[0], args[1], args[2] ... )
but I can't edit it (spammers and so on  -  understand you).

I have a question:
If I use **kwargs  -  it causes an error "... is not implemented yet ...",  I checked my version - it's 0.3.9  - here is the same - where can I get last version?
Reply all
Reply to author
Forward
0 new messages