Recent

Author Topic: I can not able to create character constants anymore!  (Read 6067 times)

cryborg2

  • Newbie
  • Posts: 3
I can not able to create character constants anymore!
« on: January 20, 2015, 03:24:24 pm »
const PercentSign='%';

Data type of PercentSign should be a character constant (as alaways used to be,
and as the reference says: http://www.freepascal.org/docs-html/ref/refse9.html),
but it will be a constant string.

So if I try using it as a char parameter for a procedure, fpc will detect type mismatch, and stops.
Why? From when? Is it a bug? Or an intentional change in compiler's behaviour?

Maybe I've missed something, but this isn't work for me anymore!

What could be the problem?

PS.: fpc v2.6.4
« Last Edit: January 20, 2015, 03:29:18 pm by cryborg2 »

howardpc

  • Hero Member
  • *****
  • Posts: 3177
Re: I can not able to create character constants anymore!
« Reply #1 on: January 20, 2015, 05:36:53 pm »
How do you declare your parameterised procedure?
What is the exact compiler error message?
Passing a character constant as parameter to a routine compiles fine here (FPC 2.6.4).

Blaazen

  • Hero Member
  • *****
  • Posts: 2782
  • POKE 54296,15
    • Eye-Candy Controls
Re: I can not able to create character constants anymore!
« Reply #2 on: January 20, 2015, 05:42:22 pm »
I tested with FPC 3.1.1 and works well.
What mode do you use? I tested both {$mode delphi} and {$mode fpc}, both worked.

You can try:
Code: [Select]
const PercentSign: Char ='%';
Lazarus 2.1.0 r59757M FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.11.2, Plasma 5.14.2
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

cryborg2

  • Newbie
  • Posts: 3
Re: I can not able to create character constants anymore!
« Reply #3 on: January 20, 2015, 08:20:10 pm »
It's a Lazarus program, but it is not matter anyway. The main form consist of a RadioGroup with 3 choices, a SpeedButton, and an Edit. Here is the main unit:


unit bl;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls,
  LCLType, ComCtrls, FileCtrl, Buttons, ExtCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Edit1: TEdit;
    RadioGroup1: TRadioGroup;
    SpeedButton1: TSpeedButton;
    procedure SpeedButton1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

const
  sRule='§';
  sAdvise='!';
  sInfo='-';

implementation

function TranslateChar(Sign: Char): string;
begin
  case Sign of
    sRule: Result:='Rule: ';
    sAdvise: Result:='Advise: ';
    sInfo: Result:='Information: ';
  else
    Result:='';
  end;
end;

{ TForm1 }

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
  case RadioGroup1.ItemIndex of
    0: Edit1.Text:=TranslateChar(sRule);
    1: Edit1.Text:=TranslateChar(sAdvise);
    2: Edit1.Text:=TranslateChar(sInfo);
  end;
end;

initialization
  {$I bl.lrs}

end.



The compiler error messages are:

Hint: Start of reading config file F:\lazarus\fpc\2.6.4\bin\i386-win32\fpc.cfg
Hint: End of reading config file F:\lazarus\fpc\2.6.4\bin\i386-win32\fpc.cfg
Free Pascal Compiler version 2.6.4 [2014/10/11] for i386
Copyright (c) 1993-2014 by Florian Klaempfl and others
Target OS: Win32 for i386
Compiling blprj.lpr
Compiling bl.pas
bl.pas(39,10) Error: Constant and CASE types do not match
bl.pas(39,10) Error: Ordinal expression expected
bl.pas(52,39) Error: Incompatible type for arg no. 1: Got "Constant String", expected "Char"
bl.pas(36,10) Hint: Found declaration: TranslateChar(Char):AnsiString;
bl.pas(63) Fatal: There were 3 errors compiling module, stopping

« Last Edit: January 20, 2015, 08:22:40 pm by cryborg2 »

howardpc

  • Hero Member
  • *****
  • Posts: 3177
Re: I can not able to create character constants anymore!
« Reply #4 on: January 20, 2015, 08:32:33 pm »
The reason is that your constant sRule is an ansistring, not a Char.
It is a unicode symbol ('§'), represented in Lazarus as a utf8 encoded string.
The fact that it appears to be only one "character" long erroneously caused you to think that you were declaring a character constant.

Whereas the dollar sign ($) can be represented as a Char, since there is an ASCII code for it which is less than 255, so its representation will fit in a char variable.

If you change the declaration of the function to
Code: [Select]
function TranslateChar(Sign: string): string;it will compile and work as expected, since FPC allows not just ordinals like Chars, but strings as well for case selector variables.
« Last Edit: January 20, 2015, 08:38:49 pm by howardpc »

Blaazen

  • Hero Member
  • *****
  • Posts: 2782
  • POKE 54296,15
    • Eye-Candy Controls
Re: I can not able to create character constants anymore!
« Reply #5 on: January 20, 2015, 08:34:09 pm »
I observed the same as howardpc. '§' is guilty.
Lazarus 2.1.0 r59757M FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.11.2, Plasma 5.14.2
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

cryborg2

  • Newbie
  • Posts: 3
Re: I can not able to create character constants anymore!
« Reply #6 on: January 20, 2015, 10:03:32 pm »
So, that means I will never be able to declare a character as a constant if its asci code greater or equal than 128?  :o  :'(
Is there a way to avoid this behaviour?

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2280
    • havefunsoft.com
Re: I can not able to create character constants anymore!
« Reply #7 on: January 20, 2015, 10:16:34 pm »
try this:
Code: [Select]
function TranslateChar(Sign: WideChar): string;
begin
  case Sign of
    sRule: Result:='Rule: ';
    sAdvise: Result:='Advise: ';
    sInfo: Result:='Information: ';
  else
    Result:='';
  end;
end;


this should work exceptionally well for utf8 source code files.
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

ttomas

  • Full Member
  • ***
  • Posts: 181
Re: I can not able to create character constants anymore!
« Reply #8 on: January 23, 2015, 02:14:45 pm »
You can try
Code: [Select]
sRule=#167;

Blaazen

  • Hero Member
  • *****
  • Posts: 2782
  • POKE 54296,15
    • Eye-Candy Controls
Re: I can not able to create character constants anymore!
« Reply #9 on: January 23, 2015, 02:50:07 pm »
You can try
Code: [Select]
sRule=#167;
It will not work since values from 128 to 255 are not available in UTF8.
Lazarus 2.1.0 r59757M FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.11.2, Plasma 5.14.2
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

ttomas

  • Full Member
  • ***
  • Posts: 181
Re: I can not able to create character constants anymore!
« Reply #10 on: January 25, 2015, 03:56:38 pm »
@Blaazen
Not true. Work fine here, original cryborg2 source, just change
const sRule=#167;
CT 5.20, FPC 3.1.1, Win32/64

Blaazen

  • Hero Member
  • *****
  • Posts: 2782
  • POKE 54296,15
    • Eye-Candy Controls
Re: I can not able to create character constants anymore!
« Reply #11 on: January 25, 2015, 05:00:08 pm »
Here in Linux it writes out "?" in Lazarus and in plain FPC too.
Lazarus 2.1.0 r59757M FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.11.2, Plasma 5.14.2
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/