Lisp and Erlang Atoms, Ruby and Scheme Symbols. How useful are they?

前端 未结 13 1633
一个人的身影
一个人的身影 2020-12-12 18:08

How useful is the feature of having an atom data type in a programming language?

A few programming languages have the concept of atom or symbol to represent a consta

13条回答
  •  忘掉有多难
    2020-12-12 18:41

    The problem I have with similar concepts in other languages (eg, C) can be easily expressed as:

    #define RED 1
    #define BLUE 2
    
    #define BIG 1
    #define SMALL 2
    

    or

    enum colors { RED, BLUE  };
    enum sizes  { BIG, SMALL };
    

    Which causes problems such as:

    if (RED == BIG)
        printf("True");
    if (BLUE == 2)
        printf("True");
    

    Neither of which really make sense. Atoms solve a similar problem without the drawbacks noted above.

提交回复
热议问题