Multiline Object/Array Declaration

Propose new features and changes
bobsmith90210

Multiline Object/Array Declaration

Post by bobsmith90210 » 29 Mar 2015, 18:00

Hello,

One enhancement I'd love to see would be the ability to declare objects and arrays across multiple lines, similar to other languages, to increase script readability.

Example:

Code: Select all

request := {"id":"12345678","jsonrpc":"2.0","method":"echo","params":{"line-1":"Hello World","line-2":"Blerg"}}
... could become ...

Code: Select all

request := {
    "id": "12345678",
    "jsonrpc": "2.0",
    "method": "echo",
    "params":{
        "line-1": "Hello World",
        "line-2": "Blerg"
        }
    }
... and similarly (assuming there is an instance called endpoint, of a generic class handling __call's) ...

Code: Select all

endpoint.echo({
    "line-1": "Hello World",
    "line-2": "Blerg"
    })
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Multiline Object/Array Declaration

Post by kon » 29 Mar 2015, 18:06

Code: Select all

request := {"id": "12345678"
    , "jsonrpc": "2.0"}
MsgBox, % request.jsonrpc
lexikos
Posts: 6175
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Multiline Object/Array Declaration

Post by lexikos » 29 Mar 2015, 19:14

Unfortunately it isn't feasible with the current parser.

You can use a continuation section, just the same as with any other line or command.

Code: Select all

endpoint.echo({
(Join  ; Join is optional in v2
    "line-1": "Hello World",
    "line-2": "Blerg"
)})

;or
endpoint.echo({
(Join,
    "line-1": "Hello World"
    "line-2": "Blerg"
)})
Line continuation (as kon demonstrated) is probably better for small objects.
guest3456
Posts: 2419
Joined: 09 Oct 2013, 10:31

Re: Multiline Object/Array Declaration

Post by guest3456 » 30 Mar 2015, 02:36

http://ahkscript.org/docs/Scripts.htm#continuation
Long lines can be divided up into a collection of smaller ones to improve readability and maintainability. This does not reduce the script's execution speed because such lines are merged in memory the moment the script launches.

Method #1: A line that starts with "and", "or", ||, &&, a comma, or a period is automatically merged with the line directly above it (in v1.0.46+, the same is true for all other expression operators except ++ and --). In the following example, the second line is appended to the first because it begins with a comma:

Code: Select all

FileAppend, This is the text to append.`n   ; A comment is allowed here.
    , %A_ProgramFiles%\SomeApplication\LogFile.txt  ; Comment.
so perhaps:

Code: Select all

request := { "id": "12345678"
           , "jsonrpc": "2.0"
           , "method": "echo"
           , "params":{ "line-1": "Hello World"
                      , "line-2": "Blerg"}}
not the most elegant but not bad

hoff
Posts: 3
Joined: 20 Jan 2018, 07:17
GitHub: alburthoffman

Re: Multiline Object/Array Declaration

Post by hoff » 28 Jan 2018, 23:40

Hi Guys, this feature turns out to be quite useful when a complex map object needs to be defined first.

Do we have any plan to support this?

Code: Select all

request := {
    "id": "12345678",
    "jsonrpc": "2.0",
    "method": "echo",
    "params":{
        "line-1": "Hello World",
        "line-2": "Blerg"
        }
    }
the ways u mentioned are not ideal, and very hard to maintain.
User avatar
nnnik
Posts: 3335
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Multiline Object/Array Declaration

Post by nnnik » 29 Jan 2018, 00:52

You know that you don't need the "" on the left side or?
Recommends AHK Studio
lexikos
Posts: 6175
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Multiline Object/Array Declaration

Post by lexikos » 29 Jan 2018, 02:01

hoff, it's already supported in the last couple of v2.0-alpha builds. I do not intend to add support for it in v1.
Post Reply

Return to “Wish List”