Skip to content

Commit

Permalink
Fix an overload collision of onEnum(of:) caused by its optional overl…
Browse files Browse the repository at this point in the history
…oad.
  • Loading branch information
FilipDolnik committed Mar 6, 2024
1 parent a2e449b commit b25e832
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion SKIE/acceptance-tests
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class SealedFunctionGeneratorDelegate(
fun generate(kirClass: KirClass, enum: SirClass) {
val requiredFunction = generateRequiredOverload(kirClass, enum)
generateOptionalOverload(kirClass, enum, requiredFunction)
generateRequiredOverloadWithOptionalReturnType(kirClass, enum, requiredFunction)
}

private fun generateRequiredOverload(kirClass: KirClass, enum: SirClass): SirSimpleFunction =
Expand All @@ -44,6 +45,16 @@ class SealedFunctionGeneratorDelegate(
}
}

private fun generateRequiredOverloadWithOptionalReturnType(kirClass: KirClass, enum: SirClass, requiredFunction: SirSimpleFunction) {
createFunctionDeclaration(
kirClass = kirClass,
enum = enum,
returnTypeModifier = { it.toNullable() },
).apply {
addRequiredOverloadWithOptionalReturnTypeFunctionBody(requiredFunction)
}
}

private fun createFunctionDeclaration(
kirClass: KirClass,
enum: SirClass,
Expand Down Expand Up @@ -175,4 +186,16 @@ class SealedFunctionGeneratorDelegate(
)
}
}

private fun SirSimpleFunction.addRequiredOverloadWithOptionalReturnTypeFunctionBody(requiredFunction: SirSimpleFunction) {
bodyBuilder.add {
val valueParameter = valueParameters.first()

addCode(
CodeBlock.builder()
.add("return %L as %T", requiredFunction.call(valueParameter), requiredFunction.returnType.evaluate().swiftPoetTypeName)
.build(),
)
}
}
}

0 comments on commit b25e832

Please sign in to comment.