Opened 4 years ago

Closed 4 years ago

#800 closed enhancement (fixed)

Supporting sub-routine in OmegaScript

Reported by: Gaurav Arora Owned by: Olly Betts
Priority: normal Milestone:
Component: Omega Version:
Severity: normal Keywords:
Cc: Blocked By:
Blocking: Operating System: All

Description (last modified by Gaurav Arora)

I was wondering why does OmegaScript have a way to define a sub-routine which can be called multiple times to avoid repetition while writing omegascript template.

For example, if have a subroutine which needs to be executed multiple times. it would be interesting to define a subroutine which can be used multiple times and probably make writing omega-script easy.

$function{$list{$map{$opt{ARGUMENT1}, $lower{$_}, , .}, ARGUMENT1}

So this will define a subroutine. We can call this and this will get a value and the apply map and list. This might be required multiple times. Hence defining sub-routine might be more convenient than copy-pasting same code multiple times.

Is there a reason why it cannot be supported in OmegaScript?

Change History (3)

comment:1 by Gaurav Arora, 4 years ago

Description: modified (diff)

comment:2 by Olly Betts, 4 years ago

Component: OtherOmega
Type: defectenhancement

We already have "macros" - I guess you searched for "function" and didn't spot these? Perhaps we should note in the docs they offer a "function-like" feature or something like that.

$def{MACRONAME,VALUE}
        define a macro which can take 0 to 9 arguments.  You can call it with
        ``$MACRONAME`` (if it take 0 arguments) or
        ``$MACRONAME{ARG1,ARG2,ARG3}`` is it takes arguments.  In value,
        arguments are available as ``$1``, ``$2``, ...  ``$9``.  In the current
        implementation, macros can override OmegaScript commands, but this
        shouldn't be relied on.  It's recommended to use capitalised names for
        macros to avoid collision with future OmegaScript commands.

E.g. you can do:

$def{PRETTYLIST,$list{$map{$1,$lower{$_}},$. }.}

$PRETTYLIST{$cgilist{FOO}}

$PRETTYLIST{$cgilist{BAR}}

It seems you're thinking to pass parameters via $set{...} which seems less nice than being able to call a macro with parameters just like you'd call a built-in OmegaScript command.

Is there anything you need that macros don't offer?

The main limitations are probably that they don't currently support lazy evaluation, and there's no checking for the number of arguments passed. The mechanisms for these exist as macros use the same machinery as built-in commands - the hardest part is probably coming up a clean way to specify them when defining a macro.

comment:3 by Gaurav Arora, 4 years ago

Resolution: fixed
Status: newclosed

Yes, macros cover the use-case I had in mind initially. Thanks Olly.

Note: See TracTickets for help on using tickets.