What is the purpose of null?

后端 未结 25 2342
别那么骄傲
别那么骄傲 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:09

    If you are creating a statically typed language, I imagine that null could add a good deal of complexity to your compiler.

    If you are creating a dynamically typed language, NULL can come in quite handy, as it is just another "type" without any variations.

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

    the concept of null is not strictly necessary in exactly the same sense that the concept of zero is not strictly necessary.

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

    Null is only useful in situations where there are variables with unassigned values. If every variable has a value, then there is no need for null values.

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

    Use a null object pattern!

    If you language is object oriented, let it have an UndefinedValue class of which only one singleton instance exists. Then use this instance wherever null is used. This has the advantage that your null will respond to messages such as #toString and #equals. You will never run into a null pointer exception as in Java. (Of course, this requires that your language is dynamically typed).

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

    I don't think it's helpful to talk about null outside the context of the whole language design. First point of confusion: is the null type empty, or does it include a single, distinguished value (often called "nil")? A completely empty type is not very useful---although C uses the empty return type void to mark a procedure that is executed only for side effect, many other languages use a singleton type (usually the empty tuple) for this purpose.

    I find that a nil value is used most effectively in dynamically typed languages. In Smalltalk it is the value used when you need a value but you don't have any information. In Lua it is used even more effectively: the nil value is the only value that cannot be a key or a value in a Lua table. In Lua, nil is also used as the value of missing parameters or results.

    Overall I would say that a nil value can be useful in a dynamically typed setting, but in a statically typed setting, a null type is useful only for talking about functions (or procedures or methods) that are executed for side effect.

    At all costs, avoid the NULL pointer used in C and Java. These are artifacts inherent in the implementations of pointers and objects, and in a well designed lanugage they should not be allowed. By all means give your users a way to extend an existing type with a null value, but make them do it explicitly, on purpose---don't force every type to have one by accident. (As an example of explicit use, I recently implemented Bentley and Sedgewick's ternary search trees in Haskell, and I needed to extend the character type with one additional value meaning 'not a character'. For this purpose Haskell provides the Maybe type.)

    Finally, if you are writing a compiler, it is good to remember that the easiest parts of the language to compile, and the parts that cause the fewest bugs, are the parts that aren't there :-)

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

    One other way to look at null is that it's a performance issue. If you have a complex object containing other complex objects and so on, then it is more efficient to allow for all properties to initially become null instead of creating some kind of empty objects that won't be good for nothing and soon to be replaced.

    That's just one perspective that I can't see mentioned before.

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