Sign in Join

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

Mailing lists / fpc-pascal / 2007-05

Integer = Extended?

5 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

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

Wolfram Kläger

WhatŽs wrong with the following declarations:

function Any(A1 : Extended; A2 : Extended) : AnyType; overload;
function Any(A1 : Integer; A2 : Integer) : AnyType; overload;

FPC 2.04 on Win32 says, both functions have same parameter list. Consequently, there is no error message, when IŽm feeding the extended version with Integers.

Say, such autocast is a convenience feature in many cases. For my application itŽs weird. What is the appropriate workaround? Is there some homemade type checking like

if A1 is Integer then SureItsInteger else SureItsExtended?

Wolfram

Reply by email

Daniël Mantione
to Wolfram Kläger

Op Thu, 17 May 2007, schreef Wolfram Kläger:

Show quoted text 11 lines
What?s wrong with the following declarations: function Any(A1 : Extended; A2 : Extended) : AnyType; overload; function Any(A1 : Integer; A2 : Integer) : AnyType; overload; FPC 2.04 on Win32 says, both functions have same parameter list. Consequently, there is no error message, when I?m feeding the extended version with Integers. Say, such autocast is a convenience feature in many cases. For my application it?s weird. What is the appropriate workaround? Is there some homemade type checking like if A1 is Integer then SureItsInteger else SureItsExtended?

Use exact matching parameters. FPC will try to do as little type conversions as possible, so if you feed your procedure with integers it won't call the extended version. If you want to call the extended version, convert the parameters to extended first.

It is however good practise to make both variants have the same semantics.

Daniël

Reply by email

Wolfram Kläger
Show quoted text 5 lines
-----Ursprüngliche Nachricht----- Von: FPC-Pascal users discussions Gesendet: 17.05.07 08:21:54 An: FPC-Pascal users discussions Betreff: Re: [fpc-pascal] Integer = Extended?
Show quoted text 25 lines
Op Thu, 17 May 2007, schreef Wolfram Kläger: > What?s wrong with the following declarations: > > function Any(A1 : Extended; A2 : Extended) : AnyType; overload; > > function Any(A1 : Integer; A2 : Integer) : AnyType; overload; > > FPC 2.04 on Win32 says, both functions have same parameter list. Consequently, there is no error message, when I?m feeding the extended version with Integers. > > Say, such autocast is a convenience feature in many cases. For my application it?s weird. What is the appropriate workaround? Is there some homemade type checking like > > if A1 is Integer then SureItsInteger else SureItsExtended? Use exact matching parameters. FPC will try to do as little type conversions as possible, so if you feed your procedure with integers it won't call the extended version. If you want to call the extended version, convert the parameters to extended first. It is however good practise to make both variants have the same semantics. Daniël

Thank you, Daniel.

Must have been for some other reason. Could not reproduce the error. So I am happy again, that I can rely on type checks in Pascal, as I am used to.

But what about the homemade type check, anyway? Dealing with classes, I know how to use 'is', e.g. if myclass is TMyClass then ... Is there any similar construct for 'if x is Integer then .. else ...'?

Finally I agree in general, regarding good practice. But my application is about image processing. The overloaded methods are for resizing images. The extended version accepts relative input, say in percent, the integer version expects absolute pixel size. I thought itŽs a good idea, to call all such methods by the same name, stressing the compilerŽs overload feature a bit.

Wolfram

Reply by email

Marco van de Voort
to Wolfram Kläger
Show quoted text 6 lines
Finally I agree in general, regarding good practice. But my application is about image processing. The overloaded methods are for resizing images. The extended version accepts relative input, say in percent, the integer version expects absolute pixel size. I thought it?s a good idea, to call all such methods by the same name, stressing the compiler?s overload feature a bit.

Not a good idea: _)

Anyway, maybe your original problem is related to what happens if the argument type it is not extended or integer, and the argument type can be converted to both.

Reply by email

ik
to Wolfram Kläger: Thank you, Daniel. Must have been for some other reason. Could not reproduce the error.…
Wolfram Kläger wrote 44 lines
> -----Ursprüngliche Nachricht----- > Von: FPC-Pascal users discussions > Gesendet: 17.05.07 08:21:54 > An: FPC-Pascal users discussions > Betreff: Re: [fpc-pascal] Integer = Extended? > > > > Op Thu, 17 May 2007, schreef Wolfram Kläger: > > > What?s wrong with the following declarations: > > > > function Any(A1 : Extended; A2 : Extended) : AnyType; overload; > > > > function Any(A1 : Integer; A2 : Integer) : AnyType; overload; > > > > FPC 2.04 on Win32 says, both functions have same parameter list. Consequently, there is no error message, when I?m feeding the extended version with Integers. > > > > Say, such autocast is a convenience feature in many cases. For my application it?s weird. What is the appropriate workaround? Is there some homemade type checking like > > > > if A1 is Integer then SureItsInteger else SureItsExtended? > > Use exact matching parameters. FPC will try to do as little type > conversions as possible, so if you feed your procedure with integers it > won't call the extended version. If you want to call the extended version, > convert the parameters to extended first. > > It is however good practise to make both variants have the same semantics. > > Daniël Thank you, Daniel. Must have been for some other reason. Could not reproduce the error. So I am happy again, that I can rely on type checks in Pascal, as I am used to. But what about the homemade type check, anyway? Dealing with classes, I know how to use 'is', e.g. if myclass is TMyClass then ... Is there any similar construct for 'if x is Integer then .. else ...'? Finally I agree in general, regarding good practice. But my application is about image processing. The overloaded methods are for resizing images. The extended version accepts relative input, say in percent, the integer version expects absolute pixel size. I thought itŽs a good idea, to call all such methods by the same name, stressing the compilerŽs overload feature a bit. Wolfram

I think Generics can solve some of the issues (but I'm only guessing), however I do not know yet how the generics on FPC works (any guides ppl ?).

Ido

Reply by email