Forum > Suggestions

Custom type for Nil

(1/10) > >>

Warfley:
Nil is a special value in Pascal, while it is commonly referred to as a pointer like in the wiki article: https://wiki.freepascal.org/Nil

--- Quote ---The reserved word nil represents the special value of a pointer variable not pointing anywhere in particular.  In FPC it is implemented as pointer(0)
--- End quote ---
It is actually much more than that:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  sl := nil; // Works  sl := pointer(0); // Compiler errorSo there is some compilermagic at work, to make nil compatible with different types, to which regular pointers are not compatible.

So I was thinking, why could that not be opend to the programmers to also use, by making nil it's own type, e.g. TNil. This type would than through compilermagic, be implicetly convertable to all the use-cases of nil (which already is using compiler magic to be compatible in these cases),  but would allow to use nil for custom types through strong typechecking:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  TNullable<T> = record // see unit nullable    ...    class operator :=(None: TNil): TNullable<T>;  end; class operator TNullable<T>.:=(None: TNil): TNullable<T>;begin  Result.Clear;end; var  opt: TNullable<Integer>;begin  if Condition then    opt := 42  else    opt := Nil;end;This would allow to use nil in more situations where it could be useful, without introducing any runtime overhead (such as accepting pointers, but having to do a runtime check if that pointer is actually nil).

This should also not break any existing code, as nil does currently not have a specific type anyway, and can be used as pointer, class reference, array. It would just offload the compilermagic that allows for that to a specialized type

MarkMLl:
Irrespective of considerations of elegance and symmetry, of far more immediate use would be some way of saying "this var parameter is nil" since it would eliminate a very large number of cases where a C-style  *char  etc. has to be preserved as a  PByte  when translated to Pascal at the expense of comprehensive error checking.

MarkMLl

Remy Lebeau:

--- Quote from: Warfley on March 11, 2022, 02:09:07 pm ---So I was thinking, why could that not be opend to the programmers to also use, by making nil it's own type, e.g. TNil. This type would than through compilermagic, be implicetly convertable to all the use-cases of nil (which already is using compiler magic to be compatible in these cases),  but would allow to use nil for custom types through strong typechecking

--- End quote ---

This is exactly what C++ did when C++11 introduced the nullptr constant (type nullptr_t) into the language, thus allowing users to overload constructors, assignment operators, etc to accept nullptr as input separate from other types, while allowing nullptr to be implicitly convertible to all pointer types, etc.

ASerge:

--- Quote from: Warfley on March 11, 2022, 02:09:07 pm ---
--- Quote ---The reserved word nil represents the special value of a pointer variable not pointing anywhere in particular.  In FPC it is implemented as pointer(0)
--- End quote ---
It is actually much more than that:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  sl := nil; // Works  sl := pointer(0); // Compiler error
--- End quote ---
Full code please. I want to see the error.

Warfley:
Ah sorry, sl is of type TStringList:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var  sl: TStringList;begin  sl := nil;  sl := Pointer(0);end;

Navigation

[0] Message Index

[#] Next page

Go to full version