Skip to content

Commit 7b9dd4f

Browse files
authored
Merge pull request #369 from Peefy/faq-function-type-annotation-and-infer
docs(faq): add more instructions and examples for the function type annotation
2 parents 097e552 + 4189615 commit 7b9dd4f

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

docs/user_docs/support/faq-kcl.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2513,18 +2513,29 @@ s: Hello world
25132513
raw_s: Hello ${worldString}
25142514
```
25152515

2516-
## 59. How does KCL infer return types in lambdas?
2516+
## 59. How does KCL infer types in lambdas or how to specify the type for function variables?
25172517

2518-
For lambda(s),KCL automatically infers the return value type in the function body, although we can explicitly specify it. An example KCL code over here is:
2518+
For lambda(s), KCL automatically infers the parameter types and return value type in the function body, although we can explicitly specify it. An example KCL code over here is:
25192519

25202520
```python
2521+
schema Type1:
2522+
foo?: int
2523+
2524+
schema Type2:
2525+
bar?: int
2526+
25212527
f1 = lambda t: Type1 {
25222528
Type2 {}
25232529
} # The type of f1 is (Type1) -> Type2
2524-
25252530
f2 = lambda t: Type1 -> Type2 {
25262531
Type2 {}
25272532
} # The type of f2 is (Type1) -> Type2
2533+
f3: (Type1) -> Type2 = lambda t: Type1 -> Type2 {
2534+
Type2 {}
2535+
} # The type of f2 is (Type1) -> Type2
2536+
f4: (Type1) -> Type2 = lambda t {
2537+
Type2 {}
2538+
} # The type of f2 is (Type1) -> Type2
25282539
```
25292540

25302541
## 60. How to convert a list of lists to a single list in KCL?

versioned_docs/version-0.8/user_docs/support/faq-kcl.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2513,18 +2513,29 @@ s: Hello world
25132513
raw_s: Hello ${worldString}
25142514
```
25152515

2516-
## 59. How does KCL infer return types in lambdas?
2516+
## 59. How does KCL infer types in lambdas or how to specify the type for function variables?
25172517

2518-
For lambda(s),KCL automatically infers the return value type in the function body, although we can explicitly specify it. An example KCL code over here is:
2518+
For lambda(s), KCL automatically infers the return value type in the function body, although we can explicitly specify it. An example KCL code over here is:
25192519

25202520
```python
2521+
schema Type1:
2522+
foo?: int
2523+
2524+
schema Type2:
2525+
bar?: int
2526+
25212527
f1 = lambda t: Type1 {
25222528
Type2 {}
25232529
} # The type of f1 is (Type1) -> Type2
2524-
25252530
f2 = lambda t: Type1 -> Type2 {
25262531
Type2 {}
25272532
} # The type of f2 is (Type1) -> Type2
2533+
f3: (Type1) -> Type2 = lambda t: Type1 -> Type2 {
2534+
Type2 {}
2535+
} # The type of f2 is (Type1) -> Type2
2536+
f4: (Type1) -> Type2 = lambda t {
2537+
Type2 {}
2538+
} # The type of f2 is (Type1) -> Type2
25282539
```
25292540

25302541
## 60. How to convert a list of lists to a single list in KCL?

0 commit comments

Comments
 (0)