Recent

Author Topic: [SOLVED] Abstract enumerated value type  (Read 3091 times)

Eugene Loza

  • Hero Member
  • *****
  • Posts: 663
    • My games in Pascal
[SOLVED] Abstract enumerated value type
« on: January 09, 2017, 09:36:30 am »
I wonder if there is a way to make an abstract enumerated value typecast?
I.e. pseudo-code:
Code: [Select]
Type TAbc = (ta,tb,tc,td);
Type TXyz = (tx,ty,tz,tw);

procedure ReturnThirdElement(inp: TEnumerated): TEnumerated;
begin
  result := TEnumerated[2];
  //result := TEnumerated(Ord(low(TEnumerated))+2));
end;

var ABC: TAbc;
    XYZ: TXyz;
begin
  ABC := ReturnThirdElement(TAbc);
  XYZ := ReturnThirdElement(TXyz);
end;
In reality I want to make a random for a computer game that makes a random choice between low(enumeratedvalue) and high(enumeratedvalue) despite what enumerated type Value is and returns the value of the same type.
I.e. now I have to do
Code: [Select]
Nationality := TNationality(random(Ord(High(TNationality))+1));
But I'd like to have something more readable like
Code: [Select]
Nationality := myRandom(TNationality);
or at least
Code: [Select]
Nationality := TNationality(myRandom(TNationality));
without need to write a specific function GetRandomNationality(inp: TNationality): TNationality; for each enumerated type case or overloading myrandom(inp:TNationality), myrandom(inp: TProfession)...
*like "for" cycle does: for i in TNationality do ...

P.S. I'm looking for a simple and reliable solution "make and forget". If there is no simple one (yes, my idea might be very stupid :)), I'll just stick with double type-casting.
« Last Edit: January 15, 2018, 12:49:41 pm by Eugene Loza »
My FOSS games in FreePascal&CastleGameEngine: https://decoherence.itch.io/ (Sources: https://gitlab.com/EugeneLoza)

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Abstract enumerated value type
« Reply #1 on: January 09, 2017, 12:44:51 pm »
Code: Pascal  [Select][+][-]
  1. generic function RandomEnum<T>: T;
  2. begin
  3.   Result := T(Random(Ord(High(T)) + 1));
  4. end;
  5.  
  6. type
  7.   TEnum = (TA,TB,TC);
  8. var
  9.   i: Integer;
  10. begin
  11.   for i := 1 to 10 do
  12.     writeln(specialize RandomEnum<TEnum>);
  13. end.
  14.  

Eugene Loza

  • Hero Member
  • *****
  • Posts: 663
    • My games in Pascal
Re: Abstract enumerated value type
« Reply #2 on: January 15, 2018, 12:49:30 pm »
@Leledumbo, I'm so sorry, I've missed your answer back then...  :-[
Thanks a lot, I've just tried it and it's working perfectly!!!
My FOSS games in FreePascal&CastleGameEngine: https://decoherence.itch.io/ (Sources: https://gitlab.com/EugeneLoza)

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: [SOLVED] Abstract enumerated value type
« Reply #3 on: January 15, 2018, 01:26:21 pm »
Note similar is in trunk math (maybe also in 3.0.4?)
Code: Pascal  [Select][+][-]
  1. program untitled;
  2. {$ifdef fpc}{$mode delphi}{$H+}{$I-}{$endif}
  3. uses math;
  4. type
  5.   TEnum = (TA,TB,TC);
  6. var
  7.   i: Integer;
  8. begin
  9.   Randomize;
  10.   for i := 1 to 10 do
  11.     writeln(RandomFrom<TEnum>([Low(TEnum),High(Tenum)]));
  12. end.

Specialize a type, not a var.

Eugene Loza

  • Hero Member
  • *****
  • Posts: 663
    • My games in Pascal
Re: [SOLVED] Abstract enumerated value type
« Reply #4 on: January 15, 2018, 01:30:18 pm »
Note similar is in trunk math
Thanks a lot, Thaddy! However, random is only one application of the concept - I'm doing a bit more processing there... so I need a custom function, like Leledumbo proposed.
My FOSS games in FreePascal&CastleGameEngine: https://decoherence.itch.io/ (Sources: https://gitlab.com/EugeneLoza)

 

TinyPortal © 2005-2018