www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Linker error

reply Anonymous <noemail gmail.com> writes:
Why does the following give a linker error?

If I change static Note[0] empty; to static Note[] empty;, all is 
well.
Or if I leave it as Note[0] empty; and don't use it in getNotes, 
all is well.

struct Note
{
	string topic;
	string content;
}

class NoteStore
{	
	
	Note[][string] store;
	
	static Note[0] empty;
	
	Note[] getNotes(string id)
	{
		return (id in store) ? store[id] : empty;
	}	
}

void main() {}

dmd --version
DMD32 D Compiler v2.070.0
Windows 10
Jun 05 2016
next sibling parent reply Anonymous <noemail gmail.com> writes:
On Sunday, 5 June 2016 at 18:30:25 UTC, Anonymous wrote:
 Why does the following give a linker error?

 If I change static Note[0] empty; to static Note[] empty;, all 
 is well.
 Or if I leave it as Note[0] empty; and don't use it in 
 getNotes, all is well.

 struct Note
 {
 	string topic;
 	string content;
 }

 class NoteStore
 {	
 	
 	Note[][string] store;
 	
 	static Note[0] empty;
 	
 	Note[] getNotes(string id)
 	{
 		return (id in store) ? store[id] : empty;
 	}	
 }

 void main() {}

 dmd --version
 DMD32 D Compiler v2.070.0
 Windows 10
Should have included: OPTLINK (R) for Win32 Release 8.00.17 Copyright (C) Digital Mars 1989-2013 All rights reserved. http://www.digitalmars.com/ctg/optlink.html ns.obj(ns) Offset 0BA0AH Record Type 009D Error 16: Index Range --- errorlevel 1
Jun 05 2016
parent reply docandrew <x x.com> writes:
On Sunday, 5 June 2016 at 18:36:13 UTC, Anonymous wrote:
 On Sunday, 5 June 2016 at 18:30:25 UTC, Anonymous wrote:
 [...]
Should have included: OPTLINK (R) for Win32 Release 8.00.17 Copyright (C) Digital Mars 1989-2013 All rights reserved. http://www.digitalmars.com/ctg/optlink.html ns.obj(ns) Offset 0BA0AH Record Type 009D Error 16: Index Range --- errorlevel 1
Hmm, on OSX w/ dmd v2.071.0 I'm unable to duplicate. Can you try upgrading to v2.071 and see if that works? -Jon
Jun 05 2016
parent Anonymous <noemail gmail.com> writes:
On Sunday, 5 June 2016 at 18:45:36 UTC, docandrew wrote:
 On Sunday, 5 June 2016 at 18:36:13 UTC, Anonymous wrote:
 On Sunday, 5 June 2016 at 18:30:25 UTC, Anonymous wrote:
 [...]
Should have included: OPTLINK (R) for Win32 Release 8.00.17 Copyright (C) Digital Mars 1989-2013 All rights reserved. http://www.digitalmars.com/ctg/optlink.html ns.obj(ns) Offset 0BA0AH Record Type 009D Error 16: Index Range --- errorlevel 1
Hmm, on OSX w/ dmd v2.071.0 I'm unable to duplicate. Can you try upgrading to v2.071 and see if that works? -Jon
I upgraded, but I still get the same error: dmd ns.d OPTLINK (R) for Win32 Release 8.00.17 Copyright (C) Digital Mars 1989-2013 All rights reserved. http://www.digitalmars.com/ctg/optlink.html ns.obj(ns) Offset 00678H Record Type 009D Error 16: Index Range --- errorlevel 1 dmd --version DMD32 D Compiler v2.071.0 Copyright (c) 1999-2015 by Digital Mars written by Walter Bright
Jun 05 2016
prev sibling parent reply Andrej Mitrovic via Digitalmars-d-learn writes:
On 6/5/16, Anonymous via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> wrote:
 	static Note[0] empty;
 	
 	Note[] getNotes(string id)
 	{
 		return (id in store) ? store[id] : empty;
 	}	
It's likely an accepts-invalid bug, meaning it should be a compiler error instead. I don't think it makes sense that the compiler tries to slice a fixed-length array of length zero.. tho perhaps it should just equate that to returning null. In any case you can return `null` instead of "empty". Fixed-length arrays of length zero aren't really all that well-defined. Some would say they make no sense, but there is a weird benefit to them when used with the built-in hashmaps (a void[0] value type wouldn't allocate memory, AFAIR and if that is still true).
Jun 05 2016
parent reply Anonymous <noemail gmail.com> writes:
On Sunday, 5 June 2016 at 20:16:54 UTC, Andrej Mitrovic wrote:
 On 6/5/16, Anonymous via Digitalmars-d-learn 
 <digitalmars-d-learn puremagic.com> wrote:
 	static Note[0] empty;
 
 	Note[] getNotes(string id)
 	{
 		return (id in store) ? store[id] : empty;
 	}
