Page 1 of 1

Conditional inclusion of subroutine

Posted: Mon Nov 16, 2020 11:11 am
by CrisisStudent
Hi,

I am writing add-in and as one option I allow users to specify their own modified subroutine to perform as part of the add-in. The motivation is to allow users to easily incorporate their custom procedure into the add-in that performs many other functions. However, when doing this I am encountering limitations on what is allowed in Eviews. My first attempt was to include the subroutine only if add-in argument is specified, e.g.

Code: Select all

Select all if arg="T" then include subname.prg endif

This does not seem to be allowed in Eviews.

The alternative approach was to specify variable path from which to include subroutine and use default path if subroutine program does not exist in the specified location; e.g.

Code: Select all

Select all if arg="T" then %subourtine_path = "C:\\documents" else %subourtine_path = "default_path" endif include %subourinte_path

However, it seems that inclusion command cannot use strings, but has to have hardcoded path.

Therefore, the only solution I could figure out was to include default subroutine program from the add-in folder, which the user then would have to modify, i.e. user would not be able to use subroutine program of his own choosing.

Is there any suggested way one can allow conditional inclusion of subroutines or variable path of subroutine files?

Thanks!
CrisisStudent

Re: Conditional inclusion of subroutine

Posted: Tue Nov 17, 2020 5:45 pm
by EViews Matt
Hello,

The include command is unlike other commands in that all includes are processed when a program is loaded, before the program actually begins executing. Consequently, includes cannot benefit from conditional statements or program variables. You can work around this limitation by splitting the program into two parts, a first part that writes the dynamic information (the desired include statements) to a fixed-name auxiliary program, and a second part the includes said auxiliary program and performs the rest of the work. As a working example, suppose I have two different versions of a subroutine, only one of which I want to include in my main program at runtime.

version1.prg:

Code: Select all

subroutine get_message(string %m) %m = "Low" endsub
version2.prg:

Code: Select all

subroutine get_message(string %m) %m = "High" endsub
Unfortunately, I cannot write my main program as follows:

Code: Select all

create u 1 series x = rnd if x(1) < .5 then include version1.prg else include version2.prg endif call get_message(%msg) @uiprompt(%msg)
Instead, I will split my main program and have the first part use a text object to build and save a list of inclusions that the second part will intake.

part1.prg:

Code: Select all

create u 1 series x = rnd text include_list if x(1) < .5 then include_list.append include version1.prg else include_list.append include version2.prg endif include_list.save dynamic_includes.prg run part2.prg
part2.prg:

Code: Select all

include dynamic_includes.prg call get_message(%msg) @uiprompt(%msg)
Following this approach, your add-in should be to accept user-provided code without unduly limiting the user.

Re: Conditional inclusion of subroutine

Posted: Wed Nov 18, 2020 1:59 am
by CrisisStudent
Thanks Matt, this should work!