Page 4 of 10

Re: Scripting: User-Defined Functions

Posted: 13 Apr 2015 10:05
by admin
SammaySarkar wrote:

Code: Select all

  $b = 'var_$b';
  echo q();
function q($a = "1" . "2" , $b) { return $a . | . $b ; } //returns 1" . "2|var_$b

Code: Select all

function q($a = """1" . """2" , $b) { return $a . | . $b ; } //returns "1" . "2"|var_$b
This is not allowed. A default value can just be a simple value, not a more complex expression.

Re: Scripting: User-Defined Functions

Posted: 13 Apr 2015 10:38
by PeterH
admin wrote:This is not allowed. A default value can just be a simple value, not a more complex expression.
Sorry: exactly what I expected. Either one quoted string, or a (unquoted) number.
If the number is quoted: then it's a quoted string :lol:

OK: I have one question: what's about Global Perm Variable (alone or inside double quotes). This might sometimes be useful.

Edit: silly - had to change Global to Perm!!! :oops:

Re: Scripting: User-Defined Functions

Posted: 13 Apr 2015 12:49
by admin
Would work (resolve perms) but not sure if it's good. I would certainly slow down all scripts using user functions...

Re: Scripting: User-Defined Functions

Posted: 13 Apr 2015 13:17
by PeterH
New :bug: , I think.

If a Function (here: imbedded, not included, see below) Loads a script, and the script executes Caller('File'), it doesn't return the file of the function, but the file of the current active script.
I'd await either the file of the function, or at least the file of the script that owns the function. ( 8) Oh, I think that's always the same?)

In the same situation when the script executes Caller('Caption') :arrow: "function funcname" is returned, that's very good!

==> Just to say, if it makes a difference: the Function is called without assignment of the return value: I need the parameters, not the return.
(So it's more a Command with Parameters :whistle: )

Code: Select all

'Main'
   ...
   func($p1, $p2);
   ...
Function func($a, $b) {
   ...
   Load 'script.xys'
   ...
 }
------------------------------------------------------------
To Perm in defaults: no problem, was just an idea.

Re: Scripting: User-Defined Functions

Posted: 13 Apr 2015 13:54
by admin
Did you try self()? Caller is not even documented...

Re: Scripting: User-Defined Functions

Posted: 13 Apr 2015 14:06
by PeterH
admin wrote:Did you try self()? Caller is not even documented...
But I want the opposite of self() :roll: :arrow: i.e. the info about the caller of the executing script.

(If I remember right you made Caller() because I asked? So I know :biggrin: )

OK: it's not documented, so it's not a bug. :wink:
But it would be fine if you could make it work :ninja:

Edit: tried Self('file') inside function :arrow: doesn't work, too.
Seems to show the last loaded script!

Re: Scripting: User-Defined Functions

Posted: 13 Apr 2015 15:40
by bdeshi
dereferenced variables are accepted by reference but not changed by reference.

Code: Select all

  $var = '$a';
  *$var = "refer";
  $b = "refer";
  echo '$a = ' . $a . <crlf> . '$b = '. $b ;
  echo '$a was accepted: '  . r(*$var, $b);
  echo '$a = ' . $a . <crlf> . '$b = '. $b ;
function r(&$e, &$f) {
  $e  = $e . 'red'; //$a
  $f  = $f . 'red'; //$b
  return $e; //prove acceptance
 }

Re: Scripting: User-Defined Functions

Posted: 13 Apr 2015 15:44
by admin
PeterH wrote:
admin wrote:Did you try self()? Caller is not even documented...
But I want the opposite of self() :roll: :arrow: i.e. the info about the caller of the executing script.

(If I remember right you made Caller() because I asked? So I know :biggrin: )

OK: it's not documented, so it's not a bug. :wink:
But it would be fine if you could make it work :ninja:

Edit: tried Self('file') inside function :arrow: doesn't work, too.
Seems to show the last loaded script!
This is a bit tricky. Will look at it later.

Re: Scripting: User-Defined Functions

Posted: 13 Apr 2015 15:59
by admin
SammaySarkar wrote:dereferenced variables are accepted by reference but not changed by reference.

Code: Select all

  $var = '$a';
  *$var = "refer";
  $b = "refer";
  echo '$a = ' . $a . <crlf> . '$b = '. $b ;
  echo '$a was accepted: '  . r(*$var, $b);
  echo '$a = ' . $a . <crlf> . '$b = '. $b ;
function r(&$e, &$f) {
  $e  = $e . 'red'; //$a
  $f  = $f . 'red'; //$b
  return $e; //prove acceptance
 }
Confirmed and fixed.

Re: Scripting: User-Defined Functions

Posted: 13 Apr 2015 21:12
by TheQwerty
1) What are the rules for naming namespaces?

