Indexing arrays with enums in java: Internal compiler error

Elements: 10.0.0.2557
Platform: java

The following code produces internal compiler error:

namespace TestEnumIndex;

uses
  java.util;

  type
    TestEnum = public enum (One, Two, Three) of java.lang.Enum;
//  TestEnumArray = array [TestEnum] of Integer;// (E225) "TestEnum" is not a valid type for array ranges	TestEnumIndex
    TestEnumArray = array [low(TestEnum) .. high(TestEnum)] of Integer;

type
  Program = class
  public

    class method Main(args: array of String): Int32;
    begin
      var arr: TestEnumArray;
      arr[TestEnum.One] := 22;
// The above line gives:
//Severity	Code	Description	Project	File	Line	Suppression State
//Error		(E0) Internal error: System.NullReferenceException: Object reference not set to an instance of an object.
//   at RemObjects.Oxygene.Code.ArrayAccessExpressionTransform.ResolveExpression(ScopeInfo aScope, ArrayAccessExpression array)
//   at RemObjects.Oxygene.Code.Compiler.NewResolveExpressionAndStatement.VisitExpression(ScopeInfo aScope, Expression element)
//   at RemObjects.Oxygene.Code.AssignmentStatementTransform.ResolveStatement(ScopeInfo aScope, AssignmentStatement assignmentStatement)
//   at RemObjects.Oxygene.Code.Compiler.NewResolveExpressionAndStatement.VisitStatement(ScopeInfo aScope, Statement element)
//   at RemObjects.Oxygene.Code.BeginStatementTransform.ResolveStatement(ScopeInfo aScope, BeginStatement beginStatement)
//   at RemObjects.Oxygene.Code.Compiler.NewResolveExpressionAndStatement.VisitStatement(ScopeInfo aScope, Statement element)
//   at RemObjects.Oxygene.Code.Compiler.ResolveMembersCompiler.ResolveMethodBody(ScopeInfo aScope, IMethodImplementation aMethod)
//   at RemObjects.Oxygene.Code.Compiler.ResolveMembersCompiler.<>c__DisplayClass14.<ResolveMembers>b__5(IParsedType aType)
//   at RemObjects.Oxygene.Code.Compiler.Compiler.ForAllInternalTypes(Action`1 aAt)
//   at RemObjects.Oxygene.Code.Compiler.ResolveMembersCompiler.ResolveMembers__$mapped(Compiler self)	TestEnumIndex	D:\RemObjects\TestEnumIndex\Program.pas	19	

    end;

  end;
end.

Thanks, logged as bugs://85054

bugs://85054 got closed with status fixed.

So this now fails because the compiler really can’t get the numeric values of a java enum. But it properly fails with a good error.

OK, I understand. But what is then the right way to declare an array indexed by an enum?

In my example above

 TestEnumArray = array [TestEnum] of Integer;// (E225) "TestEnum" is not a valid type for array ranges	TestEnumIndex

didn’t compile, while

 TestEnumArray = array [low(TestEnum) .. high(TestEnum)] of Integer;

did compile, but is now perhaps illegal because “the compiler really can’t get the numeric values of a java enum”.

You need an integer-based Enum, essentially.

Yes, but then this brings us back to Elements 2513 and 2521: Setting enum value from String

Cannot have both: from string to java-based enum, and array of[java-based enum].

Kind of a deadlock, and this is stale for a long time now.

Indeed you just can’t. The info just isn’t there to support both, sorry.

Sorry, I disagree completely.

There is enough information in the code and the proof is my software that uses both features and runs perfectly in Elements.2409. I understand that you redesigned enums after that, but this broke a lot of my code and took away the functionality which is, I believe, not essential just for me, but for the clarity of Oxygene in general.

The only feature that I really need is setting an enum value from a string for any enum type, as explained in Elements 2513 and 2521: Setting enum value from String.

I know I can make a tiny stringlist for each declared enum and search through the list to match the string with the corresponding enum value. But I don’t want to do this because:

(1) This requires a manual declaration of a string list for each and every declared enum type (which I have many),
(2) this clutters the code and
(3) makes it less manageable, as the same information is entered twice in two places,
(4) and is simply a shameful workaround for a high-level language claiming to support enums.

I spent a lot of time trying to find a suitable solution myself, and failed. I also spent a lot of effort to communicate the issue with you and explain why it is important for me and Oxygene in general. I apparently failed, too. However, please don’t chase me away with the “impossible” argument, I am not buying it. I know the solution is possible. It was possible in Elements.2409. If I can make those tiny string lists myself, I see no reason why the compiler cannot make them silently for me behind the scenes. It does have all the information in the code and it is your compiler, FGS.

I give up. Until this is resolved, I am stuck with Elements.2409.

The point is, other thinks we’re broken in 2409, and we can’t (as I understand) fix it to support both

I’ll have to leave this to Carlo to answer in more detail, sorry, as this is not my area of expertise.

@markobohanec so this was actually broken too in 2409 and would always return 0. But I think I have a plan of attack now. I’ll see what I can do later this week to tackle this.

1 Like

Thanks, logged as bugs://85088

Thanks, logged as bugs://85089

bugs://85089 got closed with status fixed.

bugs://85088 got closed with status fixed.

bugs://85089 got closed with status fixed.

bugs://85088 got closed with status fixed.

bugs://85088 got closed with status fixed.