Scripting: User-Defined Functions
Scripting: User-Defined Functions
It seems there's no thread for UDF till now?
First to say: happy to see it!
And: great idea to allow "Variable(s) by Reference" - can often be very helpful!
I don't yet understand the location in e.g. a multiscript file. I think such an UDF should be a "resource" like a hidden script. But as it's definition doesn't begin in column 1 I don't understand how the organization in a multi-script file with UDF should be.
Question: if UDF returns a value it will be called like
$x = func(1, 2);
This makes no sense, if not returning a value - it will be like a command. Will it be
func(1, 2); // or
func 1 2; // as XYplorer commands like Box "00FFFF" "Desktop";
Next (maybe I'm too fast - but before I'm too late):
description shows how to define and call UDF from the local script. And this surely will be one way to use it. But: it also would be fine to create a kind of "library" of often used UDFs to be called from different scripts. I.e. define script files with UDF(s) to be called from other scripts. I hope this is planned?
No time for tests now - but hope I can proudly start testing next weekend...
First to say: happy to see it!
And: great idea to allow "Variable(s) by Reference" - can often be very helpful!
I don't yet understand the location in e.g. a multiscript file. I think such an UDF should be a "resource" like a hidden script. But as it's definition doesn't begin in column 1 I don't understand how the organization in a multi-script file with UDF should be.
Question: if UDF returns a value it will be called like
$x = func(1, 2);
This makes no sense, if not returning a value - it will be like a command. Will it be
func(1, 2); // or
func 1 2; // as XYplorer commands like Box "00FFFF" "Desktop";
Next (maybe I'm too fast - but before I'm too late):
description shows how to define and call UDF from the local script. And this surely will be one way to use it. But: it also would be fine to create a kind of "library" of often used UDFs to be called from different scripts. I.e. define script files with UDF(s) to be called from other scripts. I hope this is planned?
No time for tests now - but hope I can proudly start testing next weekend...
Win11 Pro 223H2 Gerrman
Re: Scripting: User-Defined Functions
I missed the beta notes on function placement at the beginning of the line (no indent) so copy and pasting the examples didn't work, but it works fine now that I know.
I agree with PeterH, at some point it would be great to be able to call functions from a library of collected functions from within scripts.
And by the way, this is great! Thanks!
I agree with PeterH, at some point it would be great to be able to call functions from a library of collected functions from within scripts.
And by the way, this is great! Thanks!
Windows 11, 23H2 Build 22631.3447 at 100% 2560x1440
Re: Scripting: User-Defined Functions
Yes, thanks a lot!PeterH wrote:First to say: happy to see it!
And for sure, being able to set up a library is a must! I expect the first shared one to come up here quite soon. ;-)
Unfortunately, busy with other things I have to join the party later......
Cheers,
Filehero
Re: Scripting: User-Defined Functions
A long-standing wish-come-true!
Now I know a user with an idea can be irritating, but I tried some "functionality", prompting these
" - The first line of user function declarations has to be left-bound (not indented)."
- This breaks multi-scripts.//differ is disabled in script's menu.//Code: Select all
"differ" function diff($x, $y) { return abs($x - $y); } "C" echo diff(7,5); echo diff(10,1);
//error.//Code: Select all
"C" echo diff(7,5); function diff($x, $y) { return abs($x - $y); } echo diff(10,1);
Seems all functions have to be define at the end of a subscript, even though "User functions can be defined anywhere in a script resource, before or after the main script, and as many as you want."//this works.//Code: Select all
"C" echo diff(7,5); echo sum(10,1); function diff($x, $y) { return abs($x - $y); } function sum($x, $y) { return ($x + $y); }
.
.
" - User functions can be defined anywhere in a script resource, before or after the main script, and as many as you want." " - All user functions have global scope." " - If two or more user functions share the same name, the *first* one declared will be used, all other ones ignored."
- This is liable to cause conflicts. Please allow these two:
(1) function in current subscript has highest priority.
(2) give us function namespacing so we can choose which function to call in a script. Here's a hurried syntax suggestionmaybe an end-delimiter should be used to define end of subscript name, so string concats don't affect it (say, "fun: " . fun()@"super"@ . "fun" ; //wouldn't call fun() from "superfun" )Code: Select all
fun($a,$b)@"*"; //call fun() from current subscript fun($a,$b)@"#2"; //call fun() from current script's 2nd subscript fun($a,$b)@"super"; //call fun() from subscript labelled "super"
Here's another idea: fun($a,$b)<<"super">>
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]
[ this user is asleep ]
-
- Site Admin
- Posts: 60558
- Joined: 22 May 2004 16:48
- Location: Win8.1 @100%, Win10 @100%
- Contact:
Re: Scripting: User-Defined Functions
SammaySarkar wrote:Seems all functions have to be define at the end of a subscript, even though "User functions can be defined anywhere in a script resource, before or after the main script, and as many as you want.
No, it works when done right. Think of functions as invisible scripts in multi-script. Example:
Code: Select all
function multiply($factor1, $factor2) { return $factor1 * $factor2; }
"Test 1"
echo multiply(3, 4) . <crlf> . sum(11,12);
function sum($x, $y) {
$z = $x + $y;
return $z;
}
function div($x, $y) { return $x / $y; }
"Test 2"
echo div(10, 3);
FAQ | XY News RSS | XY Twitter
-
- Site Admin
- Posts: 60558
- Joined: 22 May 2004 16:48
- Location: Win8.1 @100%, Win10 @100%
- Contact:
Re: Scripting: User-Defined Functions
That has been bugging me too for too long. So (next beta):klownboy wrote:I missed the beta notes on function placement at the beginning of the line (no indent) so copy and pasting the examples didn't work, but it works fine now that I know.
Code: Select all
* Scripting: Now loaded scripts are automatically unindented, i.e. the first
line's indent is removed from all lines. That way you can easier copy and
paste scripts, e.g. from this change log, without the need to manually
left-align the code blocks first.
FAQ | XY News RSS | XY Twitter
Re: Scripting: User-Defined Functions
Just had a short look. (Very snoopy )
And just now the new beta arrives
First and maybe the very best: the new Include instruction!
Immediate question: can this be misused at will? By "Including" a piece of straight code (i.e. some scripting stmts) into the middle of a script? I would love this
(You almost(!) might name this "Macro".)
To indentation: don't understand yet - must wait for an example
I must say: this is like Christmas and Easter together
And just now the new beta arrives
First and maybe the very best: the new Include instruction!
Immediate question: can this be misused at will? By "Including" a piece of straight code (i.e. some scripting stmts) into the middle of a script? I would love this
(You almost(!) might name this "Macro".)
To indentation: don't understand yet - must wait for an example
I must say: this is like Christmas and Easter together
Win11 Pro 223H2 Gerrman
-
- Site Admin
- Posts: 60558
- Joined: 22 May 2004 16:48
- Location: Win8.1 @100%, Win10 @100%
- Contact:
Re: Scripting: User-Defined Functions
1) Yes, you can abuse it. It's totally dumb. It simply replaces itself with the contents of the file.
2) Unindent:
Original resource:
Unindented:
2) Unindent:
Original resource:
Code: Select all
"unset var"
$a = "Hi!";
unset_var($a);
echo $a; //$a
function unset_var(&$name) { unset $name; }
Code: Select all
"unset var"
$a = "Hi!";
unset_var($a);
echo $a; //$a
function unset_var(&$name) { unset $name; }
FAQ | XY News RSS | XY Twitter
Re: Scripting: User-Defined Functions
guess more new goodies are coming. First UDF, then include in the very next beta, then ..., can't wait!PeterH wrote:I must say: this is like Christmas and Easter together
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]
[ this user is asleep ]
-
- Posts: 261
- Joined: 02 Dec 2014 16:49
Re: Scripting: User-Defined Functions
Windows 8.1, 64-bit
-
- Posts: 1416
- Joined: 04 Nov 2008 05:35
- Location: Hanoi, Vietnam
Re: Scripting: User-Defined Functions
This not work (return 1 instead of 2)
Code: Select all
Example 3: Argument by Reference
$a = 1; add_one($a); echo $a; //2
function add_one(&$a) {
$a++;
}
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
-
- Posts: 1416
- Joined: 04 Nov 2008 05:35
- Location: Hanoi, Vietnam
Re: Scripting: User-Defined Functions
Oh, it returns 2 now. weird.
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
That's fine: a very intelligent form of dumbness!admin wrote:1) Yes, you can abuse it. It's totally dumb. It simply replaces itself with the contents of the file.
Having read this I must say: I don't like it. Fine you changed it to old state.admin wrote:2) Unindent:
...
(User having the "Label" wrong = indented has a problem )
More often I see the opposite: missing indentation for script line after copy.
But now I've learned: include must start in column 1! Verified with binocular222's example.
One (rather old) wish (from Load) : for include it would be very fine, if it also could replace itself with the contents of a variable (instead of a file). With this you could build a kind of script-generator. (Again: name it "Macro ability".) If it's not too difficult...
Win11 Pro 223H2 Gerrman
Re: Scripting: User-Defined Functions
So the file extension for the "include" file can be anything (i.e., in your examples you used ".inc"). You said by default XY will use the xys extension. I assume that's if the user did not include an extension when specifying the include file. I ask only because I intentionally added the "xys" to the "math3.inc" thinking I needed it, but obviously it didn't work. So, the extension for the include can be anything the user specifies. If he doesn't have an extension XY by default adds the ".xys" to find the file in <xyscripts>. If I got that right.
Windows 11, 23H2 Build 22631.3447 at 100% 2560x1440