Author Topic: Request: Return runtime-generated variables from WeiDU functions  (Read 3300 times)

Offline Argent77

  • Planewalker
  • *****
  • Posts: 187
It's currently not possible to return more than a fixed list of variables defined by the RET keyword from WeiDU functions. This limitation makes it impossible to return dynamic data structures, such as arrays, where the final number and/or name may not be known in advance.

Would it be feasible to add a feature that allows us to return dynamically created variables, e.g. by using a new keyword that adds them to the RET list at runtime?

Example:
Code: [Select]
DEFINE_PATCH_FUNCTION array_generator
INT_VAR
  array_size = 10
RET
  array
BEGIN
  FOR (i = 0; i < array_size; ++i) BEGIN
    SET EVAL ~array_%i%~ = i
    EXPORT ~array_%i%~    // mark variable "array_%i%" as a return value
  END
  SET array = array_size
END

Currently only "array" will be defined outside the function. Evaluating "%array_0%" will only result in the string "%array_0%".

Offline c4_angel

  • Planewalker
  • *****
  • Posts: 30
Re: Request: Return runtime-generated variables from WeiDU functions
« Reply #1 on: January 28, 2018, 12:53:41 PM »
Or maybe a new command like LAUNCH_ACTION_FUNCTION_OUTER/LAFO, which forcing the functions run in the global scope instead off a closed one?

Offline Wisp

  • Moderator
  • Planewalker
  • *****
  • Posts: 1176
Re: Request: Return runtime-generated variables from WeiDU functions
« Reply #2 on: January 31, 2018, 01:25:52 PM »
I can probably add a RET_ARRAY keyword that essentially does a PHP_EACH over the specified array name and returns those variables.

Offline Wisp

  • Moderator
  • Planewalker
  • *****
  • Posts: 1176
Re: Request: Return runtime-generated variables from WeiDU functions
« Reply #3 on: February 10, 2018, 06:06:48 AM »
Done.

Code: [Select]
DEFINE_ACTION_FUNCTION foobar
  RET_ARRAY
    foo
    bar
BEGIN
  ACTION_DEFINE_ARRAY foo BEGIN 1 2 3 END
  ACTION_DEFINE_ARRAY bar BEGIN a b c END
END

LAF foobar RET_ARRAY foo baz = bar END

// After the function has returned, the arrays foo and baz will exist
// The array baz will be derived from the array bar

ACTION_PHP_EACH foo AS _ => v BEGIN
  PRINT "%v%" // will print 1, 2 and 3
END

ACTION_PHP_EACH baz AS _ => v BEGIN
  PRINT "%v%" // will print a, b and c
END

Offline Argent77

  • Planewalker
  • *****
  • Posts: 187
Re: Request: Return runtime-generated variables from WeiDU functions
« Reply #4 on: February 10, 2018, 07:10:34 AM »
Great work!

After a bit of trial and error I was able to manage even arrays of dynamic size.

With the new feature, the example from my first post would look like this:
Code: [Select]
DEFINE_PATCH_FUNCTION array_generator
INT_VAR
  array_size = 10
RET
  array
RET_ARRAY
  array
BEGIN
  FOR (i = 0; i < array_size; ++i) BEGIN
    SET $array(~%i%~) = i       // handled by RET_ARRAY
    SET EVAL ~array_%i%~ = i    // !!! Does not work. Use $array(index) notation instead. !!!
  END
  SET array = array_size        // handled by RET
END
« Last Edit: February 10, 2018, 08:03:27 AM by Argent77 »

Offline Wisp

  • Moderator
  • Planewalker
  • *****
  • Posts: 1176
Re: Request: Return runtime-generated variables from WeiDU functions
« Reply #5 on: February 10, 2018, 08:46:21 AM »
Code: [Select]
    SET EVAL ~array_%i%~ = i    // !!! Does not work. Use $array(index) notation instead. !!!
The reason it does not work is that while $array(x y z) is syntactical sugar for array_x_y_z, it also registers the variable as being part of the array as a side-effect. If you define array_x_y_z with normal assignment, you can retrieve its value with $array(x y z), but the variable is not considered to be part of the array, which means it's also not considered when PHP_EACH does its thing, and possibly in other situations.

 

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Name: Email:
Verification:
Type the letters shown in the picture
Listen to the letters / Request another image
Type the letters shown in the picture:
What color is grass?:
What is the seventh word in this sentence?:
What is five minus two (use the full word)?: