Scripting: User-Defined Functions

Features wanted...
Post Reply
PeterH
Posts: 2579
Joined: 21 Nov 2005 20:39
Location: Germany

Scripting: User-Defined Functions

Post by PeterH » 10 Apr 2015 01:37

It seems there's no thread for UDF till now?

First to say: happy to see it! :D :appl:
And: great idea to allow "Variable(s) by Reference" - can often be very helpful! :ugeek:

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

klownboy
Posts: 2792
Joined: 28 Feb 2012 19:27

Re: Scripting: User-Defined Functions

Post by klownboy » 10 Apr 2015 03:04

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. :oops:

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! :appl:

Filehero
Posts: 2293
Joined: 27 Feb 2012 18:50
Location: Windows 10 Pro x64

Re: Scripting: User-Defined Functions

Post by Filehero » 10 Apr 2015 06:40

PeterH wrote:First to say: happy to see it! :D :appl:
Yes, thanks a lot! :appl: :appl: :appl:

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

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

Re: Scripting: User-Defined Functions

Post by SammaySarkar » 10 Apr 2015 07:45

:tup: :appl: :tup:
A long-standing wish-come-true!

Now I know a user with an idea can be irritating, but I tried some "functionality", prompting these ideas observations:
  • "      - The first line of user function declarations has to be left-bound (not indented)."

    - This breaks multi-scripts.

    Code: Select all

    "differ"
    function diff($x, $y) { return abs($x - $y); }
    
    "C"
     echo diff(7,5);
     echo diff(10,1);
    //differ is disabled in script's menu.//

    Code: Select all

    "C"
     echo diff(7,5);
    function diff($x, $y) { return abs($x - $y); }
    
     echo diff(10,1);
    //error.//
    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."

    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); }
    
    //this works.//
    .
    .
  • "      - 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 suggestion

    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"
    maybe 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" )
    Here's another idea: fun($a,$b)<<"super">> :kidding:
But UDF's a great gift for us. It can only improve from now! :D :appl: :appl:

admin
Site Admin
Posts: 47703
Joined: 22 May 2004 16:48
Location: Cologne, Win 8.1, Win 10
Contact:

Re: Scripting: User-Defined Functions

Post by admin » 10 Apr 2015 08:15

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);

admin
Site Admin
Posts: 47703
Joined: 22 May 2004 16:48
Location: Cologne, Win 8.1, Win 10
Contact:

Re: Scripting: User-Defined Functions

Post by admin » 10 Apr 2015 10:45

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. :oops:
That has been bugging me too for too long. So (next beta):

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.

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

Re: Scripting: User-Defined Functions

Post by PeterH » 10 Apr 2015 11:11

Just had a short look. (Very snoopy :P )
And just now the new beta arrives :D

First and maybe the very best: the new Include instruction! :shock: :D :D :shock: :appl: :appl: :appl:
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 :P
(You almost(!) might name this "Macro".)


To indentation: don't understand yet - must wait for an example :oops:

I must say: this is like Christmas and Easter together :tup: :D :appl:
W7(x64) SP1 German
( +WXP SP3 )

admin
Site Admin
Posts: 47703
Joined: 22 May 2004 16:48
Location: Cologne, Win 8.1, Win 10
Contact:

Re: Scripting: User-Defined Functions

Post by admin » 10 Apr 2015 13:00

1) Yes, you can abuse it. It's totally dumb. It simply replaces itself with the contents of the file.

2) Unindent:
Original resource:

Code: Select all

        "unset var"
          $a = "Hi!";
          unset_var($a);
          echo $a;  //$a
        function unset_var(&$name) { unset $name; }        
Unindented:

Code: Select all

"unset var"
  $a = "Hi!";
  unset_var($a);
  echo $a;  //$a
function unset_var(&$name) { unset $name; }

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

Re: Scripting: User-Defined Functions

Post by SammaySarkar » 10 Apr 2015 13:33

PeterH wrote:I must say: this is like Christmas and Easter together :tup: :D :appl:
guess more new goodies are coming. First UDF, then include in the very next beta, then ...Image, can't wait!

aliteralmind
Posts: 255
Joined: 02 Dec 2014 16:49

Re: Scripting: User-Defined Functions

Post by aliteralmind » 10 Apr 2015 14:48

:appl:
Windows 8.1, 64-bit

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

Re: Scripting: User-Defined Functions

Post by binocular222 » 10 Apr 2015 15:58

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

admin
Site Admin
Posts: 47703
Joined: 22 May 2004 16:48
Location: Cologne, Win 8.1, Win 10
Contact:

Re: Scripting: User-Defined Functions

Post by admin » 10 Apr 2015 16:16

Sure? Works here. :?

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

Re: Scripting: User-Defined Functions

Post by binocular222 » 10 Apr 2015 16:34

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

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

Re: Scripting: User-Defined Functions

Post by PeterH » 10 Apr 2015 18:12

admin wrote:1) Yes, you can abuse it. It's totally dumb. It simply replaces itself with the contents of the file.
That's fine: a very intelligent form of dumbness!
admin wrote:2) Unindent:
...
Having read this I must say: I don't like it. Fine you changed it to old state.
(User having the "Label" wrong = indented has a problem :mrgreen: )
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) :oops: : 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...
W7(x64) SP1 German
( +WXP SP3 )

klownboy
Posts: 2792
Joined: 28 Feb 2012 19:27

Re: Scripting: User-Defined Functions

Post by klownboy » 10 Apr 2015 20:02

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. :wink:

Post Reply