Re: [fpc-pascal] EnumToString
You can directly write enums with Write or WriteLn and convert enums to strings with WriteStr. program EnumStr; {$mode objfpc}{$H+} type TColor = (cRed, cGreen, cBlue, cYellow); function ColorToStr(C: TColor) : String; begin WriteStr(Result, C); end; var Color : TColor; begin for Color in TColor do WriteLn(ColorToStr(Color)); end. 2011/11/1 Juha Manninen : > Hi > I remember there is a way to get a string representation of an enumerated > type directly without using a lookup string array, but I forgot the syntax. > Lazarus uses only lookup arrays, maybe because the other syntax is new. > How is the syntax? > Juha > > ___ > fpc-pascal maillist - fpc-pascal@lists.freepascal.org > http://lists.freepascal.org/mailman/listinfo/fpc-pascal > ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] 2 bugs ???
2011/9/7 Leonardo M. Ramé : > From: Yann Bat > > > The error message is correct, because inside the scope of method Bug, _T > points to nil, and the size of nil is 0, so 1 div 0 raises the error. > I think that SizeOf(_T) should be unknown rather than 0. The compiler should not make any assumption about _T before specialization. > What about using Result := 100 div SizeOf(FField); ? Same error with SizeOf(FField). > > Leonardo. > ___ > fpc-pascal maillist - fpc-pascal@lists.freepascal.org > http://lists.freepascal.org/mailman/listinfo/fpc-pascal > ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] 2 bugs ???Q
Thanks. You've just found bug number 3 ! :-) But #1 and #2 are alway here. 2011/9/7 Marco van de Voort : > In our previous episode, Yann Bat said: >> I am trying to learn freepascal generics and I think that I have found 2 >> bugs. > >> var >> V: TGenInt; >> begin >> V.Create(589); > > This is no pascal way of creating a class. Use v:=tgenint.create; > > ___ > fpc-pascal maillist - fpc-pascal@lists.freepascal.org > http://lists.freepascal.org/mailman/listinfo/fpc-pascal > ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
[fpc-pascal] 2 bugs ???
Hi, I am trying to learn freepascal generics and I think that I have found 2 bugs. Tested with FPC 2.4.4 Linux x86 == = BUG 1 - with SizeOf = == program SizeOfBug; {$mode objfpc}{$H+} type generic TGen<_T> = class(TObject) private FField: _T; public constructor Create(Val: _T); function Bug: Integer; end; {--- TGen.Create ---} constructor TGen.Create(Val: _T); begin inherited Create; FField := Val; end; {--- TGen.Bug ---} function TGen.Bug : Integer; begin Result := 10 div SizeOf(_T); // *** DIVISION BY ZERO *** // THE FOLLOWING CODE IS OK ! // // var // S: Integer; // begin // S := SizeOf(_T); // Result := 10 div S; end; type TGenInt = specialize TGen; var V: TGenInt; begin V.Create(589); WriteLn('V.Bug = ', V.Bug); V.Free; end. == = BUG 2 - with WriteLn = == program WriteLnBug; {$mode objfpc}{$H+} type generic TGen<_T> = class(TObject) private FField: _T; public constructor Create(Val: _T); procedure Bug; end; {--- TGen.Create ---} constructor TGen.Create(Val: _T); begin inherited Create; FField := Val; end; {--- TGen.Bug ---} procedure TGen.Bug; begin WriteLn('FField = ', FField); // *** CAN'T READ OR WRITE VARIABLES OF THIS TYPE *** end; type TGenInt = specialize TGen; var V: TGenInt; begin V.Create(589); V.Bug; V.Free; end. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Length bug ?
2010/10/11 Michael Van Canneyt : > > Please enter a bug report for this. > Done. http://mantis.freepascal.org/view.php?id=17604 ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
[fpc-pascal] Length bug ?
Hi, I think I have found a bug in FPC 2.4.0. Tested under Windows XP 32 bits and Linux 32 bits. program TestLength; begin WriteLn('L =', Length('') ); end. This program gives L = 1 but I think it should give L = 0. Is this really a bug ? Can you reproduce this ? Thanks ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] FPC bug or brain bug ?
> It is allowed in Delphi and TP because they allow declaring typed constants > of objects. Exactly what I wanted to use. I'll have to use constructors so. Thank you for your help. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] FPC bug or brain bug ?
> The compiler always adds a VMT if an object has a constructor or destructor. > The reason is that the VMT also contains the instance size, which is used by > the constructor helper to allocate the required amount of memory. > Ok but why a different behaviour between [fpc | objfpc] mode and [tp | delphi] mode ? ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
[fpc-pascal] FPC bug or brain bug ?
Hi, I don't understand why compilation of the program below failed in fpc and objfpc mode. In tp and delphi mode it works as expected but in fpc and objfpc mode the compiler complains : blob.pas(17,3) Error: Constants of objects containing a VMT aren't allowed blob.pas(17,3) Fatal: Syntax error, ";" expected but "(" found Since my object has no virtual methods why a VMT ? Thanks. {$mode fpc} program Blob; type TBlob = object private fId : LongInt; public constructor Init; function Id: LongInt; end; PBlob=^TBlob; const NullBlob : TBlob = (fId: 0); {=== TBlob ===} constructor TBlob.Init; begin fId:=Random(1024) + 1; end; function TBlob.Id; begin Id:=fId; end; var B : TBlob; begin B:=NullBlob; WriteLn('B : ', B.Id); B.Init; WriteLn('B : ', B.Id); end. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
[fpc-pascal] Re: Memory allocation problem
Please forgot my stupid question. GetMem works perfectly as described in the documentation. My problem cames from an another part of my program. Yann ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
[fpc-pascal] Memory allocation problem
Hi, I'm looking for a function or a procedure that tries to allocate memory and return nil on failure. GetMem seems to be a good candidate but it doesn't works as expected. The FreePascal documentations (http://www.freepascal.org/docs-html/rtl/system/getmem.html) tells that it returns nil on failure but it raises an out of memory exception (may be bug ???) when I try to allocates too much memory on linux (I haven't try on windows). Since my code is in fpc mode I can't catch the exeption. I know that there is a ReturnNilIfGrowHeapFails variable but this changes the behaviour of the all memory allocation system... I think I can do something like the following code but I fear it's not thread safe. function GetMemOrNil(size: PtrInt): pointer; var tmp : boolean; begin tmp:=ReturnNilIfGrowHeapFails; ReturnNilIfGrowHeapFails:=true; GetMemOrNil:=GetMem(size); ReturnNilIfGrowHeapFails:=tmp; end; Yann Ps : Sorry for poor english. I hope you can understand me. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal