Scripting: User-Defined Functions

Features wanted...
binocular222
Posts: 1416
Joined: 04 Nov 2008 05:35
Location: Hanoi, Vietnam

Re: Scripting: User-Defined Functions

Post 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.
I'm a casual coder using AHK language. All of my xys scripts:
http://www.xyplorer.com/xyfc/viewtopic. ... 243#p82488

bdeshi
Posts: 4249
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612 / Dhaka
Contact:

Re: Scripting: User-Defined Functions

Post 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"
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

Stef123

Re: Scripting: User-Defined Functions

Post 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:

admin
Site Admin
Posts: 60357
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: Scripting: User-Defined Functions

Post 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.

bdeshi
Posts: 4249
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612 / Dhaka
Contact:

Re: Scripting: User-Defined Functions

Post 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; }
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

PeterH
Posts: 2776
Joined: 21 Nov 2005 20:39
Location: Germany

Re: Scripting: User-Defined Functions

Post 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.)
W7(x64) SP1 German
( +WXP SP3 )

bdeshi
Posts: 4249
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612 / Dhaka
Contact:

Re: Scripting: User-Defined Functions

Post 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.
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

admin
Site Admin
Posts: 60357
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: Scripting: User-Defined Functions

Post by admin »

The namespace feature will stay "unofficial/in freeze" for the time being.

Marco
Posts: 2347
Joined: 27 Jun 2011 15:20

Re: Scripting: User-Defined Functions

Post 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 ") {".
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]

PeterH
Posts: 2776
Joined: 21 Nov 2005 20:39
Location: Germany

Re: Scripting: User-Defined Functions

Post 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: )
W7(x64) SP1 German
( +WXP SP3 )

bdeshi
Posts: 4249
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612 / Dhaka
Contact:

Re: Scripting: User-Defined Functions

Post 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.
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

admin
Site Admin
Posts: 60357
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: Scripting: User-Defined Functions

Post 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.

bdeshi
Posts: 4249
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612 / Dhaka
Contact:

Re: Scripting: User-Defined Functions

Post by bdeshi »

what if the INCLUDEd content has heredocs?
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

Stef123

Re: Scripting: User-Defined Functions

Post 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?

PeterH
Posts: 2776
Joined: 21 Nov 2005 20:39
Location: Germany

Re: Scripting: User-Defined Functions

Post 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
W7(x64) SP1 German
( +WXP SP3 )

Post Reply