Skip to content

Cascade operator #12017

@mindplay-dk

Description

@mindplay-dk

I couldn't find this request in the issue backlog, so I'd like to request support for method and property cascades, e.g. via the .. operator, familiar from other languages like Dart.

While this is merely syntactic sugar, it's very convenient - take the following example:

function request(method: string, url: string, body?: string) : Promise<XMLHttpRequest> {
    return new Promise<XMLHttpRequest>(function (resolve, reject) {
        var xhr = new XMLHttpRequest()

        xhr.open(method, url)

        xhr.onload = function () {
            if (this.status >= 200 && this.status < 300) {
                resolve(xhr)
            } else {
                reject(xhr)
            }
        }

        xhr.onerror = function () {
            reject(new Error(`unable to complete ${method} request for: ${url}`))
        }
        
        xhr.send(body)
    })
}

We can discard the intermediary variable and references to it, so the above becomes:

function request(method: string, url: string, body?: string) : Promise<XMLHttpRequest> {
    return new Promise<XMLHttpRequest>(function (resolve, reject) {
        new XMLHttpRequest()
            ..open(method, url)
            ..onload = function () {
                if (this.status >= 200 && this.status < 300) {
                    resolve(xhr)
                } else {
                    reject(xhr)
                }
            }
            ..onerror = function () {
                reject(new Error(`unable to complete ${method} request for: ${url}`))
            }
            ..send(body)
    })
}

Dart having a very similar syntax, I believe this feature can basically be referenced 1:1 from Dart?

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions