出现错误:(55, 41) Kotlin:类型推断失败

Posted

技术标签:

【中文标题】出现错误:(55, 41) Kotlin:类型推断失败【英文标题】:Getting Error:(55, 41) Kotlin: Type inference failed 【发布时间】:2019-09-05 08:49:06 【问题描述】:

我收到Error:(55, 41) Kotlin: Type inference failed. Expected type mismatch: inferred type is (Mutable)List<Optional<Address!>!>! but List<Address>? was expected

我将数据保存到我的 build.gradle 的 mongoDB 文档中:

implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
implementation 'org.springframework.boot:spring-boot-starter-data-rest'
implementation 'org.springframework.boot:spring-boot-starter-hateoas'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'com.fasterxml.jackson.module:jackson-module-kotlin'

我的文档对象

@Document(collection = "address")
class Address 
    @Id
    var id: String? = null
    var number: String? = null
    var street: String? = null
    var neighborhood: String? = null
    var locality: String? = null
    var region: String? = null
    var country: String? = null
    var code: String? = null
    var google_place_id: String? = null

    constructor()

@Document(collection = "person")
class Person 
    @Id
    var id: String? = null
    var name: String? = null
    var lastName: String? = null
    var phone: String? = null
    var email: String? = null
    var password: String? = null
    @DBRef(db = "mlm")
    var address: List<Address>? = null

    constructor()

我有接口

@Repository
interface PersonRepository : MongoRepository<Person, String>

@Repository
interface AddressRepository : MongoRepository<Address, String>

我的控制器工作 我的播种机/井测试真的要看看如何做到这一点,这就是我遇到问题的地方

class DbSeeder 
    @Autowired
    private val personRepository: PersonRepository? = null
    @Autowired
    private val addressRepository: AddressRepository? = null

    fun addressLoading() 
        val address1 = Address()
        address1.id = "5cb2e9424274072ec4bb4199"
        address1.number = "1"
        address1.street = "Microsoft Way"
        address1.neighborhood = "Redmond"
        address1.locality = "King County"
        address1.region = "Washington"
        address1.code = "425"
        address1.country = "United States"
        address1.google_place_id = "5644+83 Centurion"

        val address2 = Address()
        address2.id = "5cb2e9424274072ec4bb4198"
        address2.number = "1600"
        address2.street = "Amphitheatre Parkway"
        address2.neighborhood = ""
        address2.locality = "Mountain View"
        address2.region = "California"
        address2.country = "United States"
        address2.code = "94043"
        address2.google_place_id = "CWC8+Q9 Mountain View, California, USA"

        val address = Arrays.asList(address1, address2)
        this.addressRepository!!.insert(address)
    

    fun personLoading() 
        val personDocument = Person()
        personDocument.id = "5cb2e9424274072ec4bb4197"
        personDocument.name = "William"
        personDocument.lastName = "Gates"
        personDocument.phone = "1081010810"
        personDocument.email = "bill.gates@gmail.com"
        personDocument.password = "bill-secret"

        val personAddressDBRef = addressRepository!!.findById("5cb2e9424274072ec4bb4199")

        personDocument.address = Arrays.asList(personAddressDBRef)

    //  val personDBRef = personRepository!!.save(personDocument)  // If the ObjectID is requires else ware
        personRepository!!.save(personDocument)
    

要保存我的地址,我需要找到与 ObjectID 相关的地址,然后将 ObjectID 保存为个人文档中的 DBRef

val personAddressDBRef = addressRepository!!.findById("5cb2e9424274072ec4bb4199")
personDocument.address = Arrays.asList(personAddressDBRef) // << Error here

在最后一行我收到错误Error:(55, 41) Kotlin: Type inference failed. Expected type mismatch: inferred type is (Mutable)List&lt;Optional&lt;Address!&gt;!&gt;! but List&lt;Address&gt;? was expected 我似乎不知道如何继续 我在 GitHub 上提供了它https://github.com/Morons/gofetchbyidandinsertdbref.git 任何帮助将不胜感激

【问题讨论】:

这里显示的错误来自日志,没有错误@yole你有解决方案吗? 错误没有正确显示错误。因为它们包含&lt;&gt;,所以它们需要用反引号转义。 【参考方案1】:

您需要阅读https://kotlinlang.org/docs/reference/java-interop.html#null-safety-and-platform-types 以了解推断类型的编写方式。但问题很简单:

    personAddressDBRef 具有 Optional&lt;Address&gt; 类型,因为这是 findById 返回的内容。

    所以Arrays.asList(personAddressDBRef)List&lt;Optional&lt;Address&gt;&gt;(同样,请参阅上面的链接了解为什么您会看到更复杂的类型)。

    您不能将personDocument.address 设置为List&lt;Optional&lt;Address&gt;&gt;,而是需要List&lt;Address&gt;

因此您需要决定如何将Optional&lt;Address&gt; 转换为List&lt;Address&gt;。一种方法是

personDocument.address = personAddressDBRef.map  listOf(it) .orElseGet  listOf() 

【讨论】:

是什么导致 Bundle parcelables 出现 Type inference failed 错误? 您是在询问一些不同的代码吗?这里不涉及捆绑或包裹。

以上是关于出现错误:(55, 41) Kotlin:类型推断失败的主要内容,如果未能解决你的问题,请参考以下文章

在 Kotlin 中无法“findViewById”。收到错误“类型推断失败”

Kotlin 类型不匹配:推断类型是 View!但 TextView 是预期的

kotlin 中的 Observable.combineLatest 类型推断

Kotlin - 类型不匹配:推断类型是 Any?但布尔值是预期的

Kotlin函数 ⑤ ( 匿名函数变量类型推断 | 匿名函数参数类型自动推断 | 匿名函数又称为 Lambda 表达式 )

KotlinKotlin 变量与常量 ( 变量声明 | 只读变量 | 类型推断 | 编译时常量 | Kotlin 字节码查看面板 | Kotlin 引用数据类型 )