- Learn Kotlin Programming(Second Edition)
- Stephen Samuel Stefan Bocutiu
- 153字
- 2021-06-24 14:13:31
Limitations
The Contracts API is a very interesting and useful functionality. However, keep in mind the Contracts API syntax is experimental and could be subject to change (at the time of writing these lines, Kotlin 1.3.10 is out).
The current implementation has a few limitations:
- Contracts can only be used with top-level functions. This means they cannot be used on fields and functions defined by a class.
- The function needs to start with the contract code.
- Describing a contract is limited to the function parameters but not its nested fields. If the Command class used earlier had a user field, we couldn't have placed an effect on command.user.
With great power comes great responsibility. In this case, it means the developer is solely responsible for the contract block. The user needs to make sure the contract block sets the effect correctly. At the moment, there is no way to validate the contract block.
推薦閱讀