2) Can namespace and include support single quotes, please?

3) I'm on the fence about this because it's certainly a bug but it could also be a nice feature. Given:
<xyscripts>\template.html

Code: Select all

<html><body>Hello $person!</body></html>
and <xyscripts>\Test.xys

Code: Select all

"Test"
	$person = 'World';

	$html = <<<HTML
include "template.html"
HTML;

	html($html);
The include should not be replaced since it is within a HEREDOC - and currently this breaks backwards compatibility.

Re: Scripting: User-Defined Functions

Posted: 14 Apr 2015 13:24
by TheQwerty
4) When include cannot find the specified file there is an error dialog:
Error at include: The system cannot find the file specified.

include "Lib"
The only option available to the user is "OK" which continues script execution.

I think it would be better if include was elevated to a full command similar to assert:

Code: Select all

include file, [message], [continue=0]
This way we can customize the message to tell the user where to get the include file and prevent continuing.

5) Similarly, calling a non-existent UDF shows the stepping dialog with the error:
'test::bad' is not a valid script command.
The default action when that dialog is shown is to continue which doesn't seem like the best option. To complicate matters, if the script is storing the return value of that UDF:

Code: Select all

$ret = Test::bad();
No error is shown as it is assumed that the right-hand side of the '=' is a string, thus $ret is set to "Test::bad()".

6) include should really follow the same relative path resolution rules that load does:
::help('idh_scripting_comref.htm#idh_sc_load'); wrote:(1) in the path of the calling script file (if any)
(2) in the Scripts subfolder of the application data path (variable <xydata>\Scripts)*
(3) in the application data path (variable <xydata>)
* And I sincerely hope the documentation is wrong as #2 should be checking <xyscripts> which may not be the same as <xydata>\Scripts.

7) In general we need some better error handling for detecting missing includes and UDFs.

Maybe that's in the form of try-catch:

Code: Select all

try {
  Test::bad();
} catch ($msg) { // on error - no dialog is shown, instead $msg is set to the error that would have been displayed.
  End true, $msg;
}
Or maybe it is in the form of some preemptive functions?

Code: Select all

FunExists(functionName);
// Returns true if functionName exists.
// If functionName is just a namespace 'Test::', returns true if any functions in that namespace are present.
The name should probably be better but I couldn't resist the chuckles. ;)

Or maybe it is a special user function which gets run automatically when a file is included - similar to _Initialize - in this "init" function we can set variables which can be used to detect when the file is included.

Re: Scripting: User-Defined Functions

Posted: 14 Apr 2015 13:55
by admin
TheQwerty wrote:1) What are the rules for naming namespaces?

2) Can namespace and include support single quotes, please?

3) I'm on the fence about this because it's certainly a bug but it could also be a nice feature. Given:
<xyscripts>\template.html

Code: Select all

<html><body>Hello $person!</body></html>
and <xyscripts>\Test.xys

Code: Select all

"Test"
	$person = 'World';

	$html = <<<HTML
include "template.html"
HTML;

	html($html);
The include should not be replaced since it is within a HEREDOC - and currently this breaks backwards compatibility.
1) + 2)

Code: Select all

    * Scripting: Included files and namespaces can now be double- or single- or 
      un-quoted.
    * Scripting: The names of namespaces now must conform to the same rules as 
      the names of functions: Can only consist of letters (a-z, A-Z), numbers, 
      and underscores, and must not start with a number.
3) Fixed.

Re: Scripting: User-Defined Functions

Posted: 14 Apr 2015 14:34
by admin
4) not possible here in this pre-processing phase
5) see 4)

6) yes, it's <xyscripts>; and thanks: done and pasted to the change log. :)

7) later...

Re: Scripting: User-Defined Functions

Posted: 14 Apr 2015 15:06
by TheQwerty
admin wrote:4) not possible here in this pre-processing phase
5) see 4)

6) yes, it's <xyscripts>; and thanks: done and pasted to the change log. :)

7) later...
1,2,3,6) Thanks!

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.

5) Thinking further, I suppose this is okay. It's not perfect but it's in line with pre-UDF behavior.

7) Maybe sooner for FunExists? ;) :lol:
I suppose this can be worked around by being pragmatic about includes containing a specific function that checks this:

Code: Select all

namespace "Math"
function Included() { return true; }
function sum($a=0, $b=0) { return $a + $b; }

Code: Select all

include "Math"

"Test"
  End Math::Included() != true, 'Math is not included.';
  Echo Math::sum(21,21);

Re: Scripting: User-Defined Functions

Posted: 14 Apr 2015 15:17
by admin
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!