Delphi: All constants are constant, but some are more constant than others?

前端 未结 7 1467
眼角桃花
眼角桃花 2021-01-03 20:54

Consider:

const 
   clHotlight: TColor = $00FF9933;
   clLink = clHotLight; //alias of clHotlight

[Error] file.pas: Constant expression expected
         


        
7条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-03 21:23

    The right side of a constant declaration must be a "constant expression", which is defined as "a constant expression is an expression that the compiler can evaluate without executing the program in which it occurs". You can find the whole accepted syntax for constant expression in the language guide. Note that the language guide explicitly states "Typed constants cannot occur in constant expressions." - and that's why your declarations fails, both clHotlight: TColor = $00FF9933; and AdministratorGUID: TGUID =...; are typed constants. Also, constant expression cannot include functions calls except those listed in the language guide (i.e. Length(), SizeOf(), and some others) that the compiler is able to compute at compile time. Rewrite this way:

    const
      AdminGUID = '{DE44EEA0-6712-11D4-ADD4-0006295717DA}';
      AdministratorGUID: TGUID = AdminGUID;
      SuperuserGUID: TGUID = AdminGUID;
    

    And it will work.

提交回复
热议问题