delphi - How can i define typed constant and use it in functions argument -


when defined typed constant can't use in default value of functions argument, example:

const   defc   :char  = ',';    function fun(delimiter :char=defc); 

when run code compiler show error :

[dcc32 error] : e2026 constant expression expected 

if not defined type, compiler set type ansichar , bad code, how can use way or way can compiler use char , not use ansichar character constants?

you seem rely little bit on tool-tip information. info not produced actual compiler, , not worth place bets on.

anyway, character , string literals context sensitive. means compiler (actual code generator) create appropriate code depending on how literal used.

consider these 2 consts (as alternatives):

const   defc = ',';     // tool-tip shows system.ansichar   defc = #$0298;  // tool-tip shows system.ansichar here too! 

this shows tool-tip can't relied on here, because doesn't know in context const used.

then consider following 2 functions:

function fun1(delimiter: char = defc): integer; begin   result := ord(delimiter); end;  function fun2(delimiter: ansichar = defc): integer; begin   result := ord(delimiter); end; 

and following onclick handler, calling functions

procedure tmainform.button7click(sender: tobject); begin   showmessage(inttostr(fun1));   showmessage(inttostr(fun2)); end; 

the code produced compiler differs 2 calls

mainformu.pas.233: showmessage(inttostr(fun1)); 00605036 66b89802         mov ax,$0298   // $002c if defc = ',' 0060503a e8a5ffffff       call fun1 ... mainformu.pas.234: showmessage(inttostr(fun2)); 0060504f b098             mov al,$98     // $2c if defc = ',' 00605051 e8a6ffffff       call fun2 ... 

in first call, fun1() requires char, defc literal treated unicode char, #$2098 (or $002c if defc = ',').

in second call, fun2() requires ansichar, defc const treated ansichar, #$98 (or $2c if defc = ',').

iow, character literal context sensitive, stated above.

as side note, worrying no compiler warning issued second call.


Comments

Popular posts from this blog

How to show in django cms breadcrumbs full path? -

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -