File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 88
99## Inference
1010
11- Use type inference as much as possible. You should almost never annotate
12- the type of a ` val ` field as their type will be immediately evident in
13- their value:
11+ Use type inference where possible, but put clarity first, and favour
12+ explicitness in public APIs.
1413
15- val name = "Daniel"
14+ You should almost never annotate the type of a private field or a local
15+ variable, as their type will usually be immediately evident in
16+ their value:
1617
17- However, type inference has a way of coming back to haunt you when used
18- on non-trivial methods which are part of the public interface. Just for
19- the sake of safety, you should annotate all public methods in your
20- class.
18+ private val name = "Daniel"
19+
20+ However, you may wish to still display the type where the assigned value has a
21+ complex or non-obvious form.
22+
23+ All public methods should have explicit type annotations. Type inference may
24+ break encapsulation in these cases, because it depends on internal method
25+ and class details. Without an explicit type, a change to the internals
26+ of a method or val could alter the public API of the class without warning,
27+ potentially breaking client code. Explicit type annotations can also help
28+ to improve compile times.
2129
2230### Function Values
2331
You can’t perform that action at this time.
0 commit comments