From 1193349b200e2cd3b8d0c7fad2b5644075d94374 Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Thu, 27 Aug 2020 18:21:30 +0700 Subject: [PATCH] Rework Memory.read method to have more convenient calls with result --- .../src/commonMain/kotlin/scientifik/memory/Memory.kt | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/kmath-memory/src/commonMain/kotlin/scientifik/memory/Memory.kt b/kmath-memory/src/commonMain/kotlin/scientifik/memory/Memory.kt index daae0c3d2..177c6b46b 100644 --- a/kmath-memory/src/commonMain/kotlin/scientifik/memory/Memory.kt +++ b/kmath-memory/src/commonMain/kotlin/scientifik/memory/Memory.kt @@ -83,9 +83,12 @@ interface MemoryReader { /** * Uses the memory for read then releases the reader. */ -inline fun Memory.read(block: MemoryReader.() -> Unit) { +inline fun Memory.read(block: MemoryReader.() -> R): R { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } - reader().apply(block).release() + val reader = reader() + val result = reader.block() + reader.release() + return result } /**