Overloaded function with optional set argument
As mentioned in the http://forums.smartmobilestudio.com/index.php?/topic/4002-overloaded-function-with-optinal-set-parameter-doesnt-compile/ the code below doesn't compile:
type
TOverloadOptions = set of (osNone);
type
TOverload = class
public
procedure MyProc(aValue: Integer; aOptions: TOverloadOptions = []); overload;
procedure MyProc(aValue: string; aOptions: TOverloadOptions = []); overload;
end;
{ TOverload }
procedure TOverload.MyProc(aValue: Integer; aOptions: TOverloadOptions = []);
begin
end;
procedure TOverload.MyProc(aValue: String; aOptions: TOverloadOptions = []);
begin
end;
procedure Test2;
var
x: TOverload;
begin
x := TOverload.Create;
x.MyProc(1); //Compiles
x.MyProc(1, [osNone]); //Doesn't compile
end;
Comments (6)
-
-
Any ideas why this was declined?
-
I haven't got a clue. It works fine for us.
-
repo owner It was requalified as https://bitbucket.org/egrange/dwscript/issues/10/improve-implicit-casts-from-static-arrays
Resolution means making a full test set to have the ambiguities resolved between matching interpretations (f.i. multiple set types can share the same elements for instance), and establishing a metric for those interpretations (for the overload resolution, including in cases where multiple set parameters are present).
The underlying issue being that once such code is accepted by the compiler, any change in the overload resolution mechanism would result in changes in code behavior (without compilation errors).
In the sample code, the first variant is accepted because it can be resolved by the metric, the second one does not because there is no metric for matching the static array. The metric is a score that expresses the distance between what is at the call site, and the overloads available, it is required because multiple parameters can have implicit casts.
Original pull request rejected mismatches but missed a metric in case of match (iow it was considering any static array as a perfect match for any set with elements of the same type, which f.i. would have meant an overload with a set parameter could have take precedence over an overload with a static array parameter).
-
I see your point. It makes perfect sense from the POW of one maintaining the compiler. However from our POW support for static set parameters is more important :-) Apart from the Format function I don't think we have a single case of static array params.
-
repo owner I have several cases which have more less been stuck in limbo over that issue, related to vectors & matrices, and for which there is the extra complexity of implicit casts (integer -> float). Other usage case here is for SQL parameters (same usage class as Format) and in-place array constructors (some dynamic).
So it's more about finding the time to write a few dozens of test cases, work out what should work and what should not, and then figure out a metric that will match Delphi's rules (when applicable). Overloading precedence rules in Delphi are a particularly undocumented area :)
- Log in to comment
FWIW this problem is solved by dabd2a6... which is part of pull request #4... which was declined![:unamused: 😒](https://pf-emoji-service--cdn.us-east-1.prod.public.atl-paas.net/standard/a51a7674-8d5d-4495-a2d2-a67c090f5c3b/48x48/1f612.png)