* * *

Poll

I know the answer.

false
0 (0%)
unknown
2 (66.7%)
true
1 (33.3%)

Total Members Voted: 3

Author Topic: trinary logic with operator overloads  (Read 1481 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 5968
Re: trinary logic with operator overloads
« Reply #15 on: May 08, 2018, 09:15:49 am »
[Why doesn't the following work?
Code: Pascal  [Select]
  1. program op(input, output, stderr);
  2. type
  3.         enum = (zero, one, two, three = one + two);
  4. begin
  5.         writeLn(one + two); // ideally prints three
  6. end.
Well, this works:
Code: Pascal  [Select]
  1. type enum = (zero, one, two, three = one + two);
  2. begin
  3.   writeLn(enum(ord(one) + ord(two))); // actually prints three
  4. end.
If I only could do that with NOT.... :(

In this case even this works:
Code: Pascal  [Select]
  1. type enum = (zero = -5, one = 10, two = 7, three = one + two);
  2. begin
  3.   writeLn(enum(ord(one) + ord(two))); // actually prints three
  4.   writeln(Ord(Three)); // should be 17,right?
  5. end.
Ok, compiler has a note...

I still feel that negation, addition and subtraction should be legal as overloads. Maybe not for enums with assigned values, as the ordinal test shows.
That is a hornet's nest.
« Last Edit: May 08, 2018, 09:31:18 am by Thaddy »
recommends {$macro on}{$define Silly:=ObjFpc}

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus