www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Silly struct behaviour

reply JN <666total wp.pl> writes:
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
next sibling parent reply "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
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
next sibling parent reply JN <666total wp.pl> writes:
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
parent Stefan Koch <uplink.coder googlemail.com> writes:
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
prev sibling parent reply JN <666total wp.pl> writes:
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
parent "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
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:
 
 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)
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 Cowan
Jul 13 2017
prev sibling parent Jacob Carlborg <doob me.com> writes:
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