Marcin Wisniowski
01/06/2023, 6:03 PM<T : Any, PossiblyNullableT : T?>
, where T can be any type, and PossiblyNullableT
has to be the same type or the nullable version of it. Basically the two possible ways to call my function would be:
func<String, String>()
func<String, String?>()
But I don't want to allow subclasses of T
as the second type parameter, it either has to be the same type, or the nullable version.Joffrey
01/06/2023, 6:05 PMephemient
01/06/2023, 6:06 PM<T>
it can be either nullable or non-nullable, and you can use the type T & Any
when the non-nullable version is strictly requiredMarcin Wisniowski
01/06/2023, 6:13 PMfun getValue<Type, Default>(default: Default): Default = { return [..] ?: default }
I need to constrain the Default
type so that it's the same as Type
, or the nullable version.ephemient
01/06/2023, 6:15 PMJoffrey
01/06/2023, 6:16 PMThe value it returns is not nullableThat doesn't seem to be the case, otherwise what's the point in using
?:
?Marcin Wisniowski
01/06/2023, 6:17 PMephemient
01/06/2023, 6:17 PMMarcin Wisniowski
01/06/2023, 6:43 PMifEmpty
and the fact constraining the type of the default is not actually necessary for me helped. All I need is fun <T : Default, Default> getValue(default: Default): Default = readData<T>() ?: default
.Marcin Wisniowski
01/06/2023, 7:04 PMDefault
has to be a supertype of another type. Minimal example:ephemient
01/06/2023, 7:12 PM@Suppress("BOUNDS_NOT_ALLOWED_IF_BOUNDED_BY_TYPE_PARAMETER")
fun <T, Default> getStringValue(default: Default): Default where T: String, T: Default = getValue(default)
but note that it breaks Java interop. but why would you need that?Marcin Wisniowski
01/06/2023, 7:27 PMgetStringValue
function that returns a String. I just want to add a default argument, so the caller can specify a default in case the function can't return the string it should. The default could be null.
Simplest solution would be to just add a default: String?
argument, but that changes my return type to String?
. I don't want the return type to become nullable when it's called with getStringValue("default")
, since it will never actually return null.
At that point in attempt to solve that, I made the default type generic, so that if the caller specifies a non-nullable default, the return type will be kept non-nullable. But this resulted in the problem that it's now possible to specify a default type different than String
or String?
, which makes it not compile: now I can't return [String?] ?: [Default]
, because String could possibly not be a subclass of Default, arriving at my original question: How can I constraint the Default
type to be either String
or String?
. (Or in the general case, to be either T
or T?
)Marcin Wisniowski
01/06/2023, 7:32 PMString
when you provide a String
argument, or returns String?
when you provide String?
argument. But the function normally returns a String
, not just whatever is passed as the argument, so I can't make it accept arbitrary types and make them the return type, because then I can't return the normal String
.Stephan Schröder
01/07/2023, 4:39 PMString?
you could simply write and use two different functions:
fun getStringValueOr(default: String): String
and fun getStringValueOrNull(): String?
.
(of course default
could have a default value default: String = ""
)