Anonymous functions

37 views
Skip to first unread message

chernobylmu...@gmail.com

unread,
Apr 14, 2016, 11:34:56 AM4/14/16
to RapydScript
Hi!

I started out with JS and rapydscript at the same time. When he reached the topic of anonymous functions, I was faced with the following problem.

def ask(q,y,n):
   
if confirm(q):
        y
()
   
else:
        n
()


def show_ok():
    alert
('OK')


def show_cansel():
    alert
('CANCEL')


ask
('You are ok?', show_ok, show_cansel)

it work!

but if I try to pass a function as an argument...

ask('You are OK?',
def(): alert('OK'),
def(): alert('CANCEL'))

I see the following error
C:\Users\Timofey\RapydScript\lib\parse.js:148 throw new JS_Parse_Error(message, line, col, pos, is_eof); ^ Error at new JS_Parse_Error (C:\Users\Timofey\RapydScript\lib\parse.js:135:18) at js_error (C:\Users\Timofey\RapydScript\lib\parse.js:148:11) at croak (C:\Users\Timofey\RapydScript\lib\parse.js:788:9) at token_error (C:\Users\Timofey\RapydScript\lib\parse.js:793:9) at unexpected (C:\Users\Timofey\RapydScript\lib\parse.js:799:9) at C:\Users\Timofey\RapydScript\lib\parse.js:1040:17 at C:\Users\Timofey\RapydScript\lib\parse.js:830:20 at block_ (C:\Users\Timofey\RapydScript\lib\parse.js:1733:24) at C:\Users\Timofey\RapydScript\lib\parse.js:1668:21 at function_ (C:\Users\Timofey\RapydScript\lib\parse.js:1675:14)


how to pass function as arguments?

Alexander Tsepkov

unread,
Apr 14, 2016, 11:38:11 AM4/14/16
to chernobylmu...@gmail.com, RapydScript
This is explained in the documentation: https://github.com/atsepkov/RapydScript#anonymous-functions

Being indentation-based, the compiler has no way of knowing if that comma means you're terminating the function or continuing it on next line. You should add a semi-colon at the end of the function definition:

ask('You are OK?',
def(): alert('OK');,
def(): alert('CANCEL');)
Reply all
Reply to author
Forward
0 new messages