Skip to content
This repository has been archived by the owner on Jun 14, 2022. It is now read-only.

How to denote generic function that cast from parent class to child class? #6

Open
JamesKim2998 opened this issue May 6, 2017 · 0 comments

Comments

@JamesKim2998
Copy link

JamesKim2998 commented May 6, 2017

I'm now trying to cast parent class(Component) to child class(WeaponComponent).

So I made function cast_to_weapon,

Component = class()
WeaponComponent = class()
--v function(parent: Component) --> WeaponComponent
function cast_to_weapon(parent)
    --# assume parent: WeaponComponent
    return parent
end

But as you can see, it's hard to reuse. Because, source type only can be Component, and destination type only can be WeaponComponent...

So I want to denote this like below,

--function(parent: Parent, childType: typeof(Child)) -> Child where Child extends Parent
function cast_to(parent, childType)
    return type
end

local parent = Component.new()
local child = cast_to(parent, WeaponComponent)

How can this be possible?

@JamesKim2998 JamesKim2998 changed the title How to denote function that cast from parent class to child class? How to denote generic function that cast from parent class to child class? May 6, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant