Recent

Author Topic: not operator combined with "enum in set"  (Read 1949 times)

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
not operator combined with "enum in set"
« on: April 26, 2018, 03:10:03 pm »
EstaAsignado: boolean;
efErrorEjecucion: a value from an enum (say: enum1) TEnum1 = (efErrorEjecucion, efError2...);
EstadoFiscalComoSet: a set from enum (say: set of enum1) [efErrorEjecucion, efError2...]

Code: Pascal  [Select][+][-]
  1. EstaAsignado and (not (efErrorEjecucion in EstadoFiscalComoSet))

is not the same as

Code: Pascal  [Select][+][-]
  1. EstaAsignado and (not efErrorEjecucion in EstadoFiscalComoSet)

In both FPC and Delphi the one that does it right for my needs is the top one.

In fact the second one can't be compiled in Delphi.

My questions are:
- Negating an enum has sense?
- Why the second code can be compiled in FPC and not in Delphi?

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: not operator combined with "enum in set"
« Reply #1 on: April 26, 2018, 04:04:40 pm »
EstaAsignado: boolean;
efErrorEjecucion: a value from an enum (say: enum1) TEnum1 = (efErrorEjecucion, efError2...);
EstadoFiscalComoSet: a set from enum (say: set of enum1) [efErrorEjecucion, efError2...]

Code: Pascal  [Select][+][-]
  1. EstaAsignado and (not (efErrorEjecucion in EstadoFiscalComoSet))

is not the same as

Code: Pascal  [Select][+][-]
  1. EstaAsignado and (not efErrorEjecucion in EstadoFiscalComoSet)

In both FPC and Delphi the one that does it right for my needs is the top one.

In fact the second one can't be compiled in Delphi.

My questions are:
- Negating an enum has sense?
No it makes no sense but an enum is a number negating a number is a bit operation and can be done. So your the second example works on the bits of your enum instead of the boolean result of the in operator.
- Why the second code can be compiled in FPC and not in Delphi?
well the question might be why not? enum is a simple number a negation can be used to convert one enum to its opposite in the set(if set correctly).
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: not operator combined with "enum in set"
« Reply #2 on: April 26, 2018, 04:25:01 pm »
Thanks.

And another question, if I'm using Mode Delphi there should be a warning at least?

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: not operator combined with "enum in set"
« Reply #3 on: April 26, 2018, 04:33:08 pm »
Thanks.

And another question, if I'm using Mode Delphi there should be a warning at least?
it sounds reasonable at first look, but I'm not making those kind of decision, and I have so many warnings and messages in a build that I never look at them, unless there is an error, so for me, raise it do not raise it, I will never see it. it will be a small drop in the message pollution ocean.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: not operator combined with "enum in set"
« Reply #4 on: April 26, 2018, 04:35:55 pm »
Yes, in fact I only noticed that when opening delphi and try to run it, and because in Lazarus my program was not working fine, as you described I was doing the wrong operation.

Thaddy

  • Hero Member
  • *****
  • Posts: 14210
  • Probably until I exterminate Putin.
Re: not operator combined with "enum in set"
« Reply #5 on: April 26, 2018, 06:03:10 pm »
Actually it is a bit strange that Delphi does not allow the inner negation....Because it is legal boolean logic.
Anyway, the proper syntax would be:
Code: Pascal  [Select][+][-]
  1. EstaAsignado and not (efErrorEjecucion in EstadoFiscalComoSet);// skipping superfluous brackets

Let Delphi do the warning in this case....
Specialize a type, not a var.

 

TinyPortal © 2005-2018