is there a way to force the user to pass a nullab...
# announcements
r
is there a way to force the user to pass a nullable type? Say I have
fun foo(s: String?){}
is there a way to force that one has to pass
String?
and cannot pass
String
?
a
I don't think so, but even if they do pass
String
you're treating it as a nullable inside your function. I'm not sure you need to force the caller to do anything.
r
I don't need but I would like to
a
What's the use case?
r
a user should be aware of that he deals with a nullable case. If he would have to pass a nullable type then he would probably realise that he has to take some precautions
I would like to have two overload but if it is not possible then I will probably just provide two different functions (different names)
a
But if they give you a non nullable string, why do they care?
Imagine this being called from Java, too. You definitely don't know what could happen, which is why it's good that your function takes in a nullable. If the user passes in null, then it should still work okay. If they don't, it should still work okay. Unless I'm missing more context
r
its something like
null == "hello"
so if you know that left hand side might be null then you might not do
==
but take another action
a
Hmm I'm still not sure I follow, or rather I think you are over complicating it. If my string is null, it's not equal to hello, so I'd want this to be false which your method would do. THEN, based on the response to your method, I would do something else.
r
say you treat
null
as your error code then
null == "hello"
has semantically a different meaning than
"ciao" == "hello"
you might have tree cases: same, not same, error
but don't mind, I'll look for something else. Thanks anyway 🙂
a
hmm
I see your point, so I'm sorry if this came off as kinda rude. I think I'm just conflicted because you say "you might have three cases" but it's not your responsibility to know what the caller would do. Right? Does your method /need/ to account for all three cases? If the answer to that is yes, then why not just do the null check inside the method yourself.
r
no worries, it did not come off as rude, I just figured I don't want to waste your time 🙂
a
Not at all! It was a really good discussion hidden behind a simple question. 🙂
r
sometimes you are not aware that you deal with a nullable because it is well hidden behind type inference
a
right, or if you're interroping with Java even
r
in case you think it is non nullable you would just do
==
if you knew it is nullable you would do something different
so in my case I will provide a function which does not allow nullable, say
foo
a
Yeah, you can't force nullability per se, but you can force non nullable, so maybe that's what your method should do. If they have a nullable type, they won't be able to call it, and thus they have to handle null themselves.
r
if the user starts typing
foo
and it is nullable, then he will get an error (because
foo
is not applicable to nullable`) and will instead see
fooNullable
as available option
a
and you, the method writer, no longer concern yourself with it.
yep yep yep
but maybe fooNullable isn't even necessary at the end of the day
(Assuming foo really is just an equal check, ya know?)
Idk the whole story
I have to run AFK, but if this comes up again feel free to DM me any time.
r
sure, have a nice day