Why does this code compile?

namespace oxy001;

interface

type
OldStyleClass = public class
private
protected
public
method Test (anInt : Integer) : Integer;
method Bunny : String;
end;

implementation

method OldStyleClass.Test(anInt: Integer): Integer;
begin
result := 0;
end;

method OldStyleClass.Bunny: String;
begin
result := 0;
end;

end.

Elements 9.1.99.2151
VS 2017 (will not matter very likely)
.net target

I’m little confused, let’s say surprised. Why does this code compile? Talking about the Bunny method (don’t care about the names - it’s just little tinkering for test purposes).

It’s no surprise that the output with writeln returns an empty string.

No problem for me at the moment.

0 = nil

Thank you very much Marc!

I see. The moment I try to return 1 the compilation error is in place. Could have considered this on my own.

Is this really an Oxygene thing?
It seems more C or JavaScript!

What happens if the result of the method is nullable Int32?

With this I have to give u right!

It doesnt seem very type-safe :frowning:

i’ll let Carlo explain.

1 Like

In C++ you survive too but get a runtime error. Imo it’s ok from this perspective.

NULL in case of a pointer or a reference to an object is ok.

I hope we don’t waste Carlo’s time.

Yikes.

Whilst I have no issue with the truth of a statement 0 = Integer(nil), it is (imho) only the presence of the cast that should make this true (or at least acceptably true to the compiler).

the big problem here is that we import the C headers for Toffee and Island, and in C 0 is used for null (#define NULL 0), so the compiler has to understand that when it encounters an integer 0 constant, it can translate it to nil, (this obviously only works for 0). it’s a bit of an edge case and I want to make it work for external constants only, but it’s one of those things that just has to work for pretty much all C headers to work.

1 Like

Thanks, logged as bugs://78159

Ok, this explains all actually and it sounds even understandable, when you want to include C-Headers^^

bugs://78159 got closed with status fixed.