Scripting: User-Defined Functions
-
- Posts: 1416
- Joined: 04 Nov 2008 05:35
- Location: Hanoi, Vietnam
Re: Scripting: User-Defined Functions
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.
I'm a casual coder using AHK language. All of my xys scripts:
http://www.xyplorer.com/xyfc/viewtopic. ... 243#p82488
http://www.xyplorer.com/xyfc/viewtopic. ... 243#p82488
Re: Scripting: User-Defined Functions
Actually if the include file has only functions, then including it at the END requires no messing with indentation: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:(Don't forget to indent the $a= in this case...)Code: Select all
include "_functions.inc" "Main" $a = <<<>>> hello world >>>; text $a;
Code: Select all
text makefun("grumpywords");
include "inc\makefun.xyi"
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]
[ this user is asleep ]
Re: Scripting: User-Defined Functions
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.
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.
-
- Site Admin
- Posts: 60585
- Joined: 22 May 2004 16:48
- Location: Win8.1 @100%, Win10 @100%
- Contact:
Re: Scripting: User-Defined Functions
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.
FAQ | XY News RSS | XY Twitter
Re: Scripting: User-Defined Functions
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; }
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]
[ this user is asleep ]
Re: Scripting: User-Defined Functions
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.)
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.)
Win11 Pro 223H2 Gerrman
Re: Scripting: User-Defined Functions
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.
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]
[ this user is asleep ]
-
- Site Admin
- Posts: 60585
- Joined: 22 May 2004 16:48
- Location: Win8.1 @100%, Win10 @100%
- Contact:
Re: Scripting: User-Defined Functions
The namespace feature will stay "unofficial/in freeze" for the time being.
FAQ | XY News RSS | XY Twitter
Re: Scripting: User-Defined Functions
1. Where should I place the include statement? I tried thisand "2nd" shows up greyed out. Howeverworks fine.
2. Something like thisis not parsed correctly. The debug shows a ") {".
Code: Select all
"1st"
echo "1st!";
"2nd"
include func.inc
echo silly_message();
"3rd"
echo "3rd!";
Code: Select all
"1st"
echo "1st!";
"2nd"
echo silly_message();
include func.inc
"3rd"
echo "3rd!";
2. Something like this
Code: Select all
function myfunction($items = get("SelectedItemsPathNames", "|")) {
//...various code...
}
Last edited by Marco on 02 May 2015 16:49, edited 1 time in total.
Tag Backup - SimpleUpdater - XYplorer Messenger - The Unofficial XYplorer Archive - Everything in XYplorer
Don sees all [cit. from viewtopic.php?p=124094#p124094]
Don sees all [cit. from viewtopic.php?p=124094#p124094]
Re: Scripting: User-Defined Functions
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
(An include may just refer to some plane lines, without any label. These must be located where these lines are needed )
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
(An include may just refer to some plane lines, without any label. These must be located where these lines are needed )
Win11 Pro 223H2 Gerrman
Re: Scripting: User-Defined Functions
1) the line immediately following a function has to unindented, as if the function were a subscript. Therefore, this will work: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.orOTOH, 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.
Code: Select all
"2nd"
include func.inc
echo silly_message(); // dedented
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();
Code: Select all
eval();
FUNCTION () {}
FUNCTION () {}
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.
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]
[ this user is asleep ]
-
- Site Admin
- Posts: 60585
- Joined: 22 May 2004 16:48
- Location: Win8.1 @100%, Win10 @100%
- Contact:
Re: Scripting: User-Defined Functions
I see you found workaround for point 1), but I thought it would be better it just worked as expected. Tricky! 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.
- Makes scripts look better.
- Also included stuff will now inherit the indent of the include statement.
FAQ | XY News RSS | XY Twitter
Re: Scripting: User-Defined Functions
what if the INCLUDEd content has heredocs?
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]
[ this user is asleep ]
Re: Scripting: User-Defined Functions
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?admin wrote:- Also included stuff will now inherit the indent of the include statement.
Re: Scripting: User-Defined Functions
Hm. Do you think of situations where I includeadmin wrote:I see you found workaround for point 1), but I thought it would be better it just worked as expected. Tricky! 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.
- 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?
OK: just guessing what you might think - I may be wrong
Win11 Pro 223H2 Gerrman