By introducing a non-overloaded utility function to do the access type comparisons, the OP's function definition, with the needed syntax fixes and modified to use the utility function, can be made to work.
I'm still puzzled, though, as to why invoking "=" as Standard."=" is rejected by the compiler (GNAT) for specifying "incompatible arguments".
with Text_IO; use Text_IO;
procedure non_recursive_equals is
type Lstring is access String;
-- Be aware, the ordering of the functions here is important!
function Is_Equal(Lstring1, Lstring2 : in Lstring) return Boolean is
begin
return Lstring1 = Lstring2;
end Is_Equal;
function "=" (lString1, lString2 : in Lstring) return Boolean is
begin
if Is_Equal(LString1, null) and Is_Equal(LString2, null) then
return True;
elsif Is_Equal(LString1, null) or Is_Equal(LString2, null) then
return False;
end if;
return False;
end "=";
L1, L2 : Lstring := null;
begin
Put_Line("L1 and L2 null: " & Boolean'Image(L1 = L2));
L2 := new String(1..10);
Put_Line("L2 not null : " & Boolean'Image(L1 = L2));
end non_recursive_equals;
Edit:
Here's another way, using a renames clause:
with Text_IO; use Text_IO;
procedure non_recursive_equals is
type Lstring is access String;
function Is_Equal (lString1, lString2 : in Lstring) return Boolean is
begin
if lString1 = null and lString2 = null then
return True;
elsif lString1 = null or lString2 = null then
return False;
end if;
return False;
end Is_Equal;
function "=" (Lstring1, Lstring2 : in Lstring) return Boolean renames
Is_Equal;
L1, L2 : Lstring := null;
begin
Put_Line ("L1 and L2 null: " & Boolean'Image (L1 = L2));
L2 := new String (1 .. 10);
Put_Line ("L2 not null : " & Boolean'Image (L1 = L2));
end non_recursive_equals;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…