in programming generally, is it common to write a ...
# getting-started
o
in programming generally, is it common to write a function that can accept more than 1 type of object?
v
that’s kind of odd question 😛. But to answer it - yes it’s fairly common
a
In what way? for example a
String
or an
Int
?
o
hmm yea any type
fun (T)
k
println
takes a bunch of stuff,
StringBuffer.append
takes `CharSequence`s and characters.
o
sorry sorry, objects of different types
k
Ah. Is
listOf
more like what you mean?
o
while accessing them inside as well, remember when you helped me with this before @karelpeeters? and how you passed lambdas for the cases where you wanted access to those differnt objects?
they have different properties, there’s no way i think to do this without accepting a lambda
where at call-time you pass what you want to end up doing with that object
k
I vaguely remember that simple smile . It's called "ad-hoc polymorphism", see http://beust.com/weblog/2016/06/20/ad-hoc-polymorphism-in-kotlin/
Preferred way would of course be actual polymorphism.
o
you linked to that as well back then 😛
😁 1
but I dont see in smaller cases where that’s needed, I mean you can just pass a function that does the property access for you
propertyNeededInsideFunction = getPropertyForThisObject.invoke()
and the lambda is
{objectTypeA.property}
k
I don't really get your question now - there's 3 ways to do this: overloading, ad-hoc and actual polymorphism. Each of them has their own use cases, and all of them are used.
o
yea I’ll read up on these, cause I forgot the gist of it back then when I first understood it
just took it on implementation-basis, didn’t read reasoning