Thread overview
Silly struct behaviour
Jul 13, 2017
JN
Jul 13, 2017
H. S. Teoh
Jul 13, 2017
JN
Jul 13, 2017
Stefan Koch
Jul 13, 2017
JN
Jul 13, 2017
H. S. Teoh
Jul 14, 2017
Jacob Carlborg
July 13, 2017
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...
July 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)
July 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.


July 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.
July 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)
July 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:
> > 
> > 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
July 14, 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