Scripting: User-Defined Functions

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

Re: Scripting: User-Defined Functions

Post by binocular222 » 27 Apr 2015 03:33

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

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

Re: Scripting: User-Defined Functions

Post by SammaySarkar » 27 Apr 2015 05:48

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 » 27 Apr 2015 08:39

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: 49053
Joined: 22 May 2004 16:48
Location: Cologne, Win 8.1, Win 10
Contact:

Re: Scripting: User-Defined Functions

Post by admin » 27 Apr 2015 08:56

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 | Stay home! 🎸 Wear a mask! 😷

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

Re: Scripting: User-Defined Functions

Post by SammaySarkar » 01 May 2015 19:28

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: 2588
Joined: 21 Nov 2005 20:39
Location: Germany

Re: Scripting: User-Defined Functions

Post by PeterH » 01 May 2015 21:39

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 )

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

Re: Scripting: User-Defined Functions

Post by SammaySarkar » 02 May 2015 05:54

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: 49053
Joined: 22 May 2004 16:48
Location: Cologne, Win 8.1, Win 10
Contact:

Re: Scripting: User-Defined Functions

Post by admin » 02 May 2015 08:18

The namespace feature will stay "unofficial/in freeze" for the time being.
FAQ | XY News RSS | XY Twitter | Stay home! 🎸 Wear a mask! 😷

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

Re: Scripting: User-Defined Functions

Post by Marco » 02 May 2015 11:06

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: 2588
Joined: 21 Nov 2005 20:39
Location: Germany

Re: Scripting: User-Defined Functions

Post by PeterH » 02 May 2015 11:36

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 )

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

Re: Scripting: User-Defined Functions

Post by SammaySarkar » 02 May 2015 11:46

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: 49053
Joined: 22 May 2004 16:48
Location: Cologne, Win 8.1, Win 10
Contact:

Re: Scripting: User-Defined Functions

Post by admin » 02 May 2015 12:54

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.
FAQ | XY News RSS | XY Twitter | Stay home! 🎸 Wear a mask! 😷

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

Re: Scripting: User-Defined Functions

Post by SammaySarkar » 02 May 2015 14:12

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 » 02 May 2015 14:21

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: 2588
Joined: 21 Nov 2005 20:39
Location: Germany

Re: Scripting: User-Defined Functions

Post by PeterH » 02 May 2015 14:52

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