0

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

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:

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. I tried this@Authenticate and it's a no go. Hell, I might even be over complicating it. Thanks for the insight.

4
  • You need to show your compiler errors. And remove the "return" from inside the lazy, you do not return when in a lambda. Commented Nov 3, 2016 at 20:05
  • also, can you make a sample that has fewer dependencies? Commented Nov 3, 2016 at 20:07
  • Will update right now Commented Nov 3, 2016 at 20:08
  • You still should update your question with the error messages exactly as shown from the compiler. our question should help other people with the same or similar issues, and you haven't provided enough detail for that to happen. Commented Nov 3, 2016 at 20:52

2 Answers 2

3

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. You need a typed response such as the Bounds class or a Map (which would be icky). And also in your version they could be 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.

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 ?. safe operator instead of !!. and change the Bounds class to allow 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.

Sign up to request clarification or add additional context in comments.

Comments

-1

Ok, I solved the problem: 2 fold

Syntax error as "unknown symbol"? I needed = and not : (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:

@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

6 Comments

This answer solves some of the compiler errors but the solution does not work. ne and sw are not accessible by anyone because the class is Any created from an anonymous object expression that cannot be accessed by its type.
You introduced new information in this answer that should have been added to the question.
The Lombok issue would likely be resolved by making sure Kotlin annotation processor (KAPT) is in use, so that Lombok has done its magic before the Kotlin compiler has processed the source code since Kotlin runs first before Java processing. So your answer works around that, but isn't the right way to handle integration with Java and annotation processors running there.
Actually, it's marshalled out as JSON and works as I need it to. But, I get what you're saying. I am will move it to a real map. Let me also see about turning on the KAPT
If I modify the Question to define "hub" as Java with the correct annotations, can I have the vote back?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.