简体   繁体   English

不能初始化Kotlin类属性的延迟初始化

[英]Lazy initialization of Kotlin class property won't compile

I am not entirely sure that what I think is the problem actually it. 我不确定我认为实际上是问题所在。 But I am trying to use lazy as a delegate and I am getting compiler errors 但是我试图使用lazy作为委托,并且遇到编译器错误

data class Geocode(var latitude: Double, var longitude: Double) : Comparable<Geocode> {


    override fun compareTo(other: Geocode): Int {
        var result = this.latitude.compareTo(other.latitude)
        if (result == 0)
            result = this.longitude.compareTo(other.longitude)
        return result
    }
}

data class HubKt(val position:Geocode) {
}

data class Example(val hubs:Collection<HubKt>) {

    val bounds:Any by lazy {
        object {
            val ne: this.hubs.map { h -> h.position }.max()
            val sw: this.hubs.map { h -> h.position }.min()
        }

    }
}

if this were java, I would want the bounds function to return a map: 如果这是java,我希望bounds函数返回一个映射:

public Map<String,Geocode> getBounds() {
        Geocode ne = geos.stream().max(Geocode::compareTo).get();
        Geocode sw = geos.stream().min(Geocode::compareTo).get();
        return ImmutableMap.of("ne",ne,"sw",sw);
}

I think the problem is not using the correct this . 我认为这个问题是没有使用正确的this I tried this@Authenticate and it's a no go. 我尝试过this@Authenticate ,这是不行的。 Hell, I might even be over complicating it. 地狱,我什至可能过于复杂了。 Thanks for the insight. 感谢您的见解。

Based on the current code in the question: 根据问题中的当前代码:

data class Bounds(val ne: Geocode, val sw: Geocode)

data class Example(val hubs:Collection<HubKt>) {
    val bounds: Bounds by lazy {
        Bounds(hubs.map { it.position }.max()!!, 
               hubs.map { it.position }.min()!!)
    }
} 

Otherwise in your answer you won't be able to access ne and sw in your anonymous descendant of Any that you create via the object expression. 否则,您将无法通过对象表达式创建的Any的匿名后代访问nesw You need a typed response such as the Bounds class or a Map (which would be icky). 您需要一个类型化的响应,例如Bounds类或Map (可能会很棘手)。 And also in your version they could be null. 而且在您的版本中,它们可以为null。 If you know you have at least one value in the list you can use !! 如果您知道列表中至少有一个值,则可以使用!! to assert you know the result of max or min will not be null. 断言您知道maxmin的结果不会为空。

You can do this without the copy created by the map with this change: 您可以执行以下操作,而无需更改地图创建的副本:

data class Example(val hubs:Collection<HubKt>) {
    val bounds: Bounds by lazy {
        Bounds(hubs.maxBy { it.position }!!.position, 
               hubs.minBy { it.position }!!.position)
    }
}    

Or if you want nulls as a possible bounds use the ?. 或者,如果您希望将null作为可能的界限,请使用?. safe operator instead of !!. 安全的运算符,而不是!!. and change the Bounds class to allow null : 并将Bounds类更改为允许null

data class Bounds(val ne: Geocode?, val sw: Geocode?)

data class Example(val hubs:Collection<HubKt>) {
    val bounds by lazy {
        Bounds(hubs.maxBy { it.position }?.position, 
               hubs.minBy { it.position }?.position)
    }
}

Notice in the last example I dropped the type from val bounds: Bounds because it is optional and type inference will figure it out just fine. 请注意,在上一个示例中,我从val bounds: Bounds删除了类型val bounds: Bounds因为它是可选的,并且类型推断可以很好地解决它。

Ok, I solved the problem: 2 fold 好吧,我解决了这个问题:2折

Syntax error as "unknown symbol"? 语法错误为“未知符号”吗? I needed = and not : (DOH!) 我需要=而不是: (DOH!)

  val bounds:Any by lazy {
        object {
            val ne = hubs.map { h -> h.position }.max()
            val sw = hubs.map { h -> h.position }.min()
        }

    }

Lombok: position in Hub has it's getter generated by Lombok: 龙目岛: Hub中的position是由龙目岛产生的吸气剂:

@Getter
@Setter
private Geocode position = new Geocode(50.0,50.0);

Change to: 改成:

@Setter
private Geocode position = new Geocode(50.0,50.0);

public Geocode getPosition() {
    return position;
}

Ultimately this was an integration issue. 最终,这是一个集成问题。 sigh

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM