An API designer should be able to make function pa...
# language-proposals
b
An API designer should be able to make function parameter names mandatory.
Copy code
// Syntax idea 1: use a reserved symbol like `!` for each mandatorily-named parameter
fun color(!fromHexString: String): Color { ... }
fun color(!fromHtmlName: String): Color { ... }

// Syntax idea 2: Use a compiler annotation for each mandatorily-named parameter
fun color(@MandatoryParam fromHexString: String): Color { ... }
fun color(@MandatoryParam fromHtmlName: String): Color { ... }

// Syntax idea 3: Use a compiler annotation to indicate all parameter names are mandatory
@MandatoryParams
fun color(fromHexString: String): Color { ... }
@MandatoryParams
fun color(fromHtmlName: String): Color { ... }
so that when calling them with Kotlin, it would have to look like:
Copy code
val hexColor = color(fromHexString = "#123456")  //  Good!
val htmlColor = color(fromHtmlString = "aliceblue")  //  Good!

val badHexColor = color("#123456")  //  Compile error like: There are two functions with the same signature; include parameter names to specify the function
The above functions would be exposed to Java (and other JVM langauges) like this:
Copy code
public /* static? */ Color colorFromHexString(String hexString) { ... }
public /* static? */ Color colorFromHtmlName(String htmlName) { ... }
Other use cases:
Copy code
fun show(!bufferOffscreenFirst: Boolean) { ... } // so that it never seems show(false) actually hides
fun SomeFloatRange.contains(value: Float, !tolerance: Float): Boolean  //  Label is optional on "value", but required on "tolerance" so it's impossible to think you're passing, for instance, another range of values.
👍 1
👎 5