Kotlin internal and external parameter name propose


#1

I like this in Swift, is very verbouse, please add in Kotlin to.

class User() {
    
    fun send(notification: Notification, using token: String) {
       // 1. Name of function is short and clear
       // 2. Inside of function body you use token, not a long parameter for exemple: userAccountDto.
    }
}

// When you call this function is self explanatory.
val notification = Notfication()
val token = "your_token"
User().send(notification, using: token)

#2

Do you also want to explain what this is? I guess most people have not used Switft before and have no idea what you are talking about


#3

Looks like just named parameters, but when you have one name for parameter inside function and another from outside. Not sure what this is good idea.


#4

This function is an exemple of how to use. By having this feature functions can become lets say more verbouse. What I miss from Swift in Kotlin (both languages are very similar) is that you can explain more what the function is doing and with what king of input. Lets say you have:

class User() {
    
    fun send(notification: Notification, using token: String) {
       // 1. Name of function is short and clear
       // 2. Inside of function body you use token, not a long parameter for exemple: userAccountDto.
    }
}

// When you call this function is self explanatory.
val notification = Notfication()
val token = "your_token"
User().send(notification, using: token)

Also give opportunity to chose if we want to show or hide parameters and Not specify enum classname. Exemple :

enum class UserNameType {
    nickname, emoji
}

class User() {
    
    fun convert(_ name: String, into type: UserNameType) {
       // 1. _ notification = optional parameter name.
    }
}

User().convert("Name_string" into: .nickname)

I`m not shure that give the right example, but its like programing language became more natural and uses less code to write.
For more details please check: swift functions


#5

Interesting. So if I understand this correctly in swift each argument has both a name and a label. The name is what you use to refer to the argument from within the function and the label is what is getting used when you refer to the argument when calling the code.

func someFunction(label name: Int)  -> Int{
    return name    // here I have to use the name
}
someFunction(label: 5)  // when using named parameters I have to use the label
someFunction(5)           // I think I can just call this the "normal" way as well

I hope I did not misrepresent the feature.


I can see where this could come in handy, especially when designing DSLs. The problem I see however is how this is supposed to work with reflection as each parameter now has 2 names. I guess you would return the label and not the name.
Also I dislike the idea that 2 parameters can have the same label. According to the doc in swift I can have 2 parameters with different names but the same label. IMO this should be changed to both labels and names should be unique per function. That way there is no problem with the java interop.


#6

No, parameter name is only one, label is separate. Can be realized like optional.

// Label by default will be hiden.
func someFunction(_ name: Int)  -> Int { return name }

// So in call you will have
func someFunction(123)
// In this case reflection is only made by parameter name 

// Also you can specify the label
func someFunction(label name: Int)  -> Int { return name }

func someFunction(label:123)

What about reflection, you only make it using parameter name. I think in DSLs you can specify that the label have the same value as parameter, so reflection can work in this case.