question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support of default value for parameter in Kotlin

See original GitHub issue

Current Behavior

When we use the annotations to set argument, the default value from kotlin declaration is not supported.

With the current system, the command bellow cannot be executed when the player doesn’t define the values for args :

@CommandMethod("test [x] [y] [z]")
@CommandDescription("Test command")
suspend fun testCmd(
        player: Player,
        @Argument("x") x: Double = player.location.x,
        @Argument("y") y: Double = player.location.y,
        @Argument("z") z: Double = player.location.z
) {
 println("parameters : $x $y $z")
}

If the user doesn’t add an argument, a null value will be used to set the arguments.

Effectively, in the @Argument, there is a String to define the default value, but in the most cases, it’s not enough.

Expected Behavior

The command defined above can be used if the user writes : /test /test 1 /test 1 2 /test 1 2 3

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
solonovamaxcommented, May 26, 2022

To add on to this:

As per the docs, you can check if it’s optional by checking KParameter#isOptional. If it is optional, then it can be omitted when invoking KFunction#callBy.

It seems there is no way to get what the default value is via the kotlin reflection api, however you can still use the default without knowledge of what it is.

0reactions
solonovamaxcommented, May 27, 2022

If someone wants to go ahead and implement this, then it could probably become a part of cloud. It would have to go in some new module cloud-kotlin/cloud-kotlin-default-annotations or something (similar to the couroutine support module)

That’s why I proposed something like cloud-kotlin/cloud-kotlin-core (in hindsight, I wasn’t that clear about it)

And, @Distractic, look at SuspendingExecutionHandler and its references.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Functions | Kotlin
A default value is set by appending = to the type. Overriding methods always use the base method's default parameter values.
Read more >
Quick Guide to Kotlin Default and Named Arguments - Baeldung
In this article, we looked at Kotlin's support for named arguments and default argument values. To begin with, we looked at how positional ......
Read more >
Kotlin Function - Default Arguments - Tutorial Kart
Default Arguments : Kotlin supports default argument values assigned to parameters in function definition. The function is allowed to call with no argument ......
Read more >
Kotlin Functions, Default and Named Arguments, Varargs and ...
Kotlin supports default arguments in function declarations. You can specify a default value for a function parameter. The default value is ...
Read more >
How to assign a default value to a function as parameter in ...
Unlike some other languages, in Kotlin the braces {} are the (only) required bit of syntax for writing a lambda. (You can omit...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found