question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Closures don't recognize class hierarchy

See original GitHub issue

I have this closure interface:

public interface ItemClosure
	function run(UnitInventory inventory, ItemInstance inst) 

I also have this assignment somewhere in code:

onUnequip = (UnitInventory inventory, ItemInstance inst) -> begin
    let armor = inst castTo ItemInstanceArmor
    inventory.getUnit().defense.add(armor.def)
end

ItemInstanceArmor is a subclass of ItemInstance, so this works, nothing weird there. What doesn’t work, however, is this:

onUnequip = (UnitInventory inventory, ItemInstanceArmor inst) -> begin
    inventory.getUnit().defense.add(inst.def)
end

I get a huge red line across the whole assignment because:

file: 'file:///x%3A/wc3/projects/TheRpg/wurst/equipment/classes/ItemClassArmor.wurst'
severity: 'Error'
message: 'Cannot assign (UnitInventory, ItemInstanceArmor) -> Void to ItemClosure'
at: '42,9'
source: 'Wurst'

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
peqcommented, Dec 27, 2017

Downcasts are not safe, so they are not done automatically.

This is the same (or even more restrictive) in most languages. If you want to read more about it have a look at https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)

0reactions
IgorSamuroviccommented, Dec 28, 2017

Oh, I didn’t think about that at all. I’ve now spent some time reading up on the subject and I realized I didn’t actually understand the problematics of it. Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to decide between classes v. closures in JavaScript
This tutorial compares the functionality and advantages of classes versus closures in JavaScript to help you determine which is best for ...
Read more >
Are a class's methods a type of closure?
I would assert that most class's methods don't do this, but I'll allow that there may be environments where this is a recognized...
Read more >
"Closures are poor man's objects and vice versa" - What does ...
Objects are poor man's closures. Consider Java. Java is an object-oriented programming language with no language level support for real lexical closures.
Read more >
Closures And Objects Are Equivalent - C2 wiki
Polymorphism and inheritance are used to specialize objects, closures are used to create anonymous one-shot objects that don't need specialization, they are ...
Read more >
Closures - JavaScript - MDN Web Docs
A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found