Bug 9217 – A problem with default function argument initialization

Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2012-12-27T01:19:00Z
Last change time
2012-12-27T09:04:48Z
Keywords
rejects-valid
Assigned to
nobody
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2012-12-27T01:19:26Z
import std.bigint; void foo1(BigInt x = 0) {} // Error void foo2(BigInt y = "1") {} // Error void main() { BigInt x = 0; // OK BigInt y = "1"; // OK } DMD 2.061alpha gives: test.d(2): Error: cannot implicitly convert expression (0) of type int to BigInt test.d(3): Error: cannot implicitly convert expression ("1") of type string to BigInt Workaround: import std.bigint; void foo1(BigInt x = BigInt(0)) {} // OK void foo2(BigInt y = BigInt("1")) {} // OK void main() { BigInt x = 0; // OK BigInt y = "1"; // OK } This problem in some cases asks me long function signatures as workarounds. I can't write: void foo(Ptr!(immutable(int[10])) arr=null) {...} I have to define a Ptr!(...) helper alias elsewhere, or to write this: void foo(Ptr!(immutable(int[10])) arr=Ptr!(immutable(int[10])).init) {...}
Comment #1 by andrej.mitrovich — 2012-12-27T09:04:48Z
*** This issue has been marked as a duplicate of issue 7019 ***