Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2013-11

optionally uses a unit

5 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Xiangrong Fang

Hi All,

Is it possible to optionally use a unit? e.g.

unit myunit;
uses {$ifexists persist.pas}persist,{$endif} Classes;
type
  TMyClass = class
  public
    {$ifexists persist.pas}
    procedure SaveToStream(s: TStream);
    {$endif}
  end;

Thanks,
Xiangrong

Reply by email

Dmitry Boyarintsev
to Xiangrong Fang

Looks horrible. I thought that's why class-helpers were introduced? But I'd go with a function.

thanks,
Dmitry

Xiangrong Fang wrote 22 lines
Hi All, Is it possible to optionally use a unit? e.g. unit myunit; uses {$ifexists persist.pas}persist,{$endif} Classes; type TMyClass = class public {$ifexists persist.pas} procedure SaveToStream(s: TStream); {$endif} end; Thanks, Xiangrong _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Tomas Hajny
to Xiangrong Fang: Is it possible to optionally use a unit? e.g. unit myunit; uses {$ifexists…

On Mon, November 18, 2013 07:38, Xiangrong Fang wrote:

Hi,

Show quoted text 12 lines
Is it possible to optionally use a unit? e.g. unit myunit; uses {$ifexists persist.pas}persist,{$endif} Classes; type TMyClass = class public {$ifexists persist.pas} procedure SaveToStream(s: TStream); {$endif} end;

Isn't {$IFDEF PERSIST}... combined with calling the compiler with/without -dPERSIST depending on availability of the source sufficient (and more general)? Obviously, you can test availability of the source file in a makefile/shell script/batch file/... and have the -dPERSIST added to compiler options accordingly.

BTW, your example (and partly the whole concept) might give unexpected/unwanted results - what if a precompiled version (.ppu/.o) of that unit is available but not the source file? Which directories should be checked when searching the file (note that the compiler couldn't know if the filename mentioned should belong to a unit source file, specific include file, compiled object, library or even a completely independent file, and that different search paths are used for searching files of different types)?

Tomas

Reply by email

Mark Morgan Lloyd
to Tomas Hajny
Tomas Hajny wrote 32 lines
On Mon, November 18, 2013 07:38, Xiangrong Fang wrote: Hi, > Is it possible to optionally use a unit? e.g. > > unit myunit; > uses {$ifexists persist.pas}persist,{$endif} Classes; > > type > TMyClass = class > public > {$ifexists persist.pas} > procedure SaveToStream(s: TStream); > {$endif} > end; Isn't {$IFDEF PERSIST}... combined with calling the compiler with/without -dPERSIST depending on availability of the source sufficient (and more general)? Obviously, you can test availability of the source file in a makefile/shell script/batch file/... and have the -dPERSIST added to compiler options accordingly. BTW, your example (and partly the whole concept) might give unexpected/unwanted results - what if a precompiled version (.ppu/.o) of that unit is available but not the source file? Which directories should be checked when searching the file (note that the compiler couldn't know if the filename mentioned should belong to a unit source file, specific include file, compiled object, library or even a completely independent file, and that different search paths are used for searching files of different types)?

There are definitely hazards, but I think that an exists() predicate of some form would definitely be useful. For example, inside an {$ifdef persist} it would give the developers the ability to have a tailored error message advising the maintainer that if persists.pas didn't exist he should also get such-and-such a project from local svn, and set up appropriate symlinks.

The major issue would be directories and search paths.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Xiangrong Fang
to Mark Morgan Lloyd

2013/11/18 Mark Morgan Lloyd

Show quoted text 8 lines
There are definitely hazards, but I think that an exists() predicate of some form would definitely be useful. For example, inside an {$ifdef persist} it would give the developers the ability to have a tailored error message advising the maintainer that if persists.pas didn't exist he should also get such-and-such a project from local svn, and set up appropriate symlinks. The major issue would be directories and search paths.

That's also what I want to achieve. My original purpose is to reduce dependencies by put non-critical functions into a separate aux unit while make the main unit still workable without the aux unit.​​

As to the search path, I would suggest searching the current path (which *this* unit resides in), plus the path defined in project options. I also suggest to disable path component in this proposed directive, i.e.:

{$ifexists some/relative/path/filename.pas} and
{$ifexists /some/absolute/path/filename.pas}

are BOTH NOT allowed, only {$ifexists filename.pas} is allowed.

I don't know if this conforms to fpc traditions or not.

As to the concern about existing ppu or .o file, I think it is not a problem, because you just follow the normal rule to compare file datetime of the .o and .pas file. I don't like to rely on make files as fpc has its own building system.

Xiangrong

Reply by email