Hi, I was wondering if is there a way to create op...
# announcements
e
Hi, I was wondering if is there a way to create optional methods on interfaces like with switf?
d
emaleavil: I think you would have to move the optional methods to a different interface. Then you could do
(myObject as? SomeInterface)?.method()
.
e
Thanks for your answer...but I refer to methods whose implementation is optional
g
You can add empty default method to interface
Copy code
interface Foo {
 fun bar()

 fun optionalBaz() {
    //You can override me but it's not required
  }
}
e
ok, that's what I need
d
I only looked at the documentation for a brief moments, but as I understood it optional methods are optionally implemented. With Andrey's example, the method is always implemented, as there is a default
g
But what is a practical difference?
I mean empty default method and method that optional to override
e
yes, you're right but you don't need to override it on every class which implements your interface
d
If there is a Unit return and the method doesn't have side-effects: Not much difference.
g
default method has no side effects
at least inside interface
d
println("hello world")
, there's your side effect.
g
but you don’t need to override it on every class
same for default method
d
I mean, yes in most cases you can achieve something similar, but it's different
g
I still don’t understand difference bitween optionally implementation and empty default method
you mean if later someone add some code to empty default method?
I understand general difference, I don’t understand difference in this particular case
d
For example, yes. Maybe I am wrong 😄
e
Anyway, I have to add an empty implementation on every class
I don't know but I think that it's similar to optional on swift
d
But this approach only really works with methods that return
Unit
.
e
why? I think that if you provide an empty implementation is because you're not interested in consume this method
d
And what do you do if the method returns a String? What do you return in the default method? You can't use null, do you use a special string? That would be a nightmare.
e
I can return an empty string
because I don't need it
g
But how this thing handled in Swift?
I mean if you have an optional method and somebody called it
e
I don't know
this is an answer that someone gave me on ios group
"Protocols (at least in ObjC) don’t provide method implementations. They only specify the signatures of methods to be implemented by conforming types. Optional methods don’t provide any empty default impl. Instead you call them like this:
delegate.someOptionalMethod?(argument: 42)
. Note the
?
which makes sure (at runtime) that
someOptionalMethod
only gets called if the delegate actually does implement it."
d
Thats what I read and what made me suggest the two-interface approach.
e
what happen if are there more than one method in your approach?
d
You make more than one interface. I realize it's verbose. It depends on what exactly your use-case is.