Recent

Author Topic: What is the correct syntax for using 'deprecated' on a global variable?  (Read 5539 times)

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1565
    • Lebeau Software
In Indy, there are two global variables that are marked as deprecated:

IdGlobal.pas:

Code: [Select]
var
  GOffsetFromUTC: TDateTime = 0{$IFDEF HAS_DEPRECATED} deprecated{$ENDIF};

IdWship6.pas:

Code: [Select]
var
  GIdIPv6FuncsAvailable: Boolean = False{$IFDEF HAS_DEPRECATED} deprecated{$ENDIF};

I've seen several reports on this forum that this syntax is causing errors in FreePascal, such as:

http://forum.lazarus.freepascal.org/index.php/topic,26946.msg167175.html

http://forum.lazarus.freepascal.org/index.php/topic,27143.msg167976.html

http://forum.lazarus.freepascal.org/index.php/topic,31803.msg204201.html

It works fine in Delphi (but not in FreePascal in Delphi mode).

I've seen only *one* comment explaining this error, in the following post:

http://forum.lazarus.freepascal.org/index.php/topic,31803.msg204201.html#msg205541

Code: Pascal  [Select][+][-]
  1. Here is the line showing the rror:
  2. GOffsetFromUTC: TDateTime = 0 {$IFDEF HAS_DEPRECATED} deprecated{ENDIF};

If this is the exact line, a semi-colon is missing before deprecated. It is normal that the compiler makes an error when the directive HAS_DEPRECATED is set ...

This seems contradictory to FreePascal's documentation, and is not required in Delphi.

I want the variables to cause a compiler warning.  So I was thinking of changing the declarations to this:

Code: [Select]
var
  GOffsetFromUTC: TDateTime = 0{$IFDEF HAS_DEPRECATED}{$IFDEF FPC};{$ENDIF} deprecated{$ENDIF};

Code: [Select]
var
  GIdIPv6FuncsAvailable: Boolean = False{$IFDEF HAS_DEPRECATED}{$IFDEF FPC};{$ENDIF} deprecated{$ENDIF};

But I never got confirmation if this was correct or not.  Besides, I've seen plenty of people say they were able to install/use Indy without having to make this code change.

So, is adding the semicolon really required in FreePascal?  If so, why is this required in Delphi mode, at least, when Delphi itself does not require it?  And why aren't more Indy users running into this error?
« Last Edit: November 07, 2016, 10:40:36 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Bart

  • Hero Member
  • *****
  • Posts: 5610
    • Bart en Mariska's Webstek
Re: What is the correct syntax for using 'deprecated' on a global variable?
« Reply #1 on: November 07, 2016, 11:33:26 pm »
This compiles for me (fpc 3.0.0):

Code: [Select]
{$define has_deprecated}
Const
  AConst: integer = 12 {$ifdef has_deprecated}deprecated 'foo'{$endif};
var
  GOffsetFromUTC: TDateTime = 0{$IFDEF HAS_DEPRECATED} deprecated{$ENDIF};
  GIdIPv6FuncsAvailable: Boolean = False{$IFDEF HAS_DEPRECATED} deprecated{$ENDIF}; 

Bart

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1565
    • Lebeau Software
Re: What is the correct syntax for using 'deprecated' on a global variable?
« Reply #2 on: November 08, 2016, 08:52:03 pm »
This compiles for me (fpc 3.0.0):

What about earlier versions?  Now I wonder if I will have to IFDEF the code so the semicolon is only used in pre-3.0 versions of FreePascal?

BTW, do you know when 'deprecated' was first added, and when the message string was first supported?  I know they exist in 2.4.4+, but what about earlier versions?
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12523
  • FPC developer.
Re: What is the correct syntax for using 'deprecated' on a global variable?
« Reply #3 on: November 08, 2016, 09:25:10 pm »
This compiles for me (fpc 3.0.0):

What about earlier versions?  Now I wonder if I will have to IFDEF the code so the semicolon is only used in pre-3.0 versions of FreePascal?

I happened to have 2.6.2 and 2.6.4 installed and those worked fine too, with and without message, in mode objfpc and delphi with and without has_deprecated (and with the message showing)

Deprecated has been a very long work in progress, with first (skip only) support appearing in some 1.9.x version iirc. Messages is 2.4+ or 2.6+, but there have been bugs with some of these constructs (and this with initializer is a special case).

Testing with older versions is the only way to find out I guess.

I'm not aware of people keeping track of version details for versions older than one major release (because even the most conservative but actively supported Linux distro usually has a newer version by then)
« Last Edit: November 08, 2016, 09:43:01 pm by marcov »

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1565
    • Lebeau Software
Re: What is the correct syntax for using 'deprecated' on a global variable?
« Reply #4 on: November 08, 2016, 10:14:50 pm »
I happened to have 2.6.2 and 2.6.4 installed and those worked fine too, with and without message, in mode objfpc and delphi with and without has_deprecated (and with the message showing)

OK, thanks. I think I will do something like this:

IdCompilerDefines.inc

Code: [Select]
{$IFDEF FPC}
  ...
  {$IF DEFINED(FPC_FULLVERSION) AND (FPC_FULLVERSION >= 20404)}
    {$DEFINE FPC_2_4_4_OR_ABOVE}
  {$IFEND}
  ...
  {$IF DEFINED(FPC_FULLVERSION) AND (FPC_FULLVERSION >= 20602)}
    {$DEFINE FPC_2_6_2_OR_ABOVE}
  {$IFEND}
  ...
  {$IFDEF FPC_2_4_4_OR_ABOVE}
    ...
    {$DEFINE HAS_DEPRECATED} // TODO: when was deprecated introduced? Possibly 1.9.x
    {$DEFINE HAS_DEPRECATED_MSG} // TODO: when was message support added? Possibly 2.4.x

    // RLebeau: add this
    {$IFNDEF FPC_2_6_2_OR_ABOVE}
      {$DEFINE USE_SEMICOLON_BEFORE_DEPRECATED} // TODO: which earlier versions require a semicolon?
    {$ENDIF}
  {$ENDIF}
  ...
{$ENDIF}

IdGlobal.pas

Code: [Select]
{$I IdCompilerDefines.inc}

...

var
  GOffsetFromUTC: TDateTime = 0{$IFDEF HAS_DEPRECATED}{$IFDEF USE_SEMICOLON_BEFORE_DEPRECATED};{$ENDIF} deprecated{$ENDIF};

IdWship6.pas

Code: [Select]
{$I IdCompilerDefines.inc}

...

var
  GIdIPv6FuncsAvailable: Boolean = False{$IFDEF HAS_DEPRECATED}{$IFDEF USE_SEMICOLON_BEFORE_DEPRECATED};{$ENDIF} deprecated{$ENDIF};
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018