# IFDEF Changes explained

**URL:** https://talk.remobjects.com/t/ifdef-changes-explained/14790
**Category:** Elements
**Tags:** elements, oxygene
**Created:** [November 14, 2017, 6:50am UTC](https://talk.remobjects.com/t/ifdef-changes-explained/14790 "2017-11-14T06:50:37Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![ck](https://talk.remobjects.com/user_avatar/talk.remobjects.com/ck/32/8740_2.png) [@ck](https://talk.remobjects.com/u/ck)
#### Post date: [November 14, 2017, 6:50am UTC](https://talk.remobjects.com/t/ifdef-changes-explained/14790/1 "2017-11-14T06:50:37Z")

</div>

# What changed

The biggest change in the v10 compiler is how ifdefs are parsed. In the past we had a preprocessor, a tokenizer and a parser. The preprocessor would be provided with a list of defines, remove all code that was {$IFDEF}'d out and hand that to the parser. The new compiler does this completely different: It tries to parse ifdefs as if they were regular code concepts like statements. The reason we did this is to allow for a big future feature (can’t talk about that one yet), and make shared files a LOT faster, since the file only has to be parsed once, not once per platform.

# How it works

How it works very much depends on where the parser is. There’s a lot of recovery involved but the general rule is that ifdefs now have to be at the same level as their else/endif and have to encapsulate a full concept. For example, this is allowed:

```oxygene
{$IFDEF CLR}
method Test;
{$ENDIF}

```

This is not:

```oxygene
method {$IFDEF CLR}Test{$ELSE}Test2{$ENDIF};

```

To do above, it should look like:

```oxygene
{$IFDEF CLR}
method Test;
{$ELSE}
method Test2;
{$ENDIF}

```

Ifdefs are allowed around and inside types, but if started before a type, they have to end after it, not inside:  
**Not allowed** :

```oxygene
{$IFDEF TEST}
type ABC = class 
  method Test;
{$ENDIF}
end;

```

**Allowed** :

```oxygene
{$IFDEF TEST}
type ABC = class 
  method Test;
end;
{$ENDIF}

```

and

```oxygene
type ABC = class 
{$IFDEF TEST}
  method Test;
{$ENDIF}
end;

```

## Types

In any place that accepts a type references ifdef is allowed too, to create a type that refers to a different one:

```oxygene
var x: {$IFDEF CLR}System.String{$ELSEIF JAVA}java.lang.String{$ENDIF};

```

This becomes a System.String or java.lang.String for CLR, Java and an “Error Type” on any other platform (Error types fail when they are seen by the compiler).

## Statements

For statements the same rules as above apply, ifdefs have to contain a balanced statement.  
**Not allowed** :

```oxygene
begin
  if a = 15 then begin {$IFDEF TEST}
    Step1;
  end else begin {$ENDIF}
     Step2;
  end;

```

**Allowed** :

```oxygene
begin
  {$IFDEF TEST2}
  if a = 15 then begin 
    {$IFDEF TEST}
    Step1;
    {$ENDIF}
  end else begin 
     Step2;
  end;
  {$ENDIF}

```

## Expressions

Ifdefs in expressions are only allowed at the start of any expression like:

```oxygene
MyMethod.DoCall({$IFDEF TEST}a{$ELSE}b{$ENDIF}.Name);

```

## Attributes

Attributes can be ifdefd out in any way, as long as the ifdef captures the whole attribute and not part of it.

## Methods

One thing that compiler now enforces (which I think will aid big time in cross platform development) is that the interface and implementation of a method has to be consistent in it’s ifdefs. The compiler will error if the ifdef expression doesn’t match:

```auto
{$IFDEF A}
type
  MyClass = public class
  public 
    {$IFDEF B}
    method Test;
    {$ENDIF}
  end;
{$ENDIF}
...
{$IFDEF A and B}
method MyClass.Test;
begin
end;
{$ENDIF}

```

Here the ifdef around Test has to be “A and B” to match the combined ifdef of the type above.

Ifdefs around method modifiers are allowed (like {$IFDEF TEST}virtual;{$ENDIF}), but not for **empty** as that would conditionally break the link between the interface and implementation. Additionally the compiler enforces that the signature of a method in the interfaces matches the implementation exactly, not just logically.

## Uses

For **uses** we have logic that parses pretty much anything you can throw at it.

## Other cases

We’ve added recovery and lookahead/back code for a lot of special cases that we hit in our own code but did have to do a few smaller changes. We’d like to hear about bugs you find so we can see if they’re fixable in the new compiler.

---

<div class="post-metadata">

### Author: ![ck](https://talk.remobjects.com/user_avatar/talk.remobjects.com/ck/32/8740_2.png) [@ck](https://talk.remobjects.com/u/ck)
#### Post date: [November 14, 2017, 7:34am UTC](https://talk.remobjects.com/t/ifdef-changes-explained/14790/2 "2017-11-14T07:34:48Z")

</div>



---

<div class="post-metadata">

### Author: ![FritzW](https://talk.remobjects.com/user_avatar/talk.remobjects.com/fritzw/32/6428_2.png) [@FritzW](https://talk.remobjects.com/u/FritzW)
#### Post date: [November 14, 2017, 7:49am UTC](https://talk.remobjects.com/t/ifdef-changes-explained/14790/3 "2017-11-14T07:49:19Z")

</div>

Thanks a lot for these Post,  
it makes the new rules more clear (at least for me 🙂 )

---

<div class="post-metadata">

### Author: ![DonaldShimoda](https://talk.remobjects.com/user_avatar/talk.remobjects.com/donaldshimoda/32/6_2.png) [@DonaldShimoda](https://talk.remobjects.com/u/DonaldShimoda)
#### Post date: [December 16, 2017, 3:36pm UTC](https://talk.remobjects.com/t/ifdef-changes-explained/14790/4 "2017-12-16T15:36:04Z")

</div>

Hi,  
Im migrating code compiling ok with Fire 9.3. Is a little project sharing code between android and IOS. Have 262 errors when try to build on element.

Can plase tell me what is wrong with this code?

 ![38](https://talk.remobjects.com/uploads/default/original/2X/8/857727921ac8a13bd2ceac380dddf5b9b77fd836.png)

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [December 16, 2017, 3:43pm UTC](https://talk.remobjects.com/t/ifdef-changes-explained/14790/5 "2017-12-16T15:43:54Z")

</div>

You can not ifdef just the header of a method. You will need to either ifdef the entire method, or (better) restructure it to have the same signature on all platforms (possibly using a dummy/alias type.

For example, ou could define `Activity = Object` for non-Java, and either pass a (dummy) value, or add a second overload w/o parameter that called the other one.

eg:

```auto
{$IF NOT JAVA}
type Activity = Object
{$ENDIF}

{$IF NOT JAVA}
method doCheckLogin;
begin
  docheckLogin(nil);
end;
{$ENDIF}

method doCheckLogin(aActivity: Activity);
...

```

in general, id recommend rethinking your cross-platform design/abstraction, if you need to rely on a lot of places where you need to pass extra parameters on Android only; it might make sense to instead “register” the Activity on a. property once, but then have the same methods you can call from either platform, w/o passing it explicitly…

---

<div class="post-metadata">

### Author: ![DonaldShimoda](https://talk.remobjects.com/user_avatar/talk.remobjects.com/donaldshimoda/32/6_2.png) [@DonaldShimoda](https://talk.remobjects.com/u/DonaldShimoda)
#### Post date: [December 16, 2017, 3:51pm UTC](https://talk.remobjects.com/t/ifdef-changes-explained/14790/6 "2017-12-16T15:51:52Z")

</div>

> [@mh](#):
>
> {$IF NOT JAVA}  
> type Activity = Object  
> {$ENDIF}

Excelent!

Another problem

 ![19](https://talk.remobjects.com/uploads/default/original/2X/8/829de84ad4767bb258961b9b2f903e607421052e.png)

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [December 16, 2017, 4:21pm UTC](https://talk.remobjects.com/t/ifdef-changes-explained/14790/7 "2017-12-16T16:21:01Z")

</div>

Yep. as Carlo explained, that’s no longer supported. IFDEFs must wrap full code structures. easiest fix is to use defined instead.

```auto
if defined("IOS") and (e.name = ...) then
  fData ...
else
  fData ...

```

(ps. if you post code instead of screenshots, that makes it a dozen times easier to help you by posting revised versions. you can use Cmd-Option-C to copy code PLUS error messages.)

---

<div class="post-metadata">

### Author: ![arturredzko](https://talk.remobjects.com/user_avatar/talk.remobjects.com/arturredzko/32/16908_2.png) [@arturredzko](https://talk.remobjects.com/u/arturredzko)
#### Post date: [December 16, 2017, 9:31pm UTC](https://talk.remobjects.com/t/ifdef-changes-explained/14790/8 "2017-12-16T21:31:33Z")

</div>

I attach test project to demonstrate few our problems :  
[ClassLibrary2.zip](https://talk.remobjects.com/uploads/default/original/2X/6/6265d88d42f0d74a2f83af68ba4ec6ed8b33b9e1.zip) (14.3 KB)

1. $INCLUDE support is not working as in v9 because of :

- internal parse errors (Delphi formatting that was partially fixed)
- .inc files that are added to a project cannot be found because they are in different directories so we must add relative paths in define or copy .inc files
- if the .inc file is parsed somehow, the compiler is expecting other code than uses section (like in our sample)

1. initialization and finalization unit sections are not ignored anymore for other platforms than Delphi.
2. For many common classes we have Delphi destructor like :  
`{$IFNDEF MANAGED} destructor Destroy ; override ; {$ENDIF}`  
that compiler reports as “(E1) semicolon (😉 expected, got identifier.”
3. adding _uses_ section in implementation part of unit - compiler ignores define and tries to parse Delphi units in .NET :  
`{$IFDEF DCC} uses System.SysUtils ; {$ENDIF} `  
raising (E26) Unknown namespace “System.SysUtils” in uses list.

We have huge libraries sharing code for Delphi, .NET and Java projects that compile perfect in v9, but not in v10. This article explained most issues that we have and must fix. I will report more problems after we isolate them as test cases.

---

<div class="post-metadata">

### Author: ![DonaldShimoda](https://talk.remobjects.com/user_avatar/talk.remobjects.com/donaldshimoda/32/6_2.png) [@DonaldShimoda](https://talk.remobjects.com/u/DonaldShimoda)
#### Post date: [December 16, 2017, 9:35pm UTC](https://talk.remobjects.com/t/ifdef-changes-explained/14790/9 "2017-12-16T21:35:50Z")

</div>

> [@mh](#):
>
> (ps. if you post code instead of screenshots, that makes it a dozen times easier to help you by posting revised versions. you can use Cmd-Option-C to copy code PLUS error messages.)

Agree

The problem with some piece of code is incompatible with another platform, as this:

```
  {$IFDEF JAVA}
  if aBriefCase.TableNames.length > 0 then
  {$ELSE}
  if aBriefCase.tableNames.count > 0 then
  {$ENDIF}

```

How to solve?

---

<div class="post-metadata">

### Author: ![DonaldShimoda](https://talk.remobjects.com/user_avatar/talk.remobjects.com/donaldshimoda/32/6_2.png) [@DonaldShimoda](https://talk.remobjects.com/u/DonaldShimoda)
#### Post date: [December 16, 2017, 9:43pm UTC](https://talk.remobjects.com/t/ifdef-changes-explained/14790/10 "2017-12-16T21:43:47Z")

</div>

Another problem, how to standarize signature for a class, if inhertes from classes specifics for a platform?

```
  {$IFDEF JAVA}
  DataAccess = public class(android.app.Application, SharedPreferences.OnSharedPreferenceChangeListener)
  {$ELSE}
  DataAccess = public class(IDARemoteDataAdapterDelegate)
  {$ENDIF}
  private
...
  end;
```

---

<div class="post-metadata">

### Author: ![DonaldShimoda](https://talk.remobjects.com/user_avatar/talk.remobjects.com/donaldshimoda/32/6_2.png) [@DonaldShimoda](https://talk.remobjects.com/u/DonaldShimoda)
#### Post date: [December 16, 2017, 10:02pm UTC](https://talk.remobjects.com/t/ifdef-changes-explained/14790/11 "2017-12-16T22:02:10Z")

</div>

Another situation. Seems like the moethod to be overrided need to use the same parameters name?

This was valid in Elements 9.3

```
method onMenuItemSelected(aFeatureId: Integer; anItem: MenuItem): Boolean; override;

```

Compiler complains now.

Must use the same parameters names. This is valid.

```
method onMenuItemSelected(featureId: Integer; item: MenuItem): Boolean; override;

```

Please confirm.

Edited: Seems like ever if use the same variable name compilers complain

```
method onCreateOptionsMenu(menu: Menu): Boolean; override; // E385 The method "method onCreateOptionsMenu(Menu): nullable Boolean" does not match that found in the base class ("method onCreateOptionsMenu(Menu): RemObjects.Oxygene.System.Boolean")
                                                           // H17 Return value does not match, is "nullable Boolean, should be "RemObjects.Oxygene.System.Boolean"

```

Ok dont show that error details until i copied… so after POST i see the solution was declare in this way.

```
method onCreateOptionsMenu(menu: Menu): RemObjects.Oxygene.System.Boolean; override;

```

Why cant use default boolean? theres a mistake in the uses order?

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [December 17, 2017, 12:55pm UTC](https://talk.remobjects.com/t/ifdef-changes-explained/14790/12 "2017-12-17T12:55:22Z")

</div>

> [@DonaldShimoda](#):
>
> The problem with some piece of code is incompatible with another platform, as this:  
> How to solve?

Easy, and in fact this gets cleaner:

```auto
if (defined("JAVA") and (aBriefCase.TableNames.length > 0)) or (not defined("JAVA") and (aBriefCase.tableNames.count > 0)) then
...

```

> [@DonaldShimoda](#):
>
> Another problem, how to standarize signature for a class, if inhertes from classes specifics for a platform?

Best way would be to define an alias, for example:

```auto
type
  {$IFDEF JAVA}
  DataAccessBase = android.app.Application;
  {$ELSE}
  DataAccessBase = Object;
  {$ENDIF}

  DataAccess = public class(DataAccessBase)
  {$ENDIF}
  private
    ...
  end;

```

> [@DonaldShimoda](#):
>
> method onCreateOptionsMenu(menu: Menu): Boolean; override; // E385 The method “method onCreateOptionsMenu(Menu): nullable Boolean” does not match that found in the base class (“method onCreateOptionsMenu(Menu): RemObjects.Oxygene.System.Boolean”)  
> // H17 Return value does not match, is "nullable Boolean, should be “RemObjects.Oxygene.System.Boolean”

that sounds like a bug, the compiler thinking the return is a **nullable** Boolean. I’d need a complete test case that shows this to comment more.

---

<div class="post-metadata">

### Author: ![RemObjectsSoftware](https://talk.remobjects.com/user_avatar/talk.remobjects.com/remobjectssoftware/32/15947_2.png) [@RemObjectsSoftware](https://talk.remobjects.com/u/RemObjectsSoftware)
#### Post date: [December 17, 2017, 12:55pm UTC](https://talk.remobjects.com/t/ifdef-changes-explained/14790/13 "2017-12-17T12:55:49Z")

</div>

Thanks, logged as bugs://79236 \<\< for Artur’s issues

---

<div class="post-metadata">

### Author: ![deltics](https://talk.remobjects.com/user_avatar/talk.remobjects.com/deltics/32/804_2.png) [@deltics](https://talk.remobjects.com/u/deltics)
#### Post date: [December 17, 2017, 6:42pm UTC](https://talk.remobjects.com/t/ifdef-changes-explained/14790/14 "2017-12-17T18:42:24Z")

</div>

> [@mh](#):
>
> if (defined(“JAVA”) and (aBriefCase.TableNames.length \> 0)) or (not defined(“JAVA”) and (aBriefCase.tableNames.count \> 0)) then  
> …

I haven’t yet gotten around to even installing v10, but wondering if this would work:

```
var numTableNames = if defined("JAVA") then aBriefCase.TableNames.length 
               else if not defined("JAVA") then aBriefCase.tableNames.count
               else 0;

```

Also, does this new syntax still result in code not applicable to the "current’ platform being rendered as a comment vs “active” code ? (I fervently hope so)

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [December 17, 2017, 9:29pm UTC](https://talk.remobjects.com/t/ifdef-changes-explained/14790/15 "2017-12-17T21:29:10Z")

</div>

yes, this will work. the code eliminated by `defined()` (or `$IFDEF`) needs to be _syntactically_ correct, but its identifiers don’t need to exist. e.g. you can do

```auto
if defined(“TOFFEE”) and UIDevice.currentDevice.paradigm = iPad then...
  // runs only for TOFFEE

```

and it will compile ok for ECHOES, simply skipping the whole block, even though Echoes does not know UIDevice. Boolean short-circuit rules will apply, e.g. unknown stuff can be ANDed with an undefined define, and ORed with a defined define, eg:

```auto
if defined(“TOFFEE”) or Environment.OS = macOS then... 
 // runs conditionally for ECHOES and unconditionally for TOFFEE. eg ECHOES will check Environment, while TOFFEE will skip the check.

```

What you can NOT do is have **syntacticly invalid** code in an $IFDEF _or_ anundefined define, eg:

```auto
{$IFDEF FOOBAR}
for each to bar while begin // will fail even if FOOBAR isn’t defined. 
{$ENDIF}

```

hth.

---

<div class="post-metadata">

### Author: ![joseasl](https://talk.remobjects.com/user_avatar/talk.remobjects.com/joseasl/32/8050_2.png) [@joseasl](https://talk.remobjects.com/u/joseasl)
#### Post date: [December 18, 2017, 10:46am UTC](https://talk.remobjects.com/t/ifdef-changes-explained/14790/16 "2017-12-18T10:46:33Z")

</div>

What about this one from Sugar.Data (by the way, when is it gonna jump to RTL2?)

```
  SQLiteConnection = public {$IFDEF PUREJAVA}interface{$ELSE}class{$ENDIF} {$IFDEF ANDROID}mapped to android.database.sqlite.SQLiteDatabase{$ENDIF}
  {$IFDEF PUREJAVA}mapped to java.sql.Connection{$ENDIF}{$IFDEF ECHOES}(IDisposable){$ENDIF}

```

Sometimes it is an interface, sometimes a class.

---

<div class="post-metadata">

### Author: ![arturredzko](https://talk.remobjects.com/user_avatar/talk.remobjects.com/arturredzko/32/16908_2.png) [@arturredzko](https://talk.remobjects.com/u/arturredzko)
#### Post date: [December 18, 2017, 12:02pm UTC](https://talk.remobjects.com/t/ifdef-changes-explained/14790/17 "2017-12-18T12:02:51Z")

</div>

I have similar issue but with records and classes :

`TMyPoint = public {$IFDEF JAVA}class{$ELSE}record{$ENDIF}`

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [December 18, 2017, 1:12pm UTC](https://talk.remobjects.com/t/ifdef-changes-explained/14790/18 "2017-12-18T13:12:06Z")

</div>

> [@joseasl](#):
>
> Sometimes it is an interface, sometimes a class.

You’d have to condition the whole thing.

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [December 18, 2017, 1:12pm UTC](https://talk.remobjects.com/t/ifdef-changes-explained/14790/19 "2017-12-18T13:12:30Z")

</div>

> [@arturredzko](#):
>
> I have similar issue but with records and classes :
> 
> TMyPoint = public {$IFDEF JAVA}class{$ELSE}record{$ENDIF}

ok, but _why_?

---

<div class="post-metadata">

### Author: ![arturredzko](https://talk.remobjects.com/user_avatar/talk.remobjects.com/arturredzko/32/16908_2.png) [@arturredzko](https://talk.remobjects.com/u/arturredzko)
#### Post date: [December 18, 2017, 1:57pm UTC](https://talk.remobjects.com/t/ifdef-changes-explained/14790/20 "2017-12-18T13:57:30Z")

</div>

In VCL and .NET we have records (memory management related requirements) but in Java records are expensive because every record’s property change does cloning. So we use class type to speed up things.

[Next page](https://talk.remobjects.com/t/ifdef-changes-explained/14790.md?page=2)
