Skip to content

[FAQ] Is it possible to check that an object can be cast to the type (something like "test" is B)? #433

Open
@bozaro

Description

@bozaro

General Question

I want to check that an obkect can be cast to the type.
Currently I found only one method: get type name with typeof and compare with target type name and all of childs target type names.

For example:

schema A:
    name: str

schema B(A):
    foo: str

schema B1(B):
    foo = "1"

schema B2(B):
    foo = "2"

is_B = lambda o: A {
    # Is it possible make this expression without compare with all child type names?
    typeof(o) == "B" or typeof(o) == "B1" or typeof(o) == "B2"
}

a = A {name = "a"}

b = B {name = "b", foo = "3"}

b1 = B1 {name = "b1"}

b2 = B2 {name = "b2"}

check_a = is_B(a)
check_b = is_B(b)
check_b1 = is_B(b1)
check_b2 = is_B(b2)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions