Build error with conditional defines; using attributes on local variables

Hi,

We use attributes on local variables a lot, which is not supported in .NET so we want to disable it with conditional defines, the intent is to share the code between Delphi and Oxygene.

{$IFNDEF DOTNET}
var [unsafe] localVariable := SomeInterface;
{$ENDIF}

Currently the code doesn’t compile in Oxygene and is generating the following error messages:

(E1) identifier expected, got open square bracket ([)
(E1) semicolon (; ) expected, got open square bracket ([)
(E1) assignment (:=) expected, got open square bracket ([)
(E1) semicolon (; ) expected, got identifier

We don’t want to change the location of the declaration of the variable since this changes the scope of the variable.

Is there a solution for this?
Thanks in advance.

It seems we dont define “DOTNET” (I’ll check; I thought we did).

    {$IFNDEF ELEMENTS}
    var [unsafe] localVariable := SomeInterface;
    {$ENDIF}

compiles okay and ignores the line.

That said, oddly, I only get one (clean/expected) error with the wring define, here, but maybe that depends on having a more complete test case…:

    {$IFNDEF DOTNET}
    var [unsafe] localVariable := SomeInterface; // E28 Unknown type "unsafe"
    {$ENDIF}

Ah yes, I myself forgot this tidbit from https://docs.elementscompiler.com/Compiler/ConditionalDefines/:

  • DOTNET - On .NET only, if the compiler is running on Windows on the Microsoft .NET runtime
  • MONO - On .NET only, if the compiler is running on the Mono runtime

DOTNET is only defined when you’re compiling on .NET, can I assume you’re on Mono (i.e. running the compiler on Mac ore Linux), like me?

In either case, using ELEMENTS (to distinguish vs. DELPHI) or CLR or ECHOES (to distinguish .NET vs other platforms) would be the correct define.

I run the code in .NET with windows (.net standard). Both the ELEMENTS and CLR defines also don’t work. We use the [unsafe] attribute a lot in our Delphi codebase but .NET doesn’t support attributes on local parameters, hence we want to ignore the [unsafe] attribute with conditional defines.

I made a small test project which produces the errors.
AttributesLocalParam.zip (80.3 KB)

Hmm, then my test-case wasn’t sufficient to show the actual problem — once again showing how important a complete test case os for bug reports :wink:

I’ll have a look.

See, that was the context I was missing; I declared it as a field.

Logged as bugs://E26068.

Alright, thanks!

bugs://E26068 was closed as fixed.