[Issue 8766] New: unexpected compile-time error when switching a struct definition to a class

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Oct 5 20:12:59 PDT 2012


http://d.puremagic.com/issues/show_bug.cgi?id=8766

           Summary: unexpected compile-time error when switching a struct
                    definition to a class
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: blooh_ at hotmail.com


--- Comment #0 from Christopher Crouzet <blooh_ at hotmail.com> 2012-10-05 20:01:04 PDT ---
Please consider the code below:

// ------------------------------
enum Storage : int
{
  dynamic = 0
}

enum StorageOrder : int
{
  columnMajor = 0,
  rowMajor = 1
}
alias StorageOrder.columnMajor defaultStorageOrder;


class Array( T_scalar, T_args ... )
{
  alias ArrayTraits!( T_scalar, T_args ) traits;
}


struct ArrayTraits( T_scalar, T_args ... )
{
  static if ( hasFlag( Flags.storageOrder ) == true )
    alias T_args[0 .. $ - 1] shapeTuple;
  else
    alias T_args shapeTuple;

  static immutable StorageOrder storageOrder = hasFlag( Flags.storageOrder ) ?
      T_args[$ - 1] : defaultStorageOrder;


  static int getFlags()
  {
    int flags = 0;
    if ( is( typeof( T_args[$ - 1] ) == StorageOrder ) )
      flags |= Flags.storageOrder;

    return flags;
  }

  static bool hasFlag( Flags flag )
  {
    return (getFlags() & flag) != 0;
  }


  enum Flags : int
  {
    dynamic = 1 << 0,
    storageOrder = 1 << 1
  }
}


void main()
{
  auto array1d = new Array!( float, 3 );
}
// ------------------------------

It all compiles fine as it is and produce the expected results.

Now, if you declare ArrayTraits as a class rather than a struct, then the
compilation fails saying that the variable 'flag' cannot be read at compile
time. I might be wrong, but when comparing the specs of classes and structs
from the doc below, I don't see anything that would explain this error
happenning only in one case but not the other?
http://dlang.org/struct.html

Also, if you keep it declared as a class but this time define the hasFlag()
arguments at conpile-time, such as 
static bool hasFlag( Flags flag )()
and adequately replace the calls to this method, then it compiles fine. I'm
getting confused here and am not sure anymore if how I initially declared it is
valid or not?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list