It's likely an accepts-invalid bug, meaning it should be a compiler error instead. I don't think it makes sense that the compiler tries to slice a fixed-length array of length zero.. tho perhaps it should just equate that to returning null. In any case you can return `null` instead of "empty". Fixed-length arrays of length zero aren't really all that well-defined. Some would say they make no sense, but there is a weird benefit to them when used with the built-in hashmaps (a void[0] value type wouldn't allocate memory, AFAIR and if that is still true).
Should I report this as a dmd bug then? Not sure where / how to do that. I think I'll just let it go; I was able to work passed it anyway using "static Note[] empty;", and `null` works too. Is either one better? By the way, this is from an example I found in "D Web Development" by Kai Nacke.
Jun 05 2016
parent reply Andrej Mitrovic via Digitalmars-d-learn writes:
On 6/5/16, Anonymous via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> wrote:
 Should I report this as a dmd bug then? Not sure where / how to
 do that.
You can report it here: https://issues.dlang.org
 I think I'll just let it go; I was able to work passed it anyway
 using "static Note[] empty;", and `null` works too. Is either one
 better?
null is simpler from a reader's perspective. :)
 By the way, this is from an example I found in "D Web
 Development" by Kai Nacke.
Interesting that they would use such code in the book. Which chapter is it?
Jun 05 2016
parent reply Anonymous <noemail gmail.com> writes:
On Sunday, 5 June 2016 at 21:16:36 UTC, Andrej Mitrovic wrote:
 On 6/5/16, Anonymous via Digitalmars-d-learn 
 <digitalmars-d-learn puremagic.com> wrote:
 Should I report this as a dmd bug then? Not sure where / how 
 to do that.
You can report it here: https://issues.dlang.org
 I think I'll just let it go; I was able to work passed it 
 anyway using "static Note[] empty;", and `null` works too. Is 
 either one better?
null is simpler from a reader's perspective. :)
 By the way, this is from an example I found in "D Web 
 Development" by Kai Nacke.
Interesting that they would use such code in the book. Which chapter is it?
Thanks. I agree it's simpler and switched to `null`. The example is in chapter 3.
Jun 05 2016
parent reply docandrew <x x.com> writes:
On Sunday, 5 June 2016 at 21:26:56 UTC, Anonymous wrote:
 On Sunday, 5 June 2016 at 21:16:36 UTC, Andrej Mitrovic wrote:
 On 6/5/16, Anonymous via Digitalmars-d-learn 
 <digitalmars-d-learn puremagic.com> wrote:
 Should I report this as a dmd bug then? Not sure where / how 
 to do that.
You can report it here: https://issues.dlang.org
 I think I'll just let it go; I was able to work passed it 
 anyway using "static Note[] empty;", and `null` works too. Is 
 either one better?
null is simpler from a reader's perspective. :)
 By the way, this is from an example I found in "D Web 
 Development" by Kai Nacke.
Interesting that they would use such code in the book. Which chapter is it?
Thanks. I agree it's simpler and switched to `null`. The example is in chapter 3.
That it works on OSX but not on Windows makes me think it should definitely be reported as a bug. -Jon
Jun 05 2016
parent Anonymous <noemail gmail.com> writes:
On Monday, 6 June 2016 at 02:21:03 UTC, docandrew wrote:
 On Sunday, 5 June 2016 at 21:26:56 UTC, Anonymous wrote:
 On Sunday, 5 June 2016 at 21:16:36 UTC, Andrej Mitrovic wrote:
 On 6/5/16, Anonymous via Digitalmars-d-learn 
 <digitalmars-d-learn puremagic.com> wrote:
 [...]
You can report it here: https://issues.dlang.org
 [...]
null is simpler from a reader's perspective. :)
 [...]
Interesting that they would use such code in the book. Which chapter is it?
Thanks. I agree it's simpler and switched to `null`. The example is in chapter 3.
That it works on OSX but not on Windows makes me think it should definitely be reported as a bug. -Jon
https://issues.dlang.org/show_bug.cgi?id=16129
Jun 05 2016