Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2007-01

Why this code fails

7 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Alexander Todorov

Hello,
This code compiles with 2.0.4 but gives runtime error 231 . program project1;

{$mode objfpc}{$H+}
uses Variants;
var V : Variant;
    S : String = 'test ';
begin
   V := 42;
   writeln(V);
   S := S + V; // fails here
   writeln(S);
end.

the output is:
./project1
42
Runtime error 231 at $08052EBB
$08052EBB
$08066DB3
$08063166
$08063374
$080616D0
$0806217D
$08054917
$080481AD main, line 13 of project1.lpr
$0804809F

Reply by email

Jonas Maebe
to Alexander Todorov
Alexander Todorov wrote 16 lines
This code compiles with 2.0.4 but gives runtime error 231 . program project1; {$mode objfpc}{$H+} uses Variants; var V : Variant; S : String = 'test '; begin V := 42; writeln(V); S := S + V; // fails here writeln(S); end.

The reason is that it tries to convert S to a number rather than V to a string. The error is still in the current 2.1.1.

Jonas

Reply by email

Matt Emson
to Jonas Maebe
The reason is that it tries to convert S to a number rather than V to a string. The error is still in the current 2.1.1.

Does FPC have :

function VarToStr(const V: Variant): string;

As this is what I would tend to advocate for this kind os assignment anyway.

M

Reply by email

Jonas Maebe
to Matt Emson
Matt Emson wrote 9 lines
> The reason is that it tries to convert S to a number rather than V to > a string. The error is still in the current 2.1.1. Does FPC have : function VarToStr(const V: Variant): string; As this is what I would tend to advocate for this kind os assignment anyway.

Yes, it does and it works fine if used in this program.

Jonas

Reply by email

Jonas Maebe
to Matt Emson: Does FPC have : function VarToStr(const V: Variant): string; As this is what I would tend…
Matt Emson wrote 9 lines
> The reason is that it tries to convert S to a number rather than V to > a string. The error is still in the current 2.1.1. Does FPC have : function VarToStr(const V: Variant): string; As this is what I would tend to advocate for this kind os assignment anyway.

In fact, Kylix has exactly the same behaviour as FPC, so I don't consider it to be an error after all.

Jonas

Reply by email

Alexander Todorov
Matt Emson wrote
>> The reason is that it tries to convert S to a number rather than V to >> a string. The error is still in the current 2.1.1.

May be the compiler should generate compile time error or a warning. I am using VarToStr anyway just tried to look more like Perl. Thanks to all for the reply.

Alexander.

Reply by email

Jonas Maebe
to Alexander Todorov
Alexander Todorov wrote 7 lines
On 16 jan 2007, at 15:55, Matt Emson wrote: > > >> The reason is that it tries to convert S to a number rather > than V to > >> a string. The error is still in the current 2.1.1. May be the compiler should generate compile time error or a warning.

The whole point of variants is that it allows you to get around compile time type checking. If you want compile time type checking, don't use variants. Giving a warning for each "non-variant + variant" operation which might fail would give a warning for pretty much every such operation, since there are virtually always combinations which may not give a valid result.

Jonas

Reply by email