digitalmars.D.learn - Silly struct behaviour
- JN (18/18) Jul 13 2017 Consider:
- H. S. Teoh via Digitalmars-d-learn (14/37) Jul 13 2017 It's not quite so simple. Consider for example:
- JN (4/4) Jul 13 2017 I know that's a wrong syntax, I was just showing an example.
- Stefan Koch (2/6) Jul 13 2017 easily fixable by using FunctionLiterals.
- JN (4/12) Jul 13 2017 in this case, I'd expect something like:
- H. S. Teoh via Digitalmars-d-learn (8/26) Jul 13 2017 File an enhancement request:
- Jacob Carlborg (4/27) Jul 13 2017 https://github.com/dlang/DIPs/pull/71
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...
Jul 13 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)
Jul 13 2017
I know that's a wrong syntax, I was just showing an example. Yes, here it will work, but if you want to initialize only some fields (poor man's keyword arguments), you can't use the default constructor.
Jul 13 2017
On Thursday, 13 July 2017 at 18:45:45 UTC, JN wrote:I know that's a wrong syntax, I was just showing an example. Yes, here it will work, but if you want to initialize only some fields (poor man's keyword arguments), you can't use the default constructor.easily fixable by using FunctionLiterals.
Jul 13 2017
On Thursday, 13 July 2017 at 18:09:46 UTC, H. S. Teoh wrote: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? }in this case, I'd expect something like: error: ambiguous struct definition, could match process(Foo) or process(Oof)
Jul 13 2017
On Thu, Jul 13, 2017 at 06:48:27PM +0000, JN via Digitalmars-d-learn wrote:On Thursday, 13 July 2017 at 18:09:46 UTC, H. S. Teoh wrote:File an enhancement request: https://issues.dlang.org/enter_bug.cgi You never know, we may be able to convince Walter to add this at some point. :-P T -- English has the lovely word "defenestrate", meaning "to execute by throwing someone out a window", or more recently "to remove Windows from a computer and replace it with something useful". :-) -- John CowanIt'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? }in this case, I'd expect something like: error: ambiguous struct definition, could match process(Foo) or process(Oof)
Jul 13 2017
On 2017-07-13 20:07, JN 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...https://github.com/dlang/DIPs/pull/71 -- /Jacob Carlborg
Jul 13 2017