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] Return types in lambdas? #315

Closed steeling closed 5 months ago

steeling commented 5 months ago

Looking to get the validation from specifying a return type in lambdas. Is this currently possible?

Something like

genTile = lambda _manifest: MyType1 {
    ret = MyType2 {
        ....
    }
}.ret

Is how I could do this in some other DSL's. Does KCL have some way to specify return types?

steeling commented 5 months ago
genTile = lambda _manifest: MyType1 => MyType2 {
    ...
}

Could be another syntax, unless this is already currently feasible?

steeling commented 5 months ago

nvm, answer is to do:

genTile = lambda _manifest: MyType1 { MyType2 { .... } }

Peefy commented 5 months ago

For lambda, KCL will automatically infer your return value type in the function body, although you can explicitly specify it. For example:

f1 = lambda t: Type1 {
    Type2 {}
} # The type of f1 is (Type1) -> Type2

f2 = lambda t: Type1 -> Type2 {
    Type2 {}
} # The type of f2 is (Type1) -> Type2
d4v1d03 commented 5 months ago

Hey @Peefy , made a PR #324

Peefy commented 5 months ago

Closed by #324