Support of default value for parameter in Kotlin
See original GitHub issueCurrent 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:
- Created a year ago
- Comments:7 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

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 invokingKFunction#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.
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
SuspendingExecutionHandlerand its references.