Scripting: User-Defined Functions

Features wanted...
Post Reply
TheQwerty
Posts: 4353
Joined: 03 Aug 2007 22:30

Re: Scripting: User-Defined Functions

Post by TheQwerty »

admin wrote:
TheQwerty wrote:4) Okay, but I'm not sure OK is really the best action then. If the include file cannot be found I think it's safer to end the script than proceed.
Agreed!
Sorry, but it needs a little bit of polish.

It's okay when the including resource is a script, but when it is a script file we get two dialogs:
Dialog 1: XYplorer wrote:---------------------------
XYplorer
---------------------------
Error at include: The system cannot find the path specified.



include 'Test\Libs'
---------------------------
OK
---------------------------
(Look at all those empty lines - seems to be CR CR LF CR CR LF?)

Followed by:
Dialog 2: XYplorer wrote:---------------------------
XYplorer
---------------------------
Script file '<xyscripts>\Test\Test.xys' does not contain any valid lines.
---------------------------
OK
---------------------------
The second dialog is redundant and confusing since its message is false.

That second dialog also has two forms:
1) The stepping dialog, if the script file was called via the Load SC or the equivalent Load Script File UDC.
2) An error dialog, if the script file was loaded via Scripting > Load Selected Script File.

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

Re: Scripting: User-Defined Functions

Post by admin »

Ah yes, I probably should have tested the code. :mrgreen:
FAQ | XY News RSS | XY Twitter | Stay home! 🎸 Or wear a mask! 😷

TheQwerty
Posts: 4353
Joined: 03 Aug 2007 22:30

Re: Scripting: User-Defined Functions

Post by TheQwerty »

Can namespace-less function calls give preference first to functions in the same namespace before checking the global namespace?

EDIT:
Actually.. thinking on it that might be problematic because it doesn't leave a way to access the global function. The second approach of using '*' might be better since it leaves both accessible.
/edit

Code: Select all

function hello() { echo 'go away'; }

namespace a
function hello() { echo 'a says hi!'; }
function test() { hello(); }

a::test();
Alternately, perhaps '*' could be used within a namespace to mean function in the same namespace?

Code: Select all

function hello() { echo 'go away'; }

namespace a
function hello() { echo 'a says hi!'; }
function test() { *::hello(); } // Note the * here!

a::test();
In both cases I'd want the result to show 'a says hi!'.

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

Re: Scripting: User-Defined Functions

Post by admin »

What about using ::hello(); (nothing before ::) to mean the global function, and hello(); to mean the local function (same namespace)?
FAQ | XY News RSS | XY Twitter | Stay home! 🎸 Or wear a mask! 😷

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

Re: Scripting: User-Defined Functions

Post by Filehero »

admin wrote:What about using ::hello(); (nothing before ::) to mean the global function, and hello(); to mean the local function (same namespace)?
Since in .NET this prefix is also used for static aka global calls, it makes sense to me.

TheQwerty
Posts: 4353
Joined: 03 Aug 2007 22:30

Re: Scripting: User-Defined Functions

Post by TheQwerty »

admin wrote:What about using ::hello(); (nothing before ::) to mean the global function, and hello(); to mean the local function (same namespace)?
That sounds alright to me.

TheQwerty
Posts: 4353
Joined: 03 Aug 2007 22:30

Re: Scripting: User-Defined Functions

Post by TheQwerty »

:bug: XY can crash when you start using a dozen or more arguments.

Code: Select all

function Test1($_1,$_2,$_3,$_4,$_5,$_6,$_7,$_8,$_9,$_10,$_11) { echo 'Test1'; }
function Test2($_1,$_2,$_3,$_4,$_5,$_6,$_7,$_8,$_9,$_10,$_11,$_12) { echo 'Test2'; }
function Test3($_1,$_2,$_3,$_4,$_5,$_6,$_7,$_8,$_9,$_10,$_11,$_12,$_13) { echo 'Test3'; }

"Test"
  SaveSettings;

  // All OK but #3 should probably cause an error.
  Test1(1,2,3,4,5,6,7,8,9,10);
  Test1(1,2,3,4,5,6,7,8,9,10,11);
  Test1(1,2,3,4,5,6,7,8,9,10,11,12);

  // OK
  Test2(1,2,3,4,5,6,7,8,9,10,11);
  // Rarely crashes.
  Test2(1,2,3,4,5,6,7,8,9,10,11,12);
  // Always crashes.
  Test2(1,2,3,4,5,6,7,8,9,10,11,12,13);

  // Always crashes.
  Test3(1,2,3,4,5,6,7,8,9,10,11);
  Test3(1,2,3,4,5,6,7,8,9,10,11,12);
  Test3(1,2,3,4,5,6,7,8,9,10,11,12,13);
  Test3(1,2,3,4,5,6,7,8,9,10,11,12,13,14);

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

Re: Scripting: User-Defined Functions

Post by admin »

TheQwerty wrote::bug: XY can crash when you start using a dozen or more arguments.
Yes. Will be handled smoother in next beta.
FAQ | XY News RSS | XY Twitter | Stay home! 🎸 Or wear a mask! 😷

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

Re: Scripting: User-Defined Functions

Post by admin »

TheQwerty wrote:
admin wrote:What about using ::hello(); (nothing before ::) to mean the global function, and hello(); to mean the local function (same namespace)?
That sounds alright to me.
OK, but it will remain a point on the to do list. Implementing this would be a lot of work. Needs more thought.
FAQ | XY News RSS | XY Twitter | Stay home! 🎸 Or wear a mask! 😷

TheQwerty
Posts: 4353
Joined: 03 Aug 2007 22:30

Re: Scripting: User-Defined Functions

Post by TheQwerty »

admin wrote:
TheQwerty wrote:
admin wrote:What about using ::hello(); (nothing before ::) to mean the global function, and hello(); to mean the local function (same namespace)?
That sounds alright to me.
OK, but it will remain a point on the to do list. Implementing this would be a lot of work. Needs more thought.
That's unfortunate... Guess I'll just have to be extra careful in selecting my namespace names or be ready to search & replace when I change them. ;)

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

Re: Scripting: User-Defined Functions

Post by admin »

Simply give each function its own name... :)
FAQ | XY News RSS | XY Twitter | Stay home! 🎸 Or 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 »

admin wrote:What about using ::hello(); (nothing before ::) to mean the global function, and hello(); to mean the local function (same namespace)?
except this edge case (just to annoy you) when the script starts with :: (because it was converted from a one liner) and the very first statement is a function call:

Code: Select all

::q();
function q() {echo "a";}
:whistle:
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

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

Re: Scripting: User-Defined Functions

Post by admin »

There is a way out:

Code: Select all

call ::q();
function q() {echo "a";}
FAQ | XY News RSS | XY Twitter | Stay home! 🎸 Or 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 »

Code: Select all

function q() { return 1; }
echo q();
happens.

Code: Select all

echo q();
function q() { return 1; }
happens.

Code: Select all

function q() { return 1; }
 echo q();
nothing happens.

Code: Select all

 echo q();
function q() { return 1; }
happens.
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

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

Re: Scripting: User-Defined Functions

Post by SammaySarkar »

I really need include cmd to be available for one-liners. I've saved some frequent functions as an inc file, but none can be used in addressbar quickscripts because include wants to eat the whole line.

It's possible to script an alternative but nothing as simple as include "inc\xys.inc"
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

Post Reply