You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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!
The text was updated successfully, but these errors were encountered:
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. 😃
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:
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:
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!
The text was updated successfully, but these errors were encountered: