I would like to offer two methods with the same name which can accept optional or non-optional input:
class Formatter {
fun format(input: Number?) : String? {return number?.toString()}
fun format(input: Number) : String {return number.toString()}
}
Apparently this is not possible due to JVM limitations:
Platform declaration clash: The following declarations have the same JVM signature (test(Lorg.example.Number;)Ljava/lang/String;):
Is there a readable workaround to achieve the same goal? My current solution is to rename one method (e.g. formatNonNull(input: Number)).
Bonus: My Formatter-class is actually written in Java and looks like this:
class Formatter {
@Nullable String format(@Nullable Number input) : String {return number != null ? number.toString(): null;}
}
It shall be extended by the non-null variant:
@NonNull String formatNonNull(@NonNull Number input) : String {return number.toString();}
Is there a way to improve this i.e. without introducing a new name (e.g. with kotlin extension)?
contracts
can help you - user2340612