Sign in Join

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

Mailing lists / fpc-pascal / 2010-07

Two questions about Pascal

4 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

José Mejuto

Hello FPC-Pascal,

Two "simple" questions.

1) Is there any way in Pascal to write something like this (pseudocode):

With Result:=Object.Create() do begin
  Free;
end;

Which will be written without the "with" as:

Result:=Object.Create();
Result.Free;

2) Which is the expected way to detect if a float value is exact ? Maybe only applying "if V = trunc(V) then ..." ?

Thank you for your time.

Best regards, José

Reply by email

Jonas Maebe
to José Mejuto
José Mejuto wrote 6 lines
1) Is there any way in Pascal to write something like this (pseudocode): With Result:=Object.Create() do begin Free; end;
Just leave out the "Result:=" part and it will compile/work fine..
2) Which is the expected way to detect if a float value is exact ? Maybe only applying "if V = trunc(V) then ..." ?

Use "if V = int(V) then ..." instead. trunc() converts to an int64 (which will then again be converted to a float), while int() returns a floating point type (with the fractional part removed).

Jonas

Reply by email

José Mejuto
to Jonas Maebe

Hello FPC-Pascal,

you wrote 6 lines
> 1) Is there any way in Pascal to write something like this > (pseudocode): > > With Result:=Object.Create() do begin > Free; > end;
JM> Just leave out the "Result:=" part and it will compile/work fine..

I was looking for a "with" like:

with TNX(a) "named" as TNX_a do begin
  with TNXX(b) "named" as TNXX_b do begin
    TNX_a.OP1:=TNXX_b.OP2;
    TNXX_b.OP3:=TNX_a.OP2;
    [.....]
  end;
end;

But I think that there is nothing like that in Pascal. Once piece of code uses a heavy polimorfism (in fact I think too heavy) and in some pieces of code I must perform a lot of type castings or assign a variable of the desired type wich incurres in some unneeded asignements. I had solved many of them using the "absolute" keyword, but others would be solved with a "variable scoped retype", but once the variable needs to be assigned to a result, in example, another bunch of type casts appears :(

> 2) Which is the expected way to detect if a float value is exact ? > Maybe only applying "if V = trunc(V) then ..." ?

JM> Use "if V = int(V) then ..." instead. trunc() converts to an JM> int64 (which will then again be converted to a float), while int() JM> returns a floating point type (with the fractional part removed).

Fine! Thank you.

Best regards, José

Reply by email

Luca Olivetti
to José Mejuto

Al 30/07/10 00:12, En/na José Mejuto ha escrit:

Show quoted text 11 lines
I was looking for a "with" like: with TNX(a) "named" as TNX_a do begin with TNXX(b) "named" as TNXX_b do begin TNX_a.OP1:=TNXX_b.OP2; TNXX_b.OP3:=TNX_a.OP2; [.....] end; end; But I think that there is nothing like that in Pascal.

No, there's nothing like that in FPC, but that feature was available in an old Texax Instrument's pascal compiler, though too much time has passed for me to remember the exact syntax. I think it was something like:

with a=some.record, b=another.record[i] do
begin
   a.op1:=b.op2;
   b.op3:=a.op2
end;

Bye

Luca

Reply by email