https://kotlinlang.org logo
#announcements
Title
# announcements
e

emaleavil

06/23/2017, 9:15 AM
Hi, I was wondering if is there a way to create optional methods on interfaces like with switf?
d

diesieben07

06/23/2017, 9:19 AM
emaleavil: I think you would have to move the optional methods to a different interface. Then you could do
(myObject as? SomeInterface)?.method()
.
e

emaleavil

06/23/2017, 9:27 AM
Thanks for your answer...but I refer to methods whose implementation is optional
g

gildor

06/23/2017, 9:28 AM
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

emaleavil

06/23/2017, 9:29 AM
ok, that's what I need
d

diesieben07

06/23/2017, 9:29 AM
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

gildor

06/23/2017, 9:30 AM
But what is a practical difference?
I mean empty default method and method that optional to override
e

emaleavil

06/23/2017, 9:32 AM
yes, you're right but you don't need to override it on every class which implements your interface
d

diesieben07

06/23/2017, 9:32 AM
If there is a Unit return and the method doesn't have side-effects: Not much difference.
g

gildor

06/23/2017, 9:32 AM
default method has no side effects
at least inside interface
d

diesieben07

06/23/2017, 9:33 AM
println("hello world")
, there's your side effect.
g

gildor

06/23/2017, 9:33 AM
but you don’t need to override it on every class
same for default method
d

diesieben07

06/23/2017, 9:34 AM
I mean, yes in most cases you can achieve something similar, but it's different
g

gildor

06/23/2017, 9:34 AM
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

diesieben07

06/23/2017, 9:37 AM
For example, yes. Maybe I am wrong 😄
e

emaleavil

06/23/2017, 9:39 AM
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

diesieben07

06/23/2017, 9:40 AM
But this approach only really works with methods that return
Unit
.
e

emaleavil

06/23/2017, 9:45 AM
why? I think that if you provide an empty implementation is because you're not interested in consume this method
d

diesieben07

06/23/2017, 9:50 AM
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

emaleavil

06/23/2017, 9:55 AM
I can return an empty string
because I don't need it
g

gildor

06/23/2017, 9:56 AM
But how this thing handled in Swift?
I mean if you have an optional method and somebody called it
e

emaleavil

06/23/2017, 9:59 AM
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

diesieben07

06/23/2017, 10:34 AM
Thats what I read and what made me suggest the two-interface approach.
e

emaleavil

06/23/2017, 10:37 AM
what happen if are there more than one method in your approach?
d

diesieben07

06/23/2017, 11:21 AM
You make more than one interface. I realize it's verbose. It depends on what exactly your use-case is.
3 Views