Antanas A.
04/15/2019, 2:06 PM`
suspend fun <T, U> DataSource.mustHave(block: suspend T.() -> U): U =
((this as? T) ?: throw RuntimeException("Operation is not supported")).block()
`
and use it: dataSource.mustHave<DataSource.HasAddOperation> { add(command) }
Logically it should be able to infer U type automatically, but if I'm specifying first type T compiler requires me to specify also U type.
eg. dataSource.mustHave<DataSource.HasAddOperation, AddResult> { add(command) }
Is it possible to somehow declare that extension function in a way that U type be inferred automatically?ribesg
04/15/2019, 2:13 PM<>
Czar
04/15/2019, 2:29 PMPavlo Liapota
04/15/2019, 2:36 PMfun <T> DataSource.mustHave(): T =
(this as? T) ?: throw RuntimeException("Operation is not supported")
And then you can write
dataSource
.mustHave<DataSource.HasAddOperation>()
.add(command)
Pavlo Liapota
04/15/2019, 2:41 PMdataSource
.mustHave<DataSource.HasAddOperation>()
.run { add(command) }
Antanas A.
04/15/2019, 2:45 PMAntanas A.
04/15/2019, 2:47 PMAntanas A.
04/15/2019, 2:49 PM