Riccardo Cardin
08/02/2024, 4:48 PMGemy
08/02/2024, 6:03 PMGemy
08/02/2024, 6:04 PMRiccardo Cardin
08/02/2024, 6:23 PMinterface CreatePortfolioUseCase {
suspend fun Raise<DomainError>.createPortfolio(model: CreatePortfolio): PortfolioId
}
fun createPortfolioUseCase(): CreatePortfolioUseCase =
object : CreatePortfolioUseCase {
override suspend fun Raise<DomainError>.createPortfolio(model: CreatePortfolio): PortfolioId =
PortfolioId("1")
}
private val underTest = createPortfolioUseCase()
@Test
internal fun `given a userId and an initial amount, when executed, then it create the portfolio`() =
runTest {
val actualResult: Either<DomainError, PortfolioId> =
with(underTest) {
either {
createPortfolio(CreatePortfolio(UserId("bob"), Money(1000.0)))
}
}
assertThat(actualResult.getOrNull()).isEqualTo(PortfolioId("1"))
}
The problem is that the class containing the method should be used as a context through the with
function.
My doubt is if there is a better way 🤷♂️Chris Lee
08/02/2024, 8:57 PMwith
. Just finished converting an app from context receivers to this (as context receivers are a dead-end), its not as elegant as context receivers (or context parameters, when they arrive) but not too bad.Jakub Zalas
08/08/2024, 11:32 AMas context receivers are a dead-end@Chris Lee why?
Chris Lee
08/08/2024, 12:51 PMJakub Zalas
08/08/2024, 1:24 PMRaise<>
contexts. Having multiple Raise<>s wouldn't require a single error hierarchy so would work kinda like unions.
I guess I'm not doing that anymore 😅phldavies
08/09/2024, 9:23 AMcontext-receivers
may be on their way out, but context-parameters are on their way in. There have been comments that there will be no gap between support for one and support for the other, but also that there is very likely to be no compiler version that supports both.