Function without Result

Hi Team,

I was facing Memory out of index issue in my Blazor WebAssembly project. To find that issue it takes my 2 weeks from where it is coming. At the end of many trials I found the very minor issue is Oxygene library have one function which don’t have result statement or just empty function.

So, It will be great help if we could add error or warning message if function does not have result statement.
Find below code for reference

interface

type

Program = class
public
class method Main(args: array of String): Int32;
class function Test(): String;
end;

Settigs = class
private
public
class function GetData: Settigs;
end;

implementation

class method Program.Main(args: array of String): Int32;
var str: String;
stg : Settigs;
begin
// add your own code here
writeLn(‘The magic happens here.’);
str := Test; // Should give error message like Delphi and .Net
stg := Settigs.GetData; // Should give error message like Delphi and .Net
Console.WriteLine("");
Console.ReadLine();
end;

class method Program.Test: String;
begin

end;

class method Settigs.GetData: Settigs;
begin

end;

Thanks,

Hi,

I understand where you’re coming from. The problem is this:

Some languages, like C#, require all variables to be explicitly initialized with a value, before you use them (they are initialized to their default, even if you don’t, but you will get warnings). Other languages assume all variables are initialized to the default (zero/nil/etc), and you can use them right away:

int I;
I = I+1; // warning in C#
var I: Integer
I := I+1; // just fine in Oxygene

in Oxygene, this includes the result variable, which is implicitly known to be initialized to its default, and as such, unlike C#, an empty method does have a correct and well-known result. And it is a very common pattern to have methods in Oxygen where not all paths (an extreme version of that being: none of them) set the result.

e.g. the below is a very common code pattern; I would expect literally hundreds, if not thousands of warnings in my own code bases, for what is perfectly valid and correct Oxygene code that would not be improved by an “else result := false”, at all:

method FindSomething: nullable Something;
begin
  if WeCouldFindTheThing then
    result := TheThingWeFound;
end;

In the RemObjects documentation: Methods (elementscompiler.com)

Method Result

If the method is defined to return a value, an implicit result Variable is available in scope, within the method body. This variable is of the same type as the method’s return type, and initialized to the default() value of the type (typically 0 or nil).

1 Like

I want to add,:one thing you an do, for reference result types, is to explicitly mark the result type as “not nullable” if you indeed always require a value. This not only will make sure you get the a warning, but is also just good practice in general, and proper and consistent use of “nullable” and “not nullable” can help you catch all sorts of problems early.

Of course that does not help in your case, if a null value is actually expected/acceptable…