Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
bozaro opened this issue Aug 1, 2024 · 3 comments
Labels
faq good first issue Good for newcomers help wanted Extra attention is needed

Comments

@bozaro
Copy link

bozaro commented Aug 1, 2024

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)
@bozaro bozaro changed the title Is it possible to check that an object can be cast to the type (something like "test" is str)? Is it possible to check that an object can be cast to the type (something like "test" is A)? Aug 1, 2024
@bozaro bozaro changed the title Is it possible to check that an object can be cast to the type (something like "test" is A)? Is it possible to check that an object can be cast to the type (something like "test" is B)? Aug 1, 2024
@Peefy
Copy link
Contributor

Peefy commented Aug 1, 2024

KCL currently supports two types of type assertions: typeof(foo) == "B" or foo as B. Your requirement seems to be to directly obtain the reflection ability of subclasses of B?

As an aside, although implementing complex reflections for KCL is not that difficult, perhaps we can increase the ability of runtime.subclasses(). However, even in languages such as Go and Rust, obtaining a subtype of a type is not an easy task.

@bozaro
Copy link
Author

bozaro commented Aug 1, 2024

In finally I wan create some logic for type B and all subtypes.

Something like:

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"
}
to_B = lambda o: A -> B {
    # Is it possible make this expression without compare with all child type names?
    _x: B = Undefined
    if typeof(o) == "B":
        _x = o as B

    if typeof(o) == "B1":
        _x = o as B1

    if typeof(o) == "B2":
        _x = o as B2

    _x
}

bar = lambda a: A {
    _x: B = Undefined
    if is_B(a):
        _x = to_B(a)

    {
        if _x:
            test: _x.foo
        
    }
}

In my current task type hierary is something like: endpoint declaration -> endpoint declaration with chart deploy info -> endpoint declaration with chart deploy info with project-specific default

@Peefy
Copy link
Contributor

Peefy commented Aug 2, 2024

I see.

To achieve this, there is currently no great way in KCL. Possible solutions in the future:

  • Use advanced API of runtime reflection in KCL code to obtain B and all subtypes of B e.g. runtime.subtypes(A)
  • Obtain B and all subtypes of B through static analysis through GetSchemaTypeMapping API, and then generate is_B and to_B functions.

@Peefy Peefy added the faq label Aug 2, 2024
@Peefy Peefy changed the title Is it possible to check that an object can be cast to the type (something like "test" is B)? [FAQ] Is it possible to check that an object can be cast to the type (something like "test" is B)? Aug 2, 2024
@Peefy Peefy transferred this issue from kcl-lang/kcl Aug 5, 2024
@Peefy Peefy added help wanted Extra attention is needed good first issue Good for newcomers labels Aug 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
faq good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants