Skip to content
This repository was archived by the owner on Jun 14, 2022. It is now read-only.
This repository was 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

@JamesKim2998

Description

@JamesKim2998

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions