0

Sorry for the terrible title, but I can't seem to find an allowable way to ask this question, because I don't know how to refer to the code constructs I am looking at.

Looking at this file: https://github.com/Hexworks/caves-of-zircon-tutorial/blob/master/src/main/kotlin/org/hexworks/cavesofzircon/systems/InputReceiver.kt

I don't understand what is going on here:

    override fun update(entity: GameEntity<out EntityType>, context: GameContext): Boolean {
    val (_, _, uiEvent, player) = context

I can understand some things.

We are overriding the update function, which is defined in the Behavior class, which is a superclass of this class.

The update function accepts two parameters. A GameEntity named entity, and a GameContext called context.

The function returns a Boolean result.

However, I do not understand the next line at all. Just open and close parentheses, two underscores as the first two parameters, and then an assignment to the context argument. What is it we are assigning the value of context to?

Based on IDE behavior, apparently the open-close parentheses are related to the constructor for GameContext. But I would not know that otherwise. I also don't understand what the meaning is of the underscores in the argument list.

And finally, I have read about the declaration-site variance keyword "out", but I don't really understand what it means here. We have GameEntity<out EntityType>. So as I understand it, that means this method produces EntityType, but does not consume it. How is that demonstrated in this code?

2 Answers 2

2
val (_, _, uiEvent, player) = context

You are extracting the 3rd and 4th value from the context and ignoring the first two.

Compare https://kotlinlang.org/docs/reference/multi-declarations.html .

About out: i don't see it being used in the code snippet you're showing. You might want to show the full method. Also, maybe it is there only for the purpose of overriding the method, to match the signature of the function.

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

2 Comments

Thank you. Looks like "Destructuring Declarations" is the concept I need to read about. It's tricky with learning a new language when you see a new construct and don't know how to refer to it for research. Also I think you are right about the out thing, it's probably just there to satisfy the signature.
Glad i could be of help :)
1

To cover the little bit that Incubbus's otherwise-great answer missed:

In the declaration

override fun update(entity: GameEntity<out EntityType>, // …

the out means that you could call the function and pass a GameEntity<SubclassOfEntityType> (or even a SubclassOfGameEntity<SubclassOfEntityType>).

With no out, you'd have to pass a GameEntity<EntityType> (or a SubclassOfGameEntity<EntityType>).

I guess that's inherited from the superclass method that you're overriding.  After all, if the superclass method could be called with a GameEntity<SubclassOfEntityType>, then your override will need to handle that too.  (The Liskov substitution principle in action!)

Comments

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.