Kotlin - 如何在 kotlin 中使用闭包和柯里化来传递参数以返回具有多个参数的函数?
Kotlin - how to use closure and currying in kotlin to pass a paramater to returned function with multiple arguments?
我有一个枚举 class,看起来像这样:
enum class CommandTypes(val access: (Ctx) -> Unit, val command: (Ctx, JsonObject) -> Either<Failure, FlatResp>) {
CREATE_DELIVERED_EVENT(adminOnly, ::createDeliveredEvent),
CREATE_SWITCHED_EVENT(adminOnly, ::createSwitchedEvent),
CREATE_STAMPED_EVENT(adminOnly, ::createStampedEvent),
除了command/event的类型之外,所有这些commands/events都有相同的逻辑。
它们看起来都是这样的:
fun createDeliveredEvent(ctx: Ctx, json: JsonObject): Either<Failure, FlatResp> =
validateAndCreateEvent(ctx, json, EventType.DELIVERED_EVENT)
fun createDeliveredEvent(ctx: Ctx, json: JsonObject): Either<Failure, FlatResp> =
validateAndCreateEvent(ctx, json, EventType.SWITCHED_EVENT)
fun createDeliveredEvent(ctx: Ctx, json: JsonObject): Either<Failure, FlatResp> =
validateAndCreateEvent(ctx, json, EventType.STAMPED_EVENT)
是否可以通过柯里化将一种类型的事件传递给命令函数,这样我就可以拥有一个接受类型的通用函数和 return 一个将类型作为参数的函数。我试过这样的事情:
fun createEvent(type:AirExpressConsignmentEventType): (ctx: Ctx, json: JsonObject) -> Either<Failure, FlatResp> =
{ ctx, json -> validateAndCreateEvent(ctx, json, type)}
我在 CommandTypes 枚举中这样使用它 class:
CREATE_DELIVERED_EVENT(adminOnly, (::createEvent(EventType.DELIVERED_EVENT)))
但是,我得到一个错误:
This syntax is reserved for future use; to call a reference, enclose
it in parentheses: (foo::bar)(args)
当我已经有了另一段使用相同语法的代码时,我不确定为什么会得到这个?
groups = (::infoGroupingWithContext)(ctx)
fun infoGroupingWithContext(ctx: Ctx): (List<Map<String, Any?>>) -> List<Map<String, Any?>> = { rows -> infoGrouping(ctx, rows) }
语法为
fun createEvent(type: EventType): (ctx: Ctx, JsonObject) -> Either<Failure, FlatResp> =
{ ctx, json -> validateAndCreateEvent(ctx, json, type) }
// or perhaps less confusing:
fun createEvent(type: EventType): (ctx: Ctx, JsonObject) -> Either<Failure, FlatResp> {
return { ctx, json -> validateAndCreateEvent(ctx, json, type) }
}
// or
fun createEvent(type: EventType) = fun (ctx: Ctx, json: JsonObject): Either<Failure, FlatResp> {
return validateAndCreateEvent(ctx, json, type)
}
但这样做可能比间接调用该函数更简洁:
enum class CommandTypes(
val access: (Ctx) -> Unit,
private val type: EventType
) {
CREATE_DELIVERED_EVENT(adminOnly, EventType.DELIVERED_EVENT),
CREATE_SWITCHED_EVENT(adminOnly, EventType.SWITCHED_EVENT),
CREATE_STAMPED_EVENT(adminOnly, EventType.STAMPED_EVENT);
val command: (Ctx, JsonObject) -> Either<Failure, FlatResp>) = {
validateAndCreateEvent(ctx, json, type)
}
}
我有一个枚举 class,看起来像这样:
enum class CommandTypes(val access: (Ctx) -> Unit, val command: (Ctx, JsonObject) -> Either<Failure, FlatResp>) {
CREATE_DELIVERED_EVENT(adminOnly, ::createDeliveredEvent),
CREATE_SWITCHED_EVENT(adminOnly, ::createSwitchedEvent),
CREATE_STAMPED_EVENT(adminOnly, ::createStampedEvent),
除了command/event的类型之外,所有这些commands/events都有相同的逻辑。 它们看起来都是这样的:
fun createDeliveredEvent(ctx: Ctx, json: JsonObject): Either<Failure, FlatResp> =
validateAndCreateEvent(ctx, json, EventType.DELIVERED_EVENT)
fun createDeliveredEvent(ctx: Ctx, json: JsonObject): Either<Failure, FlatResp> =
validateAndCreateEvent(ctx, json, EventType.SWITCHED_EVENT)
fun createDeliveredEvent(ctx: Ctx, json: JsonObject): Either<Failure, FlatResp> =
validateAndCreateEvent(ctx, json, EventType.STAMPED_EVENT)
是否可以通过柯里化将一种类型的事件传递给命令函数,这样我就可以拥有一个接受类型的通用函数和 return 一个将类型作为参数的函数。我试过这样的事情:
fun createEvent(type:AirExpressConsignmentEventType): (ctx: Ctx, json: JsonObject) -> Either<Failure, FlatResp> =
{ ctx, json -> validateAndCreateEvent(ctx, json, type)}
我在 CommandTypes 枚举中这样使用它 class:
CREATE_DELIVERED_EVENT(adminOnly, (::createEvent(EventType.DELIVERED_EVENT)))
但是,我得到一个错误:
This syntax is reserved for future use; to call a reference, enclose it in parentheses: (foo::bar)(args)
当我已经有了另一段使用相同语法的代码时,我不确定为什么会得到这个?
groups = (::infoGroupingWithContext)(ctx)
fun infoGroupingWithContext(ctx: Ctx): (List<Map<String, Any?>>) -> List<Map<String, Any?>> = { rows -> infoGrouping(ctx, rows) }
语法为
fun createEvent(type: EventType): (ctx: Ctx, JsonObject) -> Either<Failure, FlatResp> =
{ ctx, json -> validateAndCreateEvent(ctx, json, type) }
// or perhaps less confusing:
fun createEvent(type: EventType): (ctx: Ctx, JsonObject) -> Either<Failure, FlatResp> {
return { ctx, json -> validateAndCreateEvent(ctx, json, type) }
}
// or
fun createEvent(type: EventType) = fun (ctx: Ctx, json: JsonObject): Either<Failure, FlatResp> {
return validateAndCreateEvent(ctx, json, type)
}
但这样做可能比间接调用该函数更简洁:
enum class CommandTypes(
val access: (Ctx) -> Unit,
private val type: EventType
) {
CREATE_DELIVERED_EVENT(adminOnly, EventType.DELIVERED_EVENT),
CREATE_SWITCHED_EVENT(adminOnly, EventType.SWITCHED_EVENT),
CREATE_STAMPED_EVENT(adminOnly, EventType.STAMPED_EVENT);
val command: (Ctx, JsonObject) -> Either<Failure, FlatResp>) = {
validateAndCreateEvent(ctx, json, type)
}
}