Compare commits

...

2 Commits

Author SHA1 Message Date
Ilya Gorbunov
513b0f536d Preview: replace apply with also where appropriate 2017-01-13 07:29:37 +03:00
Ilya Gorbunov
466c608172 New scope function: btw 2017-01-12 21:19:29 +03:00
8 changed files with 34 additions and 29 deletions

View File

@@ -82,8 +82,8 @@ class WhenMappingTransformer(
override fun visitMethod(
access: Int, name: String, desc: String, signature: String?, exceptions: Array<out String>?
): MethodVisitor? {
return MethodNode(access, name, desc, signature, exceptions).apply {
methodNodes.add(this)
return MethodNode(access, name, desc, signature, exceptions).also {
methodNodes.add(it)
}
}
}, ClassReader.SKIP_FRAMES)

View File

@@ -220,7 +220,7 @@ class RedundantCoercionToUnitTransformer : MethodTransformer() {
REPLACE_WITH_NOP
private fun createRemovableNopInsn() =
InsnNode(Opcodes.NOP).apply { removableNops.add(this) }
InsnNode(Opcodes.NOP).also { removableNops.add(it) }
private fun getInputTop(insn: AbstractInsnNode): SourceValue {
val i = insnList.indexOf(insn)

View File

@@ -176,7 +176,7 @@ class GenerationState @JvmOverloads constructor(
::CoroutineTransformerClassBuilderFactory,
{ BuilderFactoryForDuplicateSignatureDiagnostics(
it, this.bindingContext, diagnostics, fileClassesProvider, this.moduleName
).apply { duplicateSignatureFactory = this } },
).also { duplicateSignatureFactory = it } },
{ BuilderFactoryForDuplicateClassNameDiagnostics(it, diagnostics) },
{ configuration.get(JVMConfigurationKeys.DECLARATIONS_JSON_PATH)
?.let { destination -> SignatureDumpingBuilderFactory(it, File(destination)) } ?: it }

View File

@@ -122,9 +122,9 @@ open class MetadataSerializer(private val dependOnOldBuiltIns: Boolean) {
}
val kotlinModuleFile = File(destDir, JvmCodegenUtil.getMappingFileName(JvmCodegenUtil.getModuleName(module)))
val packageTableBytes = JvmPackageTable.PackageTable.newBuilder().apply {
val packageTableBytes = JvmPackageTable.PackageTable.newBuilder().also {
for (table in packageTable.values) {
table.addTo(this)
table.addTo(it)
}
}.serializeToByteArray()

View File

@@ -50,9 +50,9 @@ private fun Any.copyValueIfNeeded(): Any {
is DoubleArray -> Arrays.copyOf(this, size)
is BooleanArray -> Arrays.copyOf(this, size)
is Array<*> -> java.lang.reflect.Array.newInstance(javaClass.componentType, size).apply {
this as Array<Any?>
(this@copyValueIfNeeded as Array<Any?>).forEachIndexed { i, value -> this[i] = value?.copyValueIfNeeded() }
is Array<*> -> java.lang.reflect.Array.newInstance(javaClass.componentType, size).also {
it as Array<Any?>
(this as Array<Any?>).forEachIndexed { i, value -> it[i] = value?.copyValueIfNeeded() }
}
is MutableCollection<*> -> (this as Collection<Any?>).mapTo(javaClass.newInstance() as MutableCollection<Any?>) { it?.copyValueIfNeeded() }

View File

@@ -59,8 +59,8 @@ class BlockInfo private constructor(val parent: BlockInfo?) {
private val infos = Stack<ExpressionInfo>()
fun create() = BlockInfo(this).apply {
this@apply.infos.addAll(this@BlockInfo.infos)
fun create() = BlockInfo(this).also {
it.infos.addAll(infos)
}
fun addInfo(loop: ExpressionInfo) {
@@ -210,8 +210,8 @@ class ExpressionCodegen(
gen(receiver, callable.dispatchReceiverType!!, data)
}
expression.extensionReceiver?.apply {
gen(this, callable.extensionReceiverType!!, data)
expression.extensionReceiver?.also {
gen(it, callable.extensionReceiverType!!, data)
}
val args = expression.descriptor.valueParameters.mapIndexed { i, valueParameterDescriptor ->
@@ -266,8 +266,8 @@ class ExpressionCodegen(
val varType = typeMapper.mapType(declaration.descriptor)
val index = frame.enter(declaration.descriptor, varType)
declaration.initializer?.apply {
StackValue.local(index, varType).store(gen(this, varType, data), mv)
declaration.initializer?.also {
StackValue.local(index, varType).store(gen(it, varType, data), mv)
}
val info = VariableInfo(
@@ -323,8 +323,8 @@ class ExpressionCodegen(
}
private fun findLocalIndex(descriptor: CallableDescriptor): Int {
val index = frame.getIndex(descriptor).apply {
if (this < 0) throw AssertionError("Non-mapped local variable descriptor: $descriptor")
val index = frame.getIndex(descriptor).also {
if (it < 0) throw AssertionError("Non-mapped local variable descriptor: $descriptor")
}
return index
}
@@ -464,12 +464,12 @@ class ExpressionCodegen(
fun markNewLabel(): Label {
return Label().apply { mv.visitLabel(this) }
return Label().also { mv.visitLabel(it) }
}
override fun visitReturn(expression: IrReturn, data: BlockInfo): StackValue {
val value = expression.value.apply {
gen(this, returnType, data)
val value = expression.value.also {
gen(it, returnType, data)
}
val afterReturnLabel = Label()
@@ -502,8 +502,8 @@ class ExpressionCodegen(
val end = Label()
thenBranch.apply {
gen(this, type, data)
thenBranch.also {
gen(it, type, data)
//coerceNotToUnit(this.asmType, type)
}
@@ -583,8 +583,8 @@ class ExpressionCodegen(
with(LoopInfo(loop, continueLabel, endLabel)) {
data.addInfo(this)
loop.body?.apply {
gen(this, data)
loop.body?.also {
gen(it, data)
}
data.removeInfo(this)
}
@@ -641,8 +641,8 @@ class ExpressionCodegen(
with(LoopInfo(loop, continueLabel, endLabel)) {
data.addInfo(this)
loop.body?.apply {
gen(this, data)
loop.body?.also {
gen(it, data)
}
data.removeInfo(this)
}

View File

@@ -87,8 +87,7 @@ class FunctionCodegen(val irFunction: IrFunction, val classCodegen: ClassCodegen
val directMember = JvmCodegenUtil.getDirectMember(descriptor)
if (DescriptorUtils.isAnnotationClass(directMember.containingDeclaration)) {
val source = directMember.source
(source.getPsi() as? KtParameter)?.defaultValue?.apply {
val defaultValue = this
(source.getPsi() as? KtParameter)?.defaultValue?.also { defaultValue ->
val constant = org.jetbrains.kotlin.codegen.ExpressionCodegen.getCompileTimeConstant(
defaultValue, state.bindingContext, true, state.shouldInlineConstVals)
assert(!state.classBuilderMode.generateBodies || constant != null) { "Default value for annotation parameter should be compile time value: " + defaultValue.getText() }

View File

@@ -48,13 +48,19 @@ public inline fun <T, R> with(receiver: T, block: T.() -> R): R = receiver.block
@kotlin.internal.InlineOnly
public inline fun <T> T.apply(block: T.() -> Unit): T { block(); return this }
/**
* Calls the specified function [block] with `this` value as its argument and returns `this` value.
*/
@kotlin.internal.InlineOnly
@SinceKotlin("1.1")
public inline fun <T> T.also(block: (T) -> Unit): T { block(this); return this }
/**
* Calls the specified function [block] with `this` value as its argument and returns its result.
*/
@kotlin.internal.InlineOnly
public inline fun <T, R> T.let(block: (T) -> R): R = block(this)
/**
* Executes the given function [action] specified number of [times].
*