Closures don't recognize class hierarchy
See original GitHub issueI 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:
- Created 6 years ago
- Comments:6
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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)
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!