Property expressions: Cannot assign "Integer" to "TEnum"

Issue #117 invalid
Former user created an issue

Is this a bug in DWScript?

Script:

unit Test;

type TIntegerField = class
    property Name: String;
    property Value: Integer;
end;

type TEnum = enum (Blub1, Blub2, Blub3);

type TFoo = class
    property EnumField: TIntegerField;
    property EnumFieldValue: TEnum read (EnumField.Value) write (EnumField.Value);

    constructor Create;
    begin
        EnumField := new TIntegerField;
    end;
end;

var Foo := new TFoo;
Foo.EnumField.Name := 'EnumName';
Foo.EnumField.Value := TEnum.Blub2;

PrintLn(Foo.EnumField.Name);
PrintLn(Foo.EnumField.Value);
PrintLn(Foo.EnumFieldValue);

Error:

Syntax Error: Incompatible types: Cannot assign "Integer" to "TEnum" [line: 12, column: 49]

Comments (2)

  1. Christian Budde

    I don't think it's a bug in DWScript, because EnumField is of type TIntegerField and its property Value is an Integer. This integer type is different to the EnumFieldValue property type (TEnum).

    I haven't tried this, but eventually you can cast it to TEnum like:

        property EnumFieldValue: TEnum read (TEnum(EnumField.Value)) write (TEnum(EnumField.Value));
    

    I'm not sure if this makes sense (in terms of legibility of the code), but eventually it compiles.

  2. Eric Grange repo owner

    Yes, Christian is right, this is by design (and same as in Delphi).

    To convert between an integer and an enum, an explicit cast is required.

  3. Log in to comment