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

[feature] Support for type arguments for convenient factory functions #1265

Closed
dennybaa opened this issue May 1, 2024 · 3 comments · Fixed by #1269
Closed

[feature] Support for type arguments for convenient factory functions #1265

dennybaa opened this issue May 1, 2024 · 3 comments · Fixed by #1269
Assignees
Labels
enhancement New feature or request resolver

Comments

@dennybaa
Copy link

dennybaa commented May 1, 2024

Hello there! I've stumbled on a potentially useful scenario. It's actually already operational in KCL, but lacks syntax sugar and notion of that we are working with types. The bellow code implements a factory function:

schema Foo:
    foo: str = "foo"

schema Bar:
    bar: str = "bar"

factory = lambda $type: any -> Foo | Bar {
    func = $type
    print("typeof func: ", end='')
    print(func)
    print("---------------------")
    instance = func()
}
_foo = factory(Foo)
_bar = factory(Bar)
if typeof(_foo) == "Foo":
    foo = _foo as Foo

if typeof(_bar) == "Bar":
    bar = _bar as Foo

It works...) But not for everything, for example instances of types which have required attributes can not be created following the above approach.

Ideally to have a language feature to be able to do something like bellow:

factory = lambda $type: type -> Foo | Bar {
    $type { required = "foo" }
}

Along with this, it could make sense to have func type too...

What do you think of this @Peefy , could this be a good realistic idea? Thank you!

@Peefy
Copy link
Contributor

Peefy commented May 1, 2024

I agree.

If we need to have factory for any schema and required attributes. We may have the following form

type {**required_attrs}

However, KCL currently check the schema type strictly. In the next version, I believe that you can impl the type factory and I will do some minor optimzed changes for KCL and show you the example later. 😃

@Peefy Peefy added this to the v0.9.0 Release milestone May 1, 2024
@Peefy Peefy self-assigned this May 1, 2024
@Peefy Peefy added enhancement New feature or request resolver labels May 1, 2024
@Peefy
Copy link
Contributor

Peefy commented May 2, 2024

I've opened a PR to impl this: #1269

But I have not added the type type for the arguments and used a runtime type type assert statement to verify the argument type at runtime.

schema Foo:
    foo: str

schema Bar:
    bar: str

factory = lambda type: any, attrs: {:} = {} -> Foo | Bar {
    assert typeof(type) == "type"
    func = $type
    instance = func() {**attrs}
}

_foo = factory(Foo, {foo = "foo"}) # Note we set attributes here.
_bar = factory(Bar, {bar = "bar"}) # Note we set attributes here.
if typeof(_foo) == "Foo":
    foo = _foo as Foo

if typeof(_bar) == "Bar":
    bar = _bar as Bar

@dennybaa
Copy link
Author

dennybaa commented May 7, 2024

Thanks a lot Peefy! Very solid 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request resolver
Projects
None yet
2 participants