Sign in Join

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

Mailing lists / fpc-pascal / 2010-10

Troubles with overloaded operators

2 messages · 1 person · –

Reply by email Subscribe to this list Your subscription

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

Sven Barth

Hi together!

I'm currently wrapping V4L2 in an object oriented way and thought that I could use operator overloading to convert flag ordinals to Pascal sets. My operator compiled without problems, but when I tried to use it I got a type error.

So I reduced my problem to a single test unit:

==== begin ====

program opoverload;
{$mode objfpc}

type
TTest = (
test1,
test2,
test3

   );
   TTests = set of TTest;
operator := (aRight: LongWord) aLeft: TTests;
begin
   Result := [];
   if (aRight >= 0) and (aRight < 21) then
     Include(Result, test1);
   if (aRight >= 21) and (aRight <= 42) then
     Include(Result, test2);
   if (aRight >= 0) and (aRight <= 42) then
     Include(Result, test3);
end;
var
   i: LongWord;
   t: TTests;
begin
   i := 21;
//  t := i;
end.

==== end ====

Compiling it as is succeeds without problems, but enabling the commented assignment to "t" results in the following output:

====

fpc opoverload.pasfpc opoverload.pas

Free Pascal Compiler version 2.4.0 [2010/01/01] for i386 Copyright (c) 1993-2009 by Florian Klaempfl
Target OS: Linux for i386
Compiling opoverload.pas
opoverload.pas(29,8) Error: Incompatible types: got "LongWord" expected "TTests"
opoverload.pas(31) Fatal: There were 1 errors compiling module, stopping Fatal: Compilation aborted
Error: /home/sven/.bin/ppc386 returned an error exitcode (normal if you did not specify a source file to be compiled)
====

Is this a bug (I'm using 2.4.0, but I've tested it with a 2.5.1 from end of August as well)? Or is it not possible to use such an overload (which would be a pity...)?

Regards,
Sven

Reply by email

Sven Barth
to Sven Barth
Sven Barth wrote
Is this a bug (I'm using 2.4.0, but I've tested it with a 2.5.1 from end of August as well)? Or is it not possible to use such an overload (which would be a pity...)?

I've searched Mantis a bit and stumpled upon this bug report: http://bugs.freepascal.org/view.php?id=13473

Jonas writes in the comments:

==== quote begin ====

It is not possible to overload operators involving ordinal types

Well, operators involving only combinations of ordinals, enums and strings.

==== quote end ====

Is this the case for sets as well?

The following are the cases that I'd like to overload:

SomeSet := SomeLongInt (converting a C-style flags int to a set)
SomeLongInt := SomeSet (converting a set to a C-style flags int)
SomeString := SomeSet (for writing a set to console)

Regards,
Sven

Reply by email