Comprehensive guide on common lisp types

前端 未结 3 480
甜味超标
甜味超标 2021-01-19 04:41

Maybe this question is too general, nevertheless i\'ll try: Is there any comprehensive guide on types in common lisp?

I\'m kind of confused about this subject:

3条回答
  •  星月不相逢
    2021-01-19 05:19

    How types are handled during compilation is defined by implementations. In the case of SBCL, types are generally treated as assertions, but the actual behavior depends on optimisation levels.

    Types as assertions means that if a function takes a number n and produces a string s, you generally don't assume that n is a number. Instead, what you have is a guarantee that if the function returns, then n effectively was a number and s is now a string. But if you reuse s, your compiler has an opportunity to skip a check for s being a string. This is generally what you want because your functions are available globally and can thus be called from anywhere. Since functions are responsible for checking their inputs, it is normal that you always check for n being a number first.

    Yet, type declarations for functions can help you in case you call a function in a context where it can be proved that types will certainly mismatch at runtime (the intersection of types is empty). In order to trust type assertions blindly, you have to lower the safety levels.

    Note: I originally posted it at a comment, but in order to avoid it being deleted, here is a link to a nice graphics representing relationships between types in CL:

    http://sellout.github.io/2012/03/03/common-lisp-type-hierarchy

提交回复
热议问题