Bug 1547 – Default parameter values should use implicit static opCall
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2007-10-03T15:15:11Z
Last change time
2019-10-24T11:51:25Z
Keywords
pull
Assigned to
No Owner
Creator
Bill Baxter
Comments
Comment #0 by wbaxter — 2007-10-03T15:15:11Z
For the most part it's possible to make a struct that wraps an array act like a built-in array. However it's not possible to give a default value of null to a struct. I think the code below should probably call the static opCall for foo().
module structnull;
import std.stdio;
struct Struct
{
int[] data;
static Struct opCall(int[] d) {
writefln("static opCall");
Struct ret; ret.data = d; return ret;
}
void opAssign(int[] d) {
writefln("opAssign");
Struct ret; ret.data = d; return ret;
}
int opEquals(int[] d) {
writefln("opEquals");
return d==data;
}
}
// this gives
// structnull.d(37): Error: cannot implicitly convert expression (null) of type void* to Struct
void foo(Struct s = null)
{
}
void main()
{
Struct a = null; // ok opCall
a = null; // ok opAssign
if (a == null) { // op opEquals
writefln("yep");
}
}
Comment #1 by walter — 2007-11-28T10:27:02Z
This bug does not only apply to null as a default parameter, but in every case where a struct OR class should get initialized by another type.
To point out the importance of this issue, I'd like to mention the application of a GMP wrapper library which wraps the GMP types (from the C library) in structs and expression templates. For GMP it is important that one can use these structs like native datatypes:
void foo (mpz arg = 0)
{
...
}
one may argue, that you can write "arg = T(0)" (via static opCall), but this does not work for int, so using mpz like int would not work!
best regards
Matthias Walter
Comment #2 by dlang-bot — 2019-08-21T12:14:20Z
@RazvanN7 created dlang/dmd pull request #10329 "Fix Issue 1547 - Default parameter values should use implicit static opCall" fixing this issue:
- Fix Issue 1547 - Default parameter values should use implicit static opCall
https://github.com/dlang/dmd/pull/10329
Comment #3 by dlang-bot — 2019-08-27T07:31:37Z
dlang/dmd pull request #10329 "Fix Issue 1547 - Default parameter values should use implicit static opCall" was merged into master:
- 1b552a67a1d205ff2747897d0934c922a869e25e by RazvanN7:
Fix Issue 1547 - Default parameter values should use implicit static opCall
https://github.com/dlang/dmd/pull/10329
Comment #4 by nick — 2019-10-24T11:51:25Z
(In reply to Bill Baxter from comment #0)
> void foo(Struct s = null)
Nice that this is now allowed, is it possible to also allow calling foo(null)? Then structs could be a drop-in replacement for pointers, but doing some extra checks on certain operations.