kcl-lang / kcl-lang.io

KCL Website and Documentation Repo: https://kcl-lang.io
https://kcl-lang.github.io
Apache License 2.0
13 stars 35 forks source link

[FAQ] schema lambda support #309

Closed wilsonwang371 closed 3 months ago

wilsonwang371 commented 6 months ago

General Question

is it possible to support schema lambdas (class methods) in kcl. we can have a self keyword which can be used to access a readonly instance.

Peefy commented 6 months ago

In fact, KCL currently supports defining member functions for schemas and can omit self.

schema Person:
    firstName: str
    lastName: str
    getFullName: () -> str = lambda {
        firstName + " " + lastName
    }

p = Person{
    firstName = "Alice"
    lastName = "White"
}
fullName = p.getFullName()

The output is

p:
  firstName: Alice
  lastName: White
fullName: Alice White

Of course, this is not as complex in configuration languages, just defining it is enough.

schema Person:
    firstName: str
    lastName: str
    fullName: str = firstName + " " + lastName

p = Person{
    firstName = "Alice"
    lastName = "White"
}
fullName = p.fullName
wilsonwang371 commented 6 months ago

Is it possible to support

    getFullName = lambda {
        firstName + " " + lastName
    }

as well?

Peefy commented 5 months ago

This is possible, but in KCL, schema members without type annotations are treated as private. image

wilsonwang371 commented 5 months ago

this is not very straightforward. can we have something like underscore in python or upper/lower case in golang. i know this already conflict with current version.

Peefy commented 5 months ago

Make sense. We are exploring the possibility of simplifying KCL and clarifying its semantics. Perhaps this situation will change in future versions. In addition, we will also provide more user-friendly prompt information for additional enhancements.

d4v1d03 commented 3 months ago

Hey @Peefy , made the PR #377