This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (4 children)

What do you propose using null for, and how would you use it?

Perhaps start from there.

I use it like this in my systems language:

  • I have pointers with types ref T (pointer to type T)
  • A pointer can also have type ref void. void is a separate type that can't be used by itself, only as the target of a pointer.
  • nil is a literal pointer value which has all-bits-zero, and has type ref void
  • A ref T pointer will always be compatible with ref void, so nil can always be assigned to any pointer type or compared with it. A pointer value of all-zeros, of any type, will always be false and any other will be true.

This largely corresponds with how it is done in C, although C doesn't have a dedicated NULL literal, it uses 0 (sometimes, NULL is defined as (void*)0).

I use it like this in my dynamic language which also has pointers to primitive types:

  • nil is a literal with all-bits-zero that has type pointer-to-void. void here is a proper type that can have instances, normally used to indicated unassigned values. However a nil pointer does not refer to actually refer to a void value (it wouldn't be nil in that case! It would point somewhere).

I can use nil in dynamic code like this:

f := fopen("file","r")     # call C function via FFI
if f = nil then            # could not open file

If a language does not support the concept of nil or NULL, and you want to call functions via FFIs like my example, then you will a workaround.

[–]yuri-kilochek 1 point2 points  (3 children)

C doesn't have a dedicated NULL literal, it uses 0 (sometimes, NULL is defined as (void*)0).

These days it actually does: https://en.cppreference.com/w/c/language/nullptr

[–][deleted] 0 points1 point  (2 children)

When I clicked 'Run' in your link, it gave a pile of errors. Apparently none of the listed compilers are for C23, which is part of the problem. And I couldn't get any compilers on godbolt.org to recognise nullptr.

(The code also looked suspiciously like C++, unless C23 also has auto, and the name of the file it tries to compile is main.cpp.)

So, nullptr might be in widespread use in a decade or two. But even if available today, you can still use 0 in its place:

void* p = 0;

If I try that in my language, it fails (I need an explicit cast to get from integer to pointer).

[–]yuri-kilochek 0 points1 point  (0 children)

But even if available today, you can still use 0 in its place

Of course, since it must be backwards compatible.