Search code examples
enumsddmd

Compiler thinks enum values are starting at 129


I recently started writing a chess engine in D. I started off with writing the board representation. Shortly after I ran into a problem with the enum that I am using for the pieces.

enum Piece : ubyte
{
  Empty,
  Pawn,
  Rook,
  Knight,
  Bishop,
  Queen,
  King
}

When I try to compile this, I get an error that

src/board.d(69): Error: cannot implicitly convert expression (129) of type int to byte

Note: I am using DMD64 D Compiler v2.068.1. The flags I am passing can be found in the makefile that is in the github repo I have linked at the bottom.

This error is printed six times. Each time the number in parentheses is incremented once. So it goes from 120 to 134. The line number, 69, corresponds to the Empty value in the Piece enum.

I am trying to get the values to go from 0, for empty, to 6, for the King, but it appears that the compiler thinks it should be starting at 129 for some reason. I have tried setting the values explicitly but it hasn't made any difference and the compiler still throws a bunch of errors.

I could try switching over to using integers to solve this problem but that would still leave the compiler thinking the enum values are starting at 129 and mess up the bit operations that I am planning on using.

If anyone could help me figure out what is wrong with this code that would be greatly appreciated.

The full program can be found here


Solution

  • The error message points at the wrong location. The enum itself is fine.

    The actual error is that Board.pieces has the wrong type. It should be ubyte[64], not byte[64].

    When you later try to assign values that have the 8th bit set that fails, because byte's range is too small.

    Please file an issue at issues.dlang.org.