y
04/15/2024, 6:18 AMinterface Foo {
/* ... */
}
interface Bar {
val thing: Thing
/* ... */
}
fun getFooByName(s: String): Foo? = /* ... */
// no getBarByName
// ... and a bunch of classes that implement some combination of Foo and Bar
would you add a fun asBar(): Bar?
or even fun getThing(): Thing?
for Foo
, or is that silly from a coupling/API design point of view?Michael de Kaste
04/15/2024, 6:24 AMval thing: Thing?
on a sealed interface where only 1 or 2 implementations have a thing and the others do not. (where translating it to an API object was easier to do with nullable fields)
I don't see the need for asBar() func as
as Bar?
functionally does the same.
In most cases I would advice against such pattern, but it depends on the usage and situation, like everythingy
04/15/2024, 6:28 AMFoo
and Bar
used to be a single interface that (for no good reason, imho) was later split into Bar
which contains val Thing
and nothing elseJoffrey
04/15/2024, 8:59 AMBar
instead of Foo
in areas of the code where you know the objects have a thing
. It's usually better to do this than to have a nullable property and never know at compile time whether it's there or not.y
04/15/2024, 12:00 PM