Can class function used in specialized class use intristic functions Low, High with generic type ?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Can class function used in specialized class use intristic functions Low, High with generic type ?

LacaK
Hi *,

I have some generic class:

generic T2DNumericArray<T> = object(specialize T2DArray<T>)
     public
       class function Truncate(Value: double): T; inline;
       ...

In class function Truncate I want check if supplied Value is in range of T.
T will be always ordinal type (byte, integer) ... (I know this, but
compiler does not of course)

Can I use in :

class function T2DNumericArray.Truncate(Value: double): T;
begin
   if Value > High(T) then
     Result := High(T)
   else if Value < Low(T) then
     Result := Low(T)
   else
     Result := Value;
end;

I understand, that from compiler POV T can be any type, so High() and
Low() can be invalid, but I wonder if there is any way how to solve this.

Thanks

-Laco.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Can class function used in specialized class use intristic functions Low, High with generic type ?

Free Pascal - General mailing list

Am 20.04.2017 13:02 schrieb "LacaK" <[hidden email]>:
>
> Hi *,
>
> I have some generic class:
>
> generic T2DNumericArray<T> = object(specialize T2DArray<T>)
>     public
>       class function Truncate(Value: double): T; inline;
>       ...
>
> In class function Truncate I want check if supplied Value is in range of T.
> T will be always ordinal type (byte, integer) ... (I know this, but compiler does not of course)
>
> Can I use in :
>
> class function T2DNumericArray.Truncate(Value: double): T;
> begin
>   if Value > High(T) then
>     Result := High(T)
>   else if Value < Low(T) then
>     Result := Low(T)
>   else
>     Result := Value;
> end;
>
> I understand, that from compiler POV T can be any type, so High() and Low() can be invalid, but I wonder if there is any way how to solve this.

The compiler will use dummy values while parsing the generic, but during specialization it will use the correct ones. Though it might be that you'd need 3.1.1 for it (dont know right now when I had fixed that).

Regards,
Sven


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Can class function used in specialized class use intristic functions Low, High with generic type ?

Cyrax
On 20.04.2017 14:37, Sven Barth via fpc-pascal wrote:

> Am 20.04.2017 13:02 schrieb "LacaK" <[hidden email]>:
>>
>> Hi *,
>>
>> I have some generic class:
>>
>> generic T2DNumericArray<T> = object(specialize T2DArray<T>)
>>      public
>>        class function Truncate(Value: double): T; inline;
>>        ...
>>
>> In class function Truncate I want check if supplied Value is in range of
> T.
>> T will be always ordinal type (byte, integer) ... (I know this, but
> compiler does not of course)
>>
>> Can I use in :
>>
>> class function T2DNumericArray.Truncate(Value: double): T;
>> begin
>>    if Value > High(T) then
>>      Result := High(T)
>>    else if Value < Low(T) then
>>      Result := Low(T)
>>    else
>>      Result := Value;
>> end;
>>
>> I understand, that from compiler POV T can be any type, so High() and
> Low() can be invalid, but I wonder if there is any way how to solve this.
>
> The compiler will use dummy values while parsing the generic, but during
> specialization it will use the correct ones. Though it might be that you'd
> need 3.1.1 for it (dont know right now when I had fixed that).
>
> Regards,
> Sven
>
>
>
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>

I think that this was the bug report and which you did fix back then :
<url:http://bugs.freepascal.org/view.php?id=28832>

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Can class function used in specialized class use intristic functions Low, High with generic type ?

LacaK
In reply to this post by Free Pascal - General mailing list

> >
> > I have some generic class:
> >
> > generic T2DNumericArray<T> = object(specialize T2DArray<T>)
> >     public
> >       class function Truncate(Value: double): T; inline;
> >       ...
> >
> > In class function Truncate I want check if supplied Value is in
> range of T.
> > T will be always ordinal type (byte, integer) ... (I know this, but
> compiler does not of course)
> >
> > Can I use in :
> >
> > class function T2DNumericArray.Truncate(Value: double): T;
> > begin
> >   if Value > High(T) then
> >     Result := High(T)
> >   else if Value < Low(T) then
> >     Result := Low(T)
> >   else
> >     Result := Value;
> > end;
> >
> > I understand, that from compiler POV T can be any type, so High()
> and Low() can be invalid, but I wonder if there is any way how to
> solve this.
>
> The compiler will use dummy values while parsing the generic, but
> during specialization it will use the correct ones. Though it might be
> that you'd need 3.1.1 for it (dont know right now when I had fixed that).
>

Yes, I can confirm, that in 3.1.1 it works, in 3.0.2 does not.
Thank you Sven

-Laco.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Can class function used in specialized class use intristic functions Low, High with generic type ?

Free Pascal - General mailing list
In reply to this post by Cyrax

Am 22.04.2017 08:18 schrieb "Cyrax" <[hidden email]>:
> I think that this was the bug report and which you did fix back then : <url:http://bugs.freepascal.org/view.php?id=28832>

Indeed it is :)

Regards,
Sven


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Loading...