gpt4 book ai didi

kotlin - Kotlin结合了arrow,lamda,?,。的用法。 , 然后让。什么意思?

转载 作者:行者123 更新时间:2023-12-02 13:20:30 25 4
gpt4 key购买 nike

fun theItemDTO.toDomainModel(
domainOrderId: String,
pIds: List<Long> = emptyList()
): theItem = let { dto ->
OrderProtoBuilders.theItem {
this.id = dto.id.toString()
skuId = dto.catalogEntryId.toString()
orderId = domainOrderId
quantity = dto.quantity
unitPrice = dto.unitPrice
totalPrice = dto.totalPrice
price = null
for (pId in pIds)
addpId(pId.toString())

dto.someMap[MAP_A]
?.let(::setAId)

dto.someMap[MAP_B]
?.let(::setBId)
}
}

对于上面的kotlin函数,我的问题顺序从内部块代码到外部块:
1. dto.someMap[MAP_A]
?.let(::setAId)
在做什么?它看起来像是 someMap[MAP_A]的重新分配,但也声明了 let的某些内容……它在做什么?
  • 这么多{包装器的含义与return相同。正确?因为我确实读过->,就像自动返回值的函数一样。那么...: theItem的最终值真的是OrderProtoBuilders.theItem {...创建的吗?
  • let { dto ->
    OrderProtoBuilders.theItem {...

    最佳答案

  • dto.someMap[MAP_A]?.let(::setAId)可以像
  • 一样扩展
    val value = dto.someMap[MAP_A]
    if (value != null) {
    setAId(value)
    }

    2。
    是的,这是正确的。你可以这样想
    //the whole function will return item created by OrderProtoBuilders.theItem
    fun theItemDTO.toDomainModel(
    domainOrderId: String,
    pIds: List<Long> = emptyList()
    ): theItem = let { dto ->
    //return OrderProtoBuilders.theItem
    OrderProtoBuilders.theItem {
    //do some additional initialization of the Item
    }
    }

    关于kotlin - Kotlin结合了arrow,lamda,?,。的用法。 , 然后让。什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57500208/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com