Why Nullable lazarus / 61-Beginners ---------------------------------------------------------------------- Warfley · 2022-03-11 08:47 There is now the type TNullable in Pascal and I was wondering, why is it named that way? This is a really odd name because it's not a common name, most languages (such as Java, C++, Typescript, Swift, etc.) call this concept an "optional". I only know one language that calls it nullable and that is C#. And the thing about C# is, it chose the name because it allows the value to be set to the constant "null". And here is the odd thing. First, Pascal does not have a "null", it has "nil", so at least it should be called nilable. Especially as nil is much better fitting here, as Null references the numeric value of "0" for pointers. But with the Nullable we are not talking about pointers, and the value will not really be set to 0, but be not assigned, or to say it differently, the value is "not in list" of the valid values. But the weirdest thing is that it is not even nilable, because you cannot assign the value nil to it. In C# it is called nullable because you can do the following: Code: C  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   Nullable i = null;but the following does not work: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i i: TNullable;begin  i := nil; // Compiler errorSo the type is not actually nilable, but rather you must call the method Clear. I think the original nullable wiki page example at least had a cast from pointer (which would do a runtime check if it is nil, otherwise throw an error), so there it at least made kindof sense (even though the name should still have been nilable), but this was omitted here So why was it called nullable? This name does not make sense on any level. It is less common a name for the concept than "optional", null doesn't even exist in pascal, and even nil cannot assigned to nullables ---------------------------------------------------------------------- marcov · 2022-03-11 08:56 In general Delphi borrowed a lot from C# in the past years. Making detail changes to make it "nillable" seems counterproductive. Moreover, for databases and variants FPC does use the term null ( field.isnull, varnull etc) ---------------------------------------------------------------------- MarkMLl · 2022-03-11 08:59 While not disagreeing with your points, it's certainly a term I've come across in various places, and https://en.wikipedia.org/wiki/Nullable#Language_support highlights e.g. Kotlin as well. I think e.g. https://softwareengineering.stackexchange.com/questions/410724/why-f- rust-and-others-use-option-type-instead-of-nullable-types- like-c-8-or-t is also useful,since it highlights the conceptual differences. MarkMLl ---------------------------------------------------------------------- Thaddy · 2022-03-11 09:26 Nullable types are very useful in database commits. Normally one would not touch a NULL field, or better: if not necessary the NULL (undefined) state should be kept instead of actually assigning a value. A second application is with ternary logic order TRUE, NULL, FALSE. (see my posts on ternary logic) IOW in both cases a way to express an undefined value over defined values. Very useful. ---------------------------------------------------------------------- Warfley · 2022-03-11 09:30 Quote from: Thaddy on March 11, 2022, 10:26:22 amNullable types are very useful in database commits. Normally one would not touch a NULL field, or better: if not necessary the NULL (undefined) state should be kept instead of actually assigning a value. A second application is with ternary logic order TRUE, NULL, FALSE. (see my posts on ternary logic) IOW in both cases a way to express an undefined value over defined values. Very useful. Not disagreeing with any of that, I am just wondering why to name it nullable, if it is not assignable to null/nil. Semantically you can do the same with an optional type, for me that would be just more fitting with the naming. ---------------------------------------------------------------------- Thaddy · 2022-03-11 11:27 Quote from: Warfley on March 11, 2022, 10:30:06 am Semantically you can do the same with an optional type, for me that would be just more fitting with the naming. It IS an optional type, in fact, both the nullable type and the ternary type from my posts are implementations of such optional types. ---------------------------------------------------------------------- Warfley · 2022-03-11 11:40 I am not asking about the usefullness or specific use-cases for the concept, I am just asking about the name. Pascal, unlike C# has no concept of "null", it has "nil", which wile in practice is basically the same, the concept is slightly different, as "nil" stands for "not in list" and describes a value that is outside the defined range, while "null" describes a pointer with the numeric value "0", which is the only value that is by definition always an invalid pointer. So TNullable does not make any sense as a name, and should be at least TNilable, but even that is not really fitting, as the type has nothing to do with the nil keyword (as opposed to C# or Kotlin where Nullable means that you can both assign as well as compare to "null"), but the TNullable type is completely unrelated to the "nil" value. So I think the name should reflect this with a more accurate name ---------------------------------------------------------------------- MarkMLl · 2022-03-11 11:51 Quote from: Warfley on March 11, 2022, 12:40:39 pm"nil" stands for "not in list" and describes a value that is outside the defined range That's a contrived backronym and I'm afraid I'm uncomfortable with it unless you can provide a definitive reference. NIL goes back at least as far as ALGOL-68. I don't know the ALGOL-60 situation but I'd add that Wirth had significant experience on systems with hardware-enforced protection, so was used to situations where a pointer contained NIL even if it wasn't part of a linked list. He did not, OTOH, have experience with SQL which was an early-adopter NUL. MarkMLl ---------------------------------------------------------------------- Thaddy · 2022-03-11 11:59 Quote from: MarkMLl on March 11, 2022, 12:51:20 pmThat's a contrived backronym Furthermore it is not true at all, it is an abbreviation of the Latin Nihil, which means nothing or empty. So it is merely coincidence that it happen to fit "not in list". As Mark wrote: it has nothing to do with a list. https://www.merriam-webster.com/dictionary/nil#h1 is one of the reliable sources where this is explained. There are - misleading - sources, though, relating to LISP which explains it was introduced in computer science in 1958. I doubt that, though, since Babbage and Ada Lovelace also allowed for undefined values.  (The latter derived from the fact that she realized that to present any value there should be a way to represent the non-existence of a value too) ---------------------------------------------------------------------- PascalDragon · 2022-03-11 12:12 Quote from: Warfley on March 11, 2022, 09:47:59 amSo why was it called nullable? This name does not make sense on any level. It is less common a name for the concept than "optional", null doesn't even exist in pascal, and even nil cannot assigned to nullables It's named nullable, because in the Delphi sphere it's usually named nullable as well (see here, here and here). ---------------------------------------------------------------------- MarkMLl · 2022-03-11 12:22 Quote from: Thaddy on March 11, 2022, 12:59:53 pmFurthermore it is not true at all, it is an abbreviation of the Latin Nihil, which means nothing or empty. I've been looking at a couple of things I know my way around out of curiosity. There's lots of references to null records and null devices in the Burroughs large system sources (late 60s) and a minimal amount to constricting lists with nil fields... and these were systems Wirth knew fairly well. However these had hardware detection of invalid/uninitialised pointers and it's arguable whether application- level software would have ever been involved in this: in principle an unprivileged user would never have been able to create an arbitrary pointer, and he'd certainly not have been able to attempt to dereference a dud one. There's a distinct possibility that there were original differences in the use of nul and nil depending on the application area: if you were doing text processing you'd not refer to a nil field because many listeners would interpret that as zero (and irrespective of the C/C++ usage, the layman wouldn't do that with null). There's also a possibility that there's national differences: much of the ALGOL impetus was European, while both SQL and C/C++ were unapologetically American... as is Delphi. MarkMLl ---------------------------------------------------------------------- Warfley · 2022-03-11 12:26 Quote from: MarkMLl on March 11, 2022, 12:51:20 pmThat's a contrived backronym and I'm afraid I'm uncomfortable with it unless you can provide a definitive reference. NIL is a common phrase from functional programming for the empty of lists and was first introduced in Lisp. The name was chosen due to how lists are implemented in functional programming, as a series of recursive expressions, where Nil is the base expression, so you can say Nil is the -1st element of the list, or an element that is "not in list". Through the way how Lisp works, basically everything is a list (LISP stands for LISt Processor). So Nil simultaniously is the empty list, the boolean value false, the root of all (recursive) datastructures and also the "non value" for any empty data. Here is some documentation for the use of Nil in LISP: https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node9.html So while it could be that for Algol 68, 10 years after the creation of LISP, the term NIL could have been reinvented seperately, but through the massive impact LISP had at the time, I highly doubt it, especially as Wirth was pretty well versed with the differend programming languages and paradigms of the time. ---------------------------------------------------------------------- Thaddy · 2022-03-11 12:29 Quote from: MarkMLl on March 11, 2022, 01:22:14 pm as is Delphi. MarkMLl Ahum, Danish.... with a -largely, but international - American staff to help Anders out... https://en.wikipedia.org/wiki/Anders_Hejlsberg ---------------------------------------------------------------------- Warfley · 2022-03-11 12:36 Quote from: PascalDragon on March 11, 2022, 01:12:28 pmIt's named nullable, because in the Delphi sphere it's usually named nullable as well (see here, here and here). Ah good old Delphi... never fails to disappoint when it comes to language design. ---------------------------------------------------------------------- Thaddy · 2022-03-11 12:43 Why? See https://en.wikipedia.org/wiki/Null_function which explains very well that state is unchanged. In SQL it means state has no value. In nil Pascal means state has no value and has better heritage than null. All of these boil down to the same paradigm: how to express that an entity does not exist, but MAY exist at some point, or used to exist... Examples are as old as Euclid. ---------------------------------------------------------------------- AlexTP · 2022-03-11 12:47 Delphi and (similar lang) C# call it nullable, so let's stay with this name. ---------------------------------------------------------------------- dseligo · 2022-03-11 13:47 Quote from: Warfley on March 11, 2022, 09:47:59 amSo the type is not actually nilable, but rather you must call the method Clear. Maybe it should be called Clearable then. ---------------------------------------------------------------------- Thaddy · 2022-03-11 14:02 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses nullable;{$macro on}{$Define TNillable:=TNullable}{$Define TClearable:=TNullable}{$Define THasNoValuable:=TNullable}{$Define TCompilerhasNoClueable:=TNullable}{$Define TWhatDoYouMeanable:=TNullable}//etc.Problems solved ---------------------------------------------------------------------- circular · 2022-03-12 05:22 And also of course Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$Define TOptional:=TNullable} ---------------------------------------------------------------------- munair · 2022-03-12 19:09 As higher programming languages use terms from human languages anyway, both NULL and NIL could be used to e.g. differentiate between pointer 0 and numeric value 0. NULL is widely used in C and GTK to mark a 0-pointer, but there are languages who use NIL to test or indicate whether an object has been instantiated. The rules are free to implement in this respect as long as documentation is clear. ---------------------------------------------------------------------- Thaddy · 2022-03-13 20:26 Quote from: munair on March 12, 2022, 08:09:20 pm The rules are free to implement in this respect as long as documentation is clear. I agree. With one remark: What is grammatically the same is not necessarily semantically the same. This confuses programmers - who are known not to read manuals, because they know better - that use multiple languages, unless they pay attention. And not just null/nill... Closer to home: e.g. what does string mean and in which context? ---------------------------------------------------------------------- BeniBela · 2022-03-13 21:50 Quote from: Thaddy on March 11, 2022, 12:27:23 pmQuote from: Warfley on March 11, 2022, 10:30:06 am Semantically you can do the same with an optional type, for me that would be just more fitting with the naming. It IS an optional type, in fact, both the nullable type and the ternary type from my posts are implementations of such optional types. Should have called  it TOption then ---------------------------------------------------------------------- munair · 2022-03-13 22:25 Quote from: Thaddy on March 13, 2022, 09:26:59 pmCloser to home: e.g. what does string mean and in which context? Fortunately, after some 50+ years the programming world still agrees on what string means. ---------------------------------------------------------------------- BeniBela · 2022-03-14 00:52 Quote from: munair on March 13, 2022, 11:25:30 pmQuote from: Thaddy on March 13, 2022, 09:26:59 pmCloser to home: e.g. what does string mean and in which context? Fortunately, after some 50+ years the programming world still agrees on what string means.  Does it mean shortstring, ansistring, utf8string, unicodestring, or widestring? ---------------------------------------------------------------------- munair · 2022-03-14 07:03 Quote from: BeniBela on March 14, 2022, 01:52:46 amDoes it mean shortstring, ansistring, utf8string, unicodestring, or widestring? What is an integer? Is it a signed number, an unsigned number, a pointer, is it 8 bits, 16 bits, 32 bits, 64 bits, 128 bits?  These are sub-classifications of the issue. ---------------------------------------------------------------------- Warfley · 2022-03-14 09:55 Quote from: munair on March 12, 2022, 08:09:20 pmThe rules are free to implement in this respect as long as documentation is clear. There is still good and bad naming. For example in C null makes sense, because it literally is just (void*)0. In higher level languages where there is no such concept of a pointer, this does not make much sense anymore, as without the concept of pointers, the idea that a pointer has the numeric value 0 does not make sense. This is why some languages, such as python do not use the term Null but rather None. So most higher level languages stick to that naming soly because of it's historical recognisability, but semantically it does not make sense in languages like C#, Swift and co. I personally do like the term Nil much more than Null because in a higher level lanugage as a programmer you should not need to know what hardware values pointers correspond to, as the Free Pascal wiki states: QuoteThe reserved word nil represents the special value of a pointer variable not pointing anywhere in particular. In FPC it is implemented as pointer(0) (the numeric value 0), however the programmer is not supposed to use this fact.So if you are not supposed to use this fact, indicating this fact through naming is actually a bad design choice. It is common knowledge that a null-pointer, is a pointer with numeric value 0, which is reinforced through the naming. But the thing is, that does not need to be, this is just a convention that most current hardware starts addressable memory after 0, but a high level language should not be restricted to such hardware decisions. If you are going to write a program for hardware which uses 0 as a valid memory region (maybe even an important one, e.g. as location for the ISR), then either, you cannot use the language for this system, or this convention needs to be broken. Nil already makes clear through the naming that it is a special value that you do not need to know, while null indicates through naming that it is related to the numeric value 0 (even more C does bake this into the standard that (void*)0 is null, meaning on systems with valid 0 pointers, the compiler needs to do a lot of magic). But, it should be said, nil is still not the best name, as it is also is rather cryptic as what it is supposed to mean, as proven by the fact that in this thread there were differing ideas on what it actually meant, by seasoned programmers. And sensible naming is important. For example one of the arguments for pascal and against C is that C has this weird type void, which unifies the concept of no value (like a function with no return value, or a function with no parameters), as well as the concept of an unknown type (as void pointers). Sure it is well documented what void is. It is still a thing beginners regularly struggle with. Pascal solves this problem by having different names for different concepts, unnamed pointers are "pointer", functions with no return value are procedures, and functions with no arguments have simply no arguments. Naming is important, not every naming convention is equal as long as it is documented. You could name your null pointer "valid_pointer", and document it, this would still make it a terrible name and make programming in that language much harder ---------------------------------------------------------------------- Thaddy · 2022-03-14 12:23 Quote from: BeniBela on March 14, 2022, 01:52:46 amDoes it mean shortstring, ansistring, utf8string, unicodestring, or widestring? And the PChar family... Maybe it is string theory... https://en.wikipedia.org/wiki/String_theory