Skip to content

provide Kotlin "scope function" functionality #437

Description

@cpurdy

Is your feature request related to a problem? Please describe.
Kotlin developers using Ecstasy miss Kotlin's "scope function" functionality. This is not a blocker; it is a "quality of life" issue.

Describe the solution you'd like
I suggest a dramatically simpler approach than the Kotlin syntax/API/etc (i.e. not six named methods with two different context names etc.)

Kotlin with:

val numbers = mutableListOf("one", "two", "three")
with(numbers) {
    println("'with' is called with argument $this")
    println("It contains $size elements")
}

possible Ecstasy:

val numbers = ["one", "two", "three"];
numbers.{
    out($"'with' is called with argument {this}");
    out($"It contains {size} elements");
};

Kotlin run:

val result = service.run {
    port = 8080
    query(prepareRequest() + " to port $port")
}

possible Ecstasy:

val result = service.{
    port = 8080;
    return query($"{prepareRequest()} to port {port}");
};

Kotlin apply:

val adam = Person("Adam").apply {
    age = 32
    city = "London"        
}

possible Ecstasy:

val adam = new Person("Adam").{
    age = 32;
    city = "London";
};

The other methods that expose "it" to avoid colliding with "this" are useful, but we probably don't want to hard-code this and create another keyword etc. My suggestion is to allow the context variable to be optionally specified as part of the above syntax to cover the let and also functionality:

Kotlin let:

val numbers = mutableListOf("one", "two", "three", "four", "five")
numbers.map { it.length }.filter { it > 3 }.let { 
    println(it)
    // and more function calls if needed
} 

possible Ecstasy:

val numbers = ["one", "two", "three", "four", "five"];
numbers.map(size).filter(it -> it > 3).it -> {
    out(it);
    // and more function calls if needed
};

Kotlin also:

val numbers = mutableListOf("one", "two", "three")
numbers
    .also { println("The list elements before adding new one: $it") }
    .add("four")

possible Ecstasy:

val numbers = ["one", "two", "three"]
    .it -> { out($"The list elements before adding new one: {it}"); }
    .add("four");

The "it" use case needs to be looked at carefully, and understood in how common it is (no pun intended), and whether the syntax is confusing. Alternatives should obviously be considered if the syntax would be confusing.

The OCaml |> operator is also related to this conversation.

Describe alternatives you've considered
The primary alternative is "none", i.e. leave the syntax as-is. Hopefully, other alternatives will appear as part of the conversation around this issue.

Additional context
https://discord.com/channels/837372142576205885/1494337700252090509

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions