https://kotlinlang.org logo
#language-proposals
Title
# language-proposals
g

GarouDan

06/10/2020, 11:50 PM
Refactored: Following the suggestion, I’d like to post a use case here. The proposal is to create new operators ourselves, like
!
(in the end),
/.
,
@
,
->
and also create lists in this way
{1, "2", a}
, instead of
listOf(1, "2", a)
. I’d like to mimic as much as possible the Mathematica language in Kotlin. A valid Mathematica code is:
Copy code
N[
	D[
		Normal[
			Series[
				Integrate[
					Zeta[x],
					x
                ],
				{x, 0, 6}
            ]
        ],
		{x, 2}
    ] /.x -> 15,
	10
]
and in Kotlin, I’d like to run something like this:
Copy code
val x = "x"

n(
	d(
		normal(
			series(
				integrate(
					zeta(x),
					x
				),
				{x, 0, 6}
			)
		),
		{x, 2}
	) /. x -> 15,
	10
)!
Where the
n
,
d
,
normal
, etc., objects have a special
toString
method, and
/.
binary operator would do something with the previous and next argument and the
->
would be another binary operator. In some way, these binary operators are similar to
+
and
-
binary operators. At the moment the following works in Kotlin:
Copy code
val x = "x"

!n(
	d(
		normal(
			series(
				integrate(
					zeta(x),
					x
				),
				listOf(x, 0, 6)
			)
		),
		listOf(x, 2)
	) + "/. $x -> 15",
	10
)
Also, a lot of different operators would be hopefully needed since it is used in this grammar, like the
@
,
@@
, etc. So the proposal is have the possibility to create the
/.
,
->
,
@
,
@@
and other operators (to mimic the grammar) and also create lists in this way
{1, "2", a}
, instead of using
listOf(1, "2", a)
.
l

louiscad

06/11/2020, 6:29 AM
I don't think it'd help readability.
e

elizarov

06/11/2020, 7:21 AM
That would be completely different language with a little relation to Kotlin. A regular Kotlin developer would not be able to understand what’s going on in the code.
👍 11
r

raulraja

06/11/2020, 9:02 AM
@GarouDan Kotlin already support certain operator overloads such as
not
in prefix position. If you don’t mind the
!
in prefix this should work:
Copy code
class MyClass {
    operator fun not() {
        println("finished")
    }
}

fun main() = !MyClass()
!
as an invoke operator is used in some langs and papers and may suit a DSL.
not
is not constrained to Boolean expressions and same goes for
+
and others that you may override at will https://kotlinlang.org/docs/reference/operator-overloading.html each one of them with their own restrictions.
l

louiscad

06/11/2020, 9:30 AM
I really dislike how unconventional this reads and how it makes the code obscure to read even for maintainers themselves.
Even on Boolean expressions, I tend to use
not()
a lot more than
!
because it's more noticeable, and helps to avoid bugs of inverted condition.
r

raulraja

06/11/2020, 9:40 AM
yeah, my point is what he wants to do is doable in prefix mode and I don’t have enough info to judge if it’s a legit use case for this particular DSL Danilo is writing. I would have written this as as just
MyClass()
but there is not enough context in the message to understand the purpose of the DSL.
g

GarouDan

06/11/2020, 11:12 AM
I’m currently using the invoke operator and the ! (not) one, as a workaround. An exclamation in the end, for my case would be better, but I disagree this would be a different programming language, actually, this is a valid kotlin that we have there (I’m using it here). In my case, the ! will just be a shortcut for calling an external program in the machine, execute it, maybe through a coroutine and return. But, the main proposal, is to create your own operator because some libraries can really benefit from this, like mine (where I’d like to make it as close as possible as the original grammar that I’m porting, in this case, Mathematica language). So all of this functions
n
,
d
,
normal
, are very common there.
l

louiscad

06/11/2020, 11:21 AM
!
a shortcut to call an an external program? To me, that should just be a function call, nothing extra. The fact it is external is mostly an implementation detail. To me, it's not obvious at all what a suffix like
!
would and should do. The limitation "against" custom arbitrary operators and unobvious non explicit things is one of Kotlin's greatest strenght, one that makes us all able to read and understand one another code, and able to trust it once understood. Kotlin's basics are simple despite several unique powerful features like extensions/receivers and coroutines. Many things can be done with just functions and standard operators. Maybe you can be more specific as to what the difference would be in your specific case between with and without the
!
suffix? I think that'll help us suggest a more idiomatic solution.
g

GarouDan

06/11/2020, 11:25 AM
I already have
run
,
runTo
, etc methods that I can call. It is just I’d like to have as much closer to this as possible:
Copy code
N[
	D[
		Normal[
			Series[
				Integrate[
					Zeta[x],
					x
                ],
				{x, 0, 6}
            ]
        ],
		{x, 2}
    ] /.x -> 15,
	10
]
because this is the valid Mathematica code.
l

louiscad

06/11/2020, 11:30 AM
There's no
!
in there. BTW, if you really cannot stand the
listOf
, you can use a
vararg
, which is convenient until the number of arguments can vary at runtime or comes from a collection.
g

GarouDan

06/11/2020, 11:33 AM
I’m already using varargs, but I cannot get rid of the listOf, because the varargs “a, b, c” should become “a, b, c” (which is happening there), but the “a, listOf(b, c)“, should become “a, {b, c}” in my case
Please see the edited version of the mathematica code, as we can see, I’d need, if possible, other operators, like the
/.
one, this would be very nice, also the
->
l

louiscad

06/11/2020, 11:37 AM
I think you want a parser, not a DSL in Kotlin. Kotlin cannot adapt its syntax to all the other languages in the wild.
g

GarouDan

06/11/2020, 11:39 AM
It is not the case for a parser, since the goal is to write everything as kotlin functions, and in the end, execute the command. I’ll not have the command written in a string form to parse it (actually I use this, but this case I don’t need any parser, I can just execute the string).
I changed my description to be more clear now. I think now is more understandable why would be nice to have this for this case
l

louiscad

06/11/2020, 11:54 AM
Can you first try showing a Kotlin snippet with named parameters and replace operators with functions and parameters so we can understand what this is doing thanks to explicit English words?
r

raulraja

06/11/2020, 12:38 PM
@GarouDan I doubt Kotlin is gonna change those semantics but you can easily emulate this with an ADT that creates a data structure then you interpret to whatever you want. This is a common technique to create DSLs without Kotlin mutable DSLs approach:
👍 2
g

GarouDan

06/11/2020, 12:56 PM
Indeed this example is very interesting, thanks for sharing. I can get benefit from a lot of things that you’ve showed. And it compiles 😉 Unfortunately the
/
div operator should be different from
/.
one, but I got the idea. Unfortunately we cannot create operators with special symbols. I’ve created these issues here: https://youtrack.jetbrains.com/issue/KT-39539 https://youtrack.jetbrains.com/issue/KT-39538
👍 1
l

louiscad

06/11/2020, 1:08 PM
@raulraja Very great example! I had that in mind, but I didn't have a ready-made example. The upside with this approach compared to custom operators, is that you know these are functions, which means it's natural to search for their documentation, and see it's something special unlike basic arithmetic operators. @GarouDan What is the name of the
/.
operator and what is it supposed to do?
r

raulraja

06/11/2020, 1:09 PM
right, I think the point is you can express any DSL similar to its origin but Kotlin is not gonna accomodate all operators unless it had the notion of operator overload optin in a given file or similar
g

GarouDan

06/11/2020, 1:10 PM
r

raulraja

06/11/2020, 1:10 PM
@danilo in that case would /= fit?
l

louiscad

06/11/2020, 1:11 PM
Why not just use a function called
replaceAll
then? It's more explicit, no need to explain it, and now way you first think it means "special division" (that was my first thought).
g

GarouDan

06/11/2020, 1:12 PM
Hum, it could be, but we have a lot more, so, it will be strange. Because kotlin has only a very small set, and for
@
,
@@
we don’t have too much possibilities.
@louiscad, I already have that 😉, it is just that I’d like to mimic the language
l

louiscad

06/11/2020, 1:13 PM
@GarouDan Would coworkers or future maintainers or contributors like that language mimic in place of explicit operator names?
g

GarouDan

06/11/2020, 1:15 PM
@louiscad, yes, for mathematica’ers this would be very nice. but of course, it is already possible to survive with strings and functions instead, but with this you could almost copy and paste from there, that’s why
Here’s is a more complex example from there (grabbed randomly): https://wolfram.com/xid/0mrb9e-g0awiw
I already have something working with all 5k functions, I just would like to improve more 😉 . After some cleanups and tests I’ll release it as open source project.
l

louiscad

06/11/2020, 2:06 PM
If you want Java to Kotlin like copy and paste experience, you might be interested in this open source IDE plugin that translates html to kotlinx.html: https://github.com/data2viz/kotlinx.html-plugin
g

GarouDan

06/11/2020, 4:28 PM
Intesting 😃 , thanks for sharing. Beyond the library a plugin could be handy as well 😉