digitalmars.D.learn - Error: 'this' is only defined in non-static member functions
- A Guy With a Question (13/13) Nov 22 2017 I have an interface where I have a classes embedded in it's scope
- Steven Schveighoffer (11/29) Nov 22 2017 Hm... not sure how this works for inner classes of Interfaces, but if
- Steven Schveighoffer (3/7) Nov 22 2017 All that being said, the error message is quite lousy.
- A Guy With a Question (7/14) Nov 22 2017 Yup that worked. Thanks!
- A Guy With a Question (3/19) Nov 22 2017 Clearly static has a slightly different meaning in D.
- Jonathan M Davis (14/30) Nov 22 2017 A static, nested class does not have access to the class that it's in. I...
- A Guy With a Question (4/8) Nov 22 2017 Hmmm...now you have me very intrigued. What is a use-case where
- Jonathan M Davis (17/26) Nov 22 2017 It would make sense with something like the nodes of a linked list if th...
- Jacob Carlborg (25/41) Nov 23 2017 Yeah, it's very similar in D and Java.
- Steven Schveighoffer (4/24) Nov 24 2017 The intent is exactly for porting Java code that uses either anonymous
- Mike Parker (13/17) Nov 22 2017 Actually, they work exactly like Java's static nested classes in
- Jonathan M Davis (5/22) Nov 22 2017 I thought that that was the case, but my Java is so rusty that I wasn't
I have an interface where I have a classes embedded in it's scope (trying to create something for the classes that implement the interface can use for unittesting). interface IExample { // stuff ... class Tester { } } I'm trying to make an instance of it like: auto tester = new IExample.Tester(); And I get the above error.
Nov 22 2017
On 11/22/17 5:19 PM, A Guy With a Question wrote:I have an interface where I have a classes embedded in it's scope (trying to create something for the classes that implement the interface can use for unittesting). interface IExample { // stuff ... class Tester { } } I'm trying to make an instance of it like: auto tester = new IExample.Tester(); And I get the above error.Hm... not sure how this works for inner classes of Interfaces, but if it's anything like inner classes of classes, then they have a hidden 'outer' pointer pointing at the owning instance of the class. This allows access to the outer class's members. So you need an instance to instantiate. I bet it's the same for interfaces. What you probably want is a static class, which will remove that connection: static class Tester ... -Steve
Nov 22 2017
On 11/22/17 5:36 PM, Steven Schveighoffer wrote:This allows access to the outer class's members. So you need an instance to instantiate. I bet it's the same for interfaces.All that being said, the error message is quite lousy. -Steve
Nov 22 2017
On Wednesday, 22 November 2017 at 22:37:46 UTC, Steven Schveighoffer wrote:On 11/22/17 5:36 PM, Steven Schveighoffer wrote:Yup that worked. Thanks! Out of curiosity, what does static mean in that context? When I think of a static class I think of them in the context of Java or namespaces that you can't directly import the contents of.This allows access to the outer class's members. So you need an instance to instantiate. I bet it's the same for interfaces.All that being said, the error message is quite lousy. -Steve
Nov 22 2017
On Wednesday, 22 November 2017 at 22:45:53 UTC, A Guy With a Question wrote:On Wednesday, 22 November 2017 at 22:37:46 UTC, Steven Schveighoffer wrote:Clearly static has a slightly different meaning in D.On 11/22/17 5:36 PM, Steven Schveighoffer wrote:Yup that worked. Thanks! Out of curiosity, what does static mean in that context? When I think of a static class I think of them in the context of Java like namespaces that you can't directly import the contents of.This allows access to the outer class's members. So you need an instance to instantiate. I bet it's the same for interfaces.All that being said, the error message is quite lousy. -Steve
Nov 22 2017
On Wednesday, November 22, 2017 22:45:53 A Guy With a Question via Digitalmars-d-learn wrote:On Wednesday, 22 November 2017 at 22:37:46 UTC, Steven Schveighoffer wrote:A static, nested class does not have access to the class that it's in. It's basically a normal class that's namespaced within another class, whereas non-static, nested class is associated with a specific instance of the class and has access to that class instance via its outer member. https://dlang.org/spec/class.html#nested http://ddili.org/ders/d.en/nested.html Similarly, if a struct, class, or function is nested within a function, and it is not marked with static, then it has access to the function that it's in, whereas if it's marked with static, then it acts more like a normal struct/class/function and does not have access to the function that it's in and is just namespaced within that function. - Jonathan M DavisOn 11/22/17 5:36 PM, Steven Schveighoffer wrote:Yup that worked. Thanks! Out of curiosity, what does static mean in that context? When I think of a static class I think of them in the context of Java or namespaces that you can't directly import the contents of.This allows access to the outer class's members. So you need an instance to instantiate. I bet it's the same for interfaces.All that being said, the error message is quite lousy. -Steve
Nov 22 2017
here as non-static, nested class is associated with a specific instance of the class and has access to that class instance via its outer member. - Jonathan M DavisHmmm...now you have me very intrigued. What is a use-case where you'd want to use a non-static embedded class? Sorry if I'm asking too many questions. But there's a lot to dig into with this language.
Nov 22 2017
On Thursday, November 23, 2017 00:17:46 A Guy With a Question via Digitalmars-d-learn wrote:It would make sense with something like the nodes of a linked list if they needed access to the container for some reason. Pretty much any case where a an instance of a nested class is going to be associated with a specific instance of its parent class and needs access to it would be a canditate. It's not that uncommon to see cases in C++ or Java where you'd pass a pointer to the "parent" to an instance of a nested class when it's created, and having outer built-in is kind of like that. Personally, I've never had a use for it. I don't even use classes much in D, since I rarely need inheritance. And as I understand it, most D programs don't use classes very heavily for that very reason. So, I have no idea how common it is to use nested classes in this manner, but I expect that someone has found it useful at some point. I thought that this meaning of static for nested classes came from Java, but it's been a while since I've done much with Java, so I don't know. - Jonathan M Davishere as non-static, nested class is associated with a specific instance of the class and has access to that class instance via its outer member. - Jonathan M DavisHmmm...now you have me very intrigued. What is a use-case where you'd want to use a non-static embedded class? Sorry if I'm asking too many questions. But there's a lot to dig into with this language.
Nov 22 2017
On 2017-11-23 01:35, Jonathan M Davis wrote:It would make sense with something like the nodes of a linked list if they needed access to the container for some reason. Pretty much any case where a an instance of a nested class is going to be associated with a specific instance of its parent class and needs access to it would be a canditate. It's not that uncommon to see cases in C++ or Java where you'd pass a pointer to the "parent" to an instance of a nested class when it's created, and having outer built-in is kind of like that. Personally, I've never had a use for it. I don't even use classes much in D, since I rarely need inheritance. And as I understand it, most D programs don't use classes very heavily for that very reason. So, I have no idea how common it is to use nested classes in this manner, but I expect that someone has found it useful at some point. I thought that this meaning of static for nested classes came from Java, but it's been a while since I've done much with Java, so I don't know.Yeah, it's very similar in D and Java. Another example that comes from Java (before version 8) is to have a class with a nested anonymous class that implements an interface. It's very common in Java for event handling or similar actions. Other languages like D or Java 8 would use a delegate/lambda for the same thing. Something like: class WindowController { Button button; Window window; this() { button = new Button; window = new Window; button.onClick = new class() Clickable { window.close(); }; } } The anonymous class could have been a named class as well and to be able to access the "window" instance variable of the controller it needs to have access to the outer context and cannot be static. -- /Jacob Carlborg
Nov 23 2017
On 11/22/17 7:35 PM, Jonathan M Davis wrote:On Thursday, November 23, 2017 00:17:46 A Guy With a Question via Digitalmars-d-learn wrote:The intent is exactly for porting Java code that uses either anonymous classes or inner classes. -SteveIt would make sense with something like the nodes of a linked list if they needed access to the container for some reason. Pretty much any case where a an instance of a nested class is going to be associated with a specific instance of its parent class and needs access to it would be a canditate. It's not that uncommon to see cases in C++ or Java where you'd pass a pointer to the "parent" to an instance of a nested class when it's created, and having outer built-in is kind of like that.here as non-static, nested class is associated with a specific instance of the class and has access to that class instance via its outer member. - Jonathan M DavisHmmm...now you have me very intrigued. What is a use-case where you'd want to use a non-static embedded class? Sorry if I'm asking too many questions. But there's a lot to dig into with this language.
Nov 24 2017
On Wednesday, 22 November 2017 at 22:45:53 UTC, A Guy With a Question wrote:Out of curiosity, what does static mean in that context? When I think of a static class I think of them in the context of Java like namespaces that you can't directly import the contents of.Actually, they work exactly like Java's static nested classes in this context. class OuterClass { ... static class StaticNestedClass { ... } } OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass(); https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
Nov 22 2017
On Thursday, November 23, 2017 00:49:33 Mike Parker via Digitalmars-d-learn wrote:On Wednesday, 22 November 2017 at 22:45:53 UTC, A Guy With a Question wrote:I thought that that was the case, but my Java is so rusty that I wasn't sure. - Jonathan M DavisOut of curiosity, what does static mean in that context? When I think of a static class I think of them in the context of Java like namespaces that you can't directly import the contents of.Actually, they work exactly like Java's static nested classes in this context. class OuterClass { ... static class StaticNestedClass { ... } } OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass(); https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
Nov 22 2017