What is the purpose of null?

后端 未结 25 2339
别那么骄傲
别那么骄傲 2020-12-07 22:24

I am in a compilers class and we are tasked with creating our own language, from scratch. Currently our dilemma is whether to include a \'null\' type or not. What purpose do

相关标签:
25条回答
  • 2020-12-07 23:01

    In C NULL was (void*(0)), so it was a type with value(?). But that didn't work with C++ templates so C++ made NULL 0, it dropped the type and became a pure value.

    However it was found that having a specific NULL type would be better so they (the C++ committee) decided that NULL will once again become a type (in C++0x).

    Also almost every language besides C++ has NULL as a type, or an equivalent unique value not the same as 0 (it might be equal to it or not, but its not the same value).

    So now even C++ will use NULL as a type, basically closing the discussions on the matter, since now everyone (almost) will have a NULL type

    Edit: Thinking about it Haskell's maybe is another solution to NULL types, but its not as easy to grasp or implement.

    0 讨论(0)
  • 2020-12-07 23:05

    What purpose does null provide?

    I believe there are two concepts of null at work here.

    The first (null the logical indicator) is a conventional program language mechanism that provides runtime indication of a non-initialized memory reference in program logic.

    The second (null the value) is a base data value that can be used in logical expressions to detect the logical null indicator (the previous definition) and make logical decisions in program code.

    Do you have any thoughts, especially for or against null?

    While null has been the bane of many programmers and the source of many application faults over the years, the null concept has validity. If you and your team create a language that uses memory references that can be potentially misused because the reference was not initialized, you will likely need a mechanism to detect that eventuality. It is always an option to create an alternative, but null is a widely known alternative.

    Bottom line, it all depends upon the goals of your language:

    1. target programming audience
    2. robustness
    3. performance
    4. etc...

    If robustness and program correctness are high on your priority list AND you allow programmatic memory references, you will want to consider null.

    BB

    0 讨论(0)
  • 2020-12-07 23:05

    My suggestion to your team is: come up with some examples programs that need to be written in your language, and see how they would look if you left out null, versus if you included it.

    0 讨论(0)
  • 2020-12-07 23:06

    Oh no, I feel the philosophy major coming out of me....

    The notion of NULL comes from the notion of the empty set in set theory. Nearly everyone agrees that the empty set is not equal to zero. Mathematicians and philosophers have been battling about the value of set theory for decades.

    In programming languages, I think it is very helpful to understand object references that do not refer to anything in memory. Google about set theory and you will see similarities between the formal symbolic systems (notation) that set theorists use and symbols we use in many computer languages.

    Regards, Sam

    0 讨论(0)
  • 2020-12-07 23:08

    Null is to objects what 0 is to numbers.

    0 讨论(0)
  • 2020-12-07 23:09

    You can think of any type as a set along with a collection of operations. There are many cases where it's convenient to have a value with isn't a "normal" value; for example, consider an "EOF" value. for C's getline(). You can handle that in one of several ways: you can have a NULL value outside the set, you can distinguish a particular value as null (in C, ((void *)0) can serve that purpose) or you can have a way of creating a new type, so that for type T, you create a type T' =def { T ∪ NULL }, which is the way Haskell does it (a "Maybe" type).

    Which one is better is good for lots of enjoyable argument.

    0 讨论(0)
提交回复
热议问题