Silly struct behaviour
H. S. Teoh via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Jul 13 11:09:46 PDT 2017
On Thu, Jul 13, 2017 at 06:07:31PM +0000, JN via Digitalmars-d-learn wrote:
> Consider:
>
> struct Foo
> {
> int bar;
> }
>
> void processFoo(Foo foo)
> {
> }
>
> void main()
> {
> Foo f = {bar: 5};
> processFoo(f); // ok
> processFoo(Foo(5)); // ok
> processFoo({bar: 5}); // fail
> processFoo(Foo({bar: 5})); // fail
> }
>
>
> Whyyyy D? It makes no sense, the compiler knows what is the type of
> the first processFoo arg anyway...
It's not quite so simple. Consider for example:
struct Foo { int bar; }
struct Oof { int bar; }
void process(Foo foo) { }
void process(Oof oof) { formatDisk(); }
void main() {
process({bar : 5}); // which overload should get called?
}
As for `Foo({bar : 5})`, that's just wrong syntax. Just write `Foo(5)`
and it will work.
T
--
People demand freedom of speech to make up for the freedom of thought which they avoid. -- Soren Aabye Kierkegaard (1813-1855)
More information about the Digitalmars-d-learn
mailing list