Page 7 of 10

Re: Scripting: User-Defined Functions

Posted: 27 Apr 2015 03:33
by binocular222
We can have a default.inc, which is always loaded in RAM. Scripts doesn't need to explicitly include this file. The "include" line is only needed for user-created *.inc other than default.inc.

Re: Scripting: User-Defined Functions

Posted: 27 Apr 2015 05:48
by bdeshi
PeterH wrote:In the conversations we had over time, I came to the conclusion that every script should have a Caption / Label, so that I think you are on the safe side with:

Code: Select all

include "_functions.inc"

"Main"
    $a = <<<>>>
hello world
>>>;

    text $a;
(Don't forget to indent the $a= in this case...)
Actually if the include file has only functions, then including it at the END requires no messing with indentation:

Code: Select all

  text makefun("grumpywords");
include "inc\makefun.xyi"

Re: Scripting: User-Defined Functions

Posted: 27 Apr 2015 08:39
by Stef123
Thanks highend, PeterH, Sammay,
indentation was indeed the culprit. It didn't occur to me, because the script worked fine will all those indents, until I added the include-line to it.

Those whitespace rules keep driving me out of my mind. After adding my own heredoc sections - strictly by copy & paste & overwrite - it stopped working. Took me a long time and zillions of WinMerge compares to track this one down: one single invisible blank at the end of my heredoc-closing. Must have happened during copy & paste, part of the editor's setting to do "smart pasting" to keep inserted words apart.

I had known all along that whitespace up front is a tricky business, but whitespace at the very end never was a problem - until this incident. :twisted:

Re: Scripting: User-Defined Functions

Posted: 27 Apr 2015 08:56
by admin
Well, these are the rules of HEREDOC. It's the same in PHP and it makes sense IMO. Tip: Let your editor mark white spaces and the guessing is over.

Re: Scripting: User-Defined Functions

Posted: 01 May 2015 19:28
by bdeshi
Please allow using variables to refer to namespaces (somehow)

Code: Select all

$math = 'math';
$math::multiply(4,5);

Code: Select all

NAMESPACE thisNS
FUNCTION first($f){
  $ns = self('namespace'); //wished elsewhere
  return $f*1 + $ns::second($f);
}
FUNCTION second($a){
  return $a*2;
}
NAMESPACE somewhereElse
function second($a){ return 0; }

Re: Scripting: User-Defined Functions

Posted: 01 May 2015 21:39
by PeterH
I'm still not convinced of using namespaces. Why not give them a unique name?

Can you explain?

(The situation I understand is to be able to differentiate between XY-native and User. Though I think I'd even use that rarely, if at all.)

Re: Scripting: User-Defined Functions

Posted: 02 May 2015 05:54
by bdeshi
when I want to call a function in the same namespace from another function.

And if this:   $ns = self('namespace');   were possible, I wouldn't have to write self(namespace) a lot.

Re: Scripting: User-Defined Functions

Posted: 02 May 2015 08:18
by admin
The namespace feature will stay "unofficial/in freeze" for the time being.

Re: Scripting: User-Defined Functions

Posted: 02 May 2015 11:06
by Marco
1. Where should I place the include statement? I tried this

Code: Select all

"1st"

 echo "1st!";

"2nd"

include func.inc

 echo silly_message();

"3rd"

 echo "3rd!";
and "2nd" shows up greyed out. However

Code: Select all

"1st"

 echo "1st!";

"2nd"

 echo silly_message();

include func.inc

"3rd"

 echo "3rd!";
works fine.

2. Something like this

Code: Select all

function myfunction($items = get("SelectedItemsPathNames", "|")) {
//...various code...
}
is not parsed correctly. The debug shows a ") {".

Re: Scripting: User-Defined Functions

Posted: 02 May 2015 11:36
by PeterH
You should place such an include - i.e. an include that includes complete functions, where you would place the functions themselves.

If you place an include at the very beginning: make sure the main part starts with a label. (You should do this anyway!)
You may easy place the include at the very end.
And you may place include somewhere in the middle - in front of a line with a label.

I hope I got it all :P

(An include may just refer to some plane lines, without any label. These must be located where these lines are needed :biggrin: )

Re: Scripting: User-Defined Functions

Posted: 02 May 2015 11:46
by bdeshi
1) the line immediately following a function has to unindented, as if the function were a subscript. Therefore, this will work:

Code: Select all

"2nd"

include func.inc

echo silly_message(); // dedented
Because of this rule, and because functions can be accessed from the whole script regardless of where and when they are defined, I suggest you either:
a) define all functions before the main script, and start the main script with an unindented line or caption, or
b) define all functions after all of the main script content. In this case the main script's first line can be indented, because everything till the next unindented line is processed as the script.

Code: Select all

FUNCTION () {}
FUNCTION () {}
"script"
  eval();
or

Code: Select all

  eval();
FUNCTION () {}
FUNCTION () {}
OTOH, INCLUDE itself must be written without indentation, but whether it's surrounding code be un-/indented depends on the included content.

2) http://www.xyplorer.com/xyfc/viewtopic. ... 50#p122820 default values for FUNCTION parameters only resolve XY or systemnative variables (<var>, %var%), but no function calls or other variables.

Re: Scripting: User-Defined Functions

Posted: 02 May 2015 12:54
by admin
I see you found workaround for point 1), but I thought it would be better it just worked as expected. Tricky! :evil: Needed a larger rewrite. Side-effect: Now include, function, and namespace statements can be indented.
- Makes scripts look better.
- Also included stuff will now inherit the indent of the include statement.

Re: Scripting: User-Defined Functions

Posted: 02 May 2015 14:12
by bdeshi
what if the INCLUDEd content has heredocs?

Re: Scripting: User-Defined Functions

Posted: 02 May 2015 14:21
by Stef123
admin wrote:- Also included stuff will now inherit the indent of the include statement.
Does that mean I won't be seeing menu items when including a "menu"-script? To my understanding, for menu items to be visible, they have to start flush left?

Re: Scripting: User-Defined Functions

Posted: 02 May 2015 14:52
by PeterH
admin wrote:I see you found workaround for point 1), but I thought it would be better it just worked as expected. Tricky! :evil: Needed a larger rewrite. Side-effect: Now include, function, and namespace statements can be indented.
- Makes scripts look better.
- Also included stuff will now inherit the indent of the include statement.
Hm. Do you think of situations where I include
- a file with a (maybe hidden) script, i.e. not ending on a }
- a file with just a few plain statements, i.e. neither (labeled) script nor function (for inside a script)

And I hope you don't want to say that script files look better when you can't optically realize where scripts start? :mrgreen:

OK: just guessing what you might think - I may be wrong :P