I am interested in how nil used in various programming languages, and in general how convenient it is from the point of view of language design.

This topic discusses exclusively dynamically typed languages.

For example, in JavaScript there are two values ​​similar to nil : null and undefined . (Was it necessary to produce?)

The Scheme has #f , but no nil . In Factor , there is no nil either, and they use f . (Does nil need to be false ?)

In addition, I somehow read that in the first versions of Objective-C, there was a nil object (Objective-C I don’t know at all), which absorbed calls, was a black hole. Those. behaved like NaN for numbers. On any message, the object returned nil .

How permissible is it to treat "empty" objects (empty list, empty dictionary, etc.) as false , and whether such a feature has dangerous side effects, or is it at all a location for any bugs.

Welcome:

  • Links to articles with an overview of various approaches
  • Links to any interesting languages ​​related to the topic.
  • Discussion of various approaches, convenience and hazards of use
  • one
    This question is related to hashcode.ru/questions/10778 but different - Vladimir Gordeev
  • one
    <i> As far as permissible to treat "empty" objects ... as false </ i> <br/> As far as the rare nonsense is relevant - cy6erGn0m
  • Well, generally yes, stupid. I just remembered the lisp (if () 'true 'false) => 'false , also a case to consider. - Vladimir Gordeev
  • Well, that is, there are cases when it is convenient. But usually it is dangerous. In the same JavaScript, how many times I came across all sorts of unexpected "coincidences", and even in different browsers .. beauty is just these smart interpreters :) - cy6erGn0m
  • one
    @Vladimir VG, you still look at Scala - you will be horrified) Null, null, Nil, Nothing, None peacefully coexist in it. And all these are different things. - Nofate

3 answers 3

NULL is needed where there are links. Why is there still undefined in JavaScript? Because null and undefined are different objects. By default, the link points to undefined, and null - an element of logic. Now the link is null, then not null. Let's just say, in C / C ++, pointers are also not null initially, but undefined.

Empty objects are not false, because false is a semantic value. The element of logical expressions, but null is not (although it is used in them, it is transformed).

In short, the presence of an empty object is mandatory for languages ​​with links, and false is a separate object, the same as the string "ABC" or 4 - i.e. non-empty, but semantically meaningful.

  • And if you exclude null and instead use false for the same purpose. No confusion will arise. - Vladimir Gordeev pm
  • Or vice versa, remove false and use null for all !true values - Vladimir Gordeev
  • It is possible and so, but such a collision may occur (in pseudo-language): var p = false; var q = p; // ... if (q) {/ * what does this mean? * /} - andruxa
  • In addition, if we talk about lists. There is an empty null set, and there is a set from the empty set [null] and these are different sets. Like ordinal numbers. ru.wikipedia.org/wiki/ Ordinal_number - andruxa

Although the author asks a question within the meaning of nil/null with respect to dynamic languages, but I believe that the meaning of NULL is most clearly revealed in SQL, namely in the case of related queries: the notorious inner / left / right join). Without the concept of NULL (that is, no data), all SQL loses much, if not more, it becomes impossible to work without NULL .

    I believe that the use of NULL is important in the process of debugging a program (script). false is still the normal value of a variable, but NULL is the absence of any value, or rather it is NULL. Reversal to a NULL variable can cause certain exceptions that will always cause a program to fail, and this is what makes it easy to find and fix these errors. I always use NULL, both when returning values ​​from functions, and when passing values ​​to a function. Checking is not easy! Variable, namely ==! = NULL ... In my opinion it is so more beautiful)) Good luck in your search for answers!

    • Plus, complement, very convenient to use in SQL. For example, the field "date of completion" of something. Either NULL - not executed, or a specific date of execution. At the same time and the flag and value. - KiTE