View Issue Details
ID | Project | Category | View Status | Date Submitted | Last Update |
---|---|---|---|---|---|
0031600 | FPC | Patch | public | 2017-03-26 18:18 | 2017-07-09 12:11 |
Reporter | Thaddy de Koning | Assigned To | Michael Van Canneyt | ||
Priority | normal | Severity | minor | Reproducibility | have not tried |
Status | resolved | Resolution | fixed | ||
Platform | All | OS | All | OS Version | All |
Product Version | 3.1.1 | Product Build | 35664 | ||
Target Version | 3.2.0 | Fixed in Version | 3.1.1 | ||
Summary | 0031600: Patch: Generic function RandomFrom | ||||
Description | The three RandomFrom versions are useful but there are scenarios that demand a more generic approach. This patch basically adds generic function RandomFrom<T>(const AValues:T):T; //math is objfpc mode. | ||||
Tags | No tags attached. | ||||
Fixed in Revision | 36706 | ||||
FPCOldBugId | |||||
FPCTarget | |||||
Attached Files |
|
|
math.pp.patch (886 bytes)
Index: rtl/objpas/math.pp =================================================================== --- rtl/objpas/math.pp (revision 35664) +++ rtl/objpas/math.pp (working copy) @@ -613,7 +613,7 @@ function RandomFrom(const AValues: array of Double): Double; overload; function RandomFrom(const AValues: array of Integer): Integer; overload; function RandomFrom(const AValues: array of Int64): Int64; overload; - +generic function RandomFrom<T>((const AValues: array of T):T; { cpu specific stuff } type TFPURoundingMode = system.TFPURoundingMode; @@ -2605,6 +2605,11 @@ result:=AValues[random(High(AValues)+1)]; end; +generic function RandomFrom<T>(const AValues: array of T): T; +begin + result:=AValues[random(High(AValues)+1)]; +end; + function FutureValue(ARate: Float; NPeriods: Integer; APayment, APresentValue: Float; APaymentTime: TPaymentTime): Float; var |
|
The Patch is ok, but description should of course read generic function RandomFrom<T>(const AValues:array of T):T; //math is objfpc mode. But that was probably obvious. Aside: These functions are good candidates to move to sysutils? |
|
Hm. No good. Needs something active, which I overlooked in my second test serie. The patch chokes, after my clean tests but I can't see why... Didn't see that. |
|
look good function from any type random from array value |
|
Yes but the patch *fails* my tests. This will work, standalone, in a separate unit in trunk only. I also know why: the global generic function/procedure feature wasn't backported to 3.0.2 and I wrongly assumed that that was the case. I also had a dangling OVERRIDEVERSIONCHECK in my test setup: then it works... Now there is a way to improve on the patch: - math.pp is needed for the compiler it seems. - So we have to do a version check in the patch. - Once the compiler is built, it will rebuild the rtl with itself - After that it works. |
|
math.pp.corrected.patch (971 bytes)
Index: rtl/objpas/math.pp =================================================================== --- rtl/objpas/math.pp (revision 35666) +++ rtl/objpas/math.pp (working copy) @@ -613,7 +613,9 @@ function RandomFrom(const AValues: array of Double): Double; overload; function RandomFrom(const AValues: array of Integer): Integer; overload; function RandomFrom(const AValues: array of Int64): Int64; overload; - +{$if FPC_FULLVERSION >=301010} +generic function RandomFrom<T>(const AValues:array of T):T; +{$ifend} { cpu specific stuff } type TFPURoundingMode = system.TFPURoundingMode; @@ -2604,7 +2606,12 @@ begin result:=AValues[random(High(AValues)+1)]; end; - +{$if FPC_FULLVERSION >=301010} +generic function RandomFrom<T>(const AValues:array of T):T; +begin + result:=AValues[random(High(AValues)+1)]; +end; +{$ifend} function FutureValue(ARate: Float; NPeriods: Integer; APayment, APresentValue: Float; APaymentTime: TPaymentTime): Float; var |
|
Attached new patch. This one is fully tested with bootstrap 3.0.0 and bootstrap 3.0.2. Attached demo. |
|
testrandomfrom.pas (294 bytes)
program testrandomfrom; {$mode delphi} uses math,sysutils; var i:integer; a:array[0..99] of string; begin for i :=0 to 99 do a[i] := IntToStr(i); for i :=1 to 100 do begin write(RandomFrom<string>(a):4); if i mod 10 = 0 then writeln; end; readln; end. |
|
Added, thanks for the contibution! |
Date Modified | Username | Field | Change |
---|---|---|---|
2017-03-26 18:18 | Thaddy de Koning | New Issue | |
2017-03-26 18:18 | Thaddy de Koning | File Added: math.pp.patch | |
2017-03-26 18:23 | Thaddy de Koning | Note Added: 0099234 | |
2017-03-26 18:24 | Thaddy de Koning | Note Edited: 0099234 | View Revisions |
2017-03-26 19:05 | Thaddy de Koning | Note Added: 0099235 | |
2017-03-27 01:33 | Derit Agustin | Note Added: 0099245 | |
2017-03-27 11:01 | Thaddy de Koning | Note Added: 0099247 | |
2017-03-27 11:07 | Thaddy de Koning | Note Edited: 0099247 | View Revisions |
2017-03-27 11:08 | Thaddy de Koning | Note Edited: 0099247 | View Revisions |
2017-03-27 12:58 | Thaddy de Koning | Note Edited: 0099247 | View Revisions |
2017-03-27 12:58 | Thaddy de Koning | File Added: math.pp.corrected.patch | |
2017-03-27 13:00 | Thaddy de Koning | Note Added: 0099249 | |
2017-03-27 13:14 | Thaddy de Koning | File Added: testrandomfrom.pas | |
2017-03-27 13:15 | Thaddy de Koning | Note Edited: 0099249 | View Revisions |
2017-07-09 12:11 | Michael Van Canneyt | Fixed in Revision | => 36706 |
2017-07-09 12:11 | Michael Van Canneyt | Note Added: 0101632 | |
2017-07-09 12:11 | Michael Van Canneyt | Status | new => resolved |
2017-07-09 12:11 | Michael Van Canneyt | Fixed in Version | => 3.1.1 |
2017-07-09 12:11 | Michael Van Canneyt | Resolution | open => fixed |
2017-07-09 12:11 | Michael Van Canneyt | Assigned To | => Michael Van Canneyt |
2017-07-09 12:11 | Michael Van Canneyt | Target Version | => 3.2.0 |