Main Board > Wish List

Short-hand syntax for anonymous arrays or objects initialization

(1/2) > >>

Stéphane Devouard:
In PHP you can create and initialize an array with :

--- Code: ---$myArray = [ "item", "another item", "a third item"];
--- End code ---

You can also pass either an indexed or associative anonymous array to a function using :

--- Code: ---myFunction([
  "key1" => "value 1",
  "key2" => "value 2"
--- End code ---

Almost same in C# :

--- Code: ---string[] myArray = { "value", "another value" }; // declaring and initializing an array
myFunction(new[] {10, 20, 30}); // passing an anonymous array to a function
--- End code ---

In Javascript you can create a simple object (and manipulate it with a syntax which is basically that of an associative array)

--- Code: ---let options = {
  url: "server:port/path/to/resource",
  timeout: 5000,
  mailFrom: "",
  mailTo: "",
  message: "Hello there"
}); // passing an anonymous object

--- End code ---

Almost the same in C#

--- Code: ---myFunction(new { TextProperty = "Some Value", NumProperty = 100 });
--- End code ---

In summary :

You do not always need to declare a variable -- you can already pass literal string or number values to a subroutine or method in PxPlus.

It would be great if it was possible to do it with other data structures such as arrays, associative arrays and objects -- the latter could be wiped out when not needed anymore using the "FOR OBJECT/PROGRAM/..." housekeeping clauses

Mike King:
Here are a couple of simple tricks you can use in PxPlus to get similar functionality. 

To quickly create and load an array try:

dim Y$[*]
read data from "Mike,Was,Here",sep="," to Y${ALL}

Use JSON to define/load an associative array:

dim load y$ = "{'name':'Mike', 'eyes':'blue'}"

Use an object to pass data.  First create the following object and save as "data.pvc"

def class "data" accept properties
end def

Now define and use to pass information as in:

oData = new("data" for program)
oData'Param2$="Hello world"

Now you can pass all that data simply by passing the object handle -- and you can use the 'data' object wherever you want and have any number of them each with a different set of properties.

As for creating 'unnamed' arrays, we currently don't have a method to do this but can take it under consideration.

Stéphane Devouard:

Thanks for the answer. I know about these small tricks.

I even used this construct in my days at Explorer :

--- Code: ---with new("data")
.'numProp = 123
.'strProp = "Hi there"
drop object .'_OBJ
end with

--- End code ---
We didn't have the FOR ... clauses for object housekeeping since we were stuck with V10

My point is elsewhere :
1. being able to pass an unnamed/anonymous array [ "Mike", "Was", "Here" ] to a function/call/method without having to declare a Y$[] array and parse a string to fill it
2. being able to pass an unnamed/anonymous associative array [ "name" => "Mike", "eyes" => "blue" ] to a function/call/method without having to declare a Y$ JSON string and parse it
3. being able to pass an unnamed/anonymous plain old pxplus object (popo ?) to a function/call/method without having to declare a class skeleton & loading it

Basically 2 & 3 are very similar (in Javascript you can get/set an object property values either with objectRef.propertyName or objectRef["propertyName"] and so if either one was possible ("litteral" associative array or object definition), that would be cool.

As a suggestion, you could provide a standard / popo class yourself (for exemple, *obj/data.pvc or *plus/obj/data.pvc or whatever name you see fit)
Then allow a syntax such as :

--- Code: ---
someObject’someMethod(new("*obj/data" for program with Param1=123, Param2$="Hello World"))

// in someObject.pvc
function someMethod(options)
! // the anonymous object instantiated above is received with the Param1 and Param2$ property values set
enter options
! // now do something with options'Param1 and options'Param2$
end function

--- End code ---

Mike King:
Actually for PxPlus 2021 we have added (actually the work is done and the logic is in QA) the ability to include a WITH clause to CALL, PERFORM and any method call to pass in values by variable name.

For example:

myObject'Function(with Name$="Dora", Type$="Cat", Age=10, Color$="Calico" )

When the method is invoked the variables Name$, Type$, Age, and Color$ will have been pre-initialized with the values specified.

Similar logic for:

CALL "SomeProg" with Name$="Dora", Type$="Cat", Age=10, Color$="Calico"
PERFORM "SomeProg" with Name$="Dora", Type$="Cat", Age=10, Color$="Calico"

This pretty much gives you what you were looking for apart from the use of an associative array.

As mentioned, this has already been developed and is one of the many new features that we will be releasing next May with our PxPlus 2021 release.
(BTW: Syntax wise the WITH clause will be accepted in Update 1 of PxPlus 2020 however it will generate a run-time error as the logic will not be enabled until PxPlus 2021)

Stéphane Devouard:

Interesting... This brings some questions

In the case of an object method, what if the variables you passed in are also properties ? Or maybe they HAVE to be declared as properties and you can set their values in the same line that you're invoking the method ? And are they passed by value or by reference ?

For CALL, same questions as the object method -- what if the variable name is also in the ENTER ?

For PERFORM that is pretty much like the GOSUB ... WITH that is alreay available

I guess they'll have to wait until next May... ;)


[0] Message Index

[#] Next page

Go to full version