Re: Error: 'this' is only defined in non-static member functions

2017-11-24 Thread Steven Schveighoffer via Digitalmars-d-learn

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:

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 Davis


Hmmm...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.


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.


The intent is exactly for porting Java code that uses either anonymous 
classes or inner classes.


-Steve


Re: Error: 'this' is only defined in non-static member functions

2017-11-23 Thread Jacob Carlborg via Digitalmars-d-learn

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


Re: Error: 'this' is only defined in non-static member functions

2017-11-22 Thread Jonathan M Davis via Digitalmars-d-learn
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:
> > 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 C# where they can't be instantiated and where they are more
> > 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

I thought that that was the case, but my Java is so rusty that I wasn't
sure.

- Jonathan M Davis



Re: Error: 'this' is only defined in non-static member functions

2017-11-22 Thread Mike Parker via Digitalmars-d-learn
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 
or C# where they can't be instantiated and where they are more 
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


Re: Error: 'this' is only defined in non-static member functions

2017-11-22 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, November 23, 2017 00:17:46 A Guy With a Question via 
Digitalmars-d-learn wrote:
> > 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 Davis
>
> Hmmm...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.

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 Davis



Re: Error: 'this' is only defined in non-static member functions

2017-11-22 Thread A Guy With a Question via Digitalmars-d-learn
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 Davis


Hmmm...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.


Re: Error: 'this' is only defined in non-static member functions

2017-11-22 Thread Jonathan M Davis via Digitalmars-d-learn
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:
> > 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
>
> 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
> C# where they can't be instantiated and where they are more like
> namespaces that you can't directly import the contents of.

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 Davis



Re: Error: 'this' is only defined in non-static member functions

2017-11-22 Thread A Guy With a Question via Digitalmars-d-learn
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:

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


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 C# where they can't be instantiated and where they are more 
like namespaces that you can't directly import the contents of.


Clearly static has a slightly different meaning in D.


Re: Error: 'this' is only defined in non-static member functions

2017-11-22 Thread A Guy With a Question via Digitalmars-d-learn
On Wednesday, 22 November 2017 at 22:37:46 UTC, Steven 
Schveighoffer wrote:

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


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 
C# where they can't be instantiated and where they are more like 
namespaces that you can't directly import the contents of.


Re: Error: 'this' is only defined in non-static member functions

2017-11-22 Thread Steven Schveighoffer via Digitalmars-d-learn

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


Re: Error: 'this' is only defined in non-static member functions

2017-11-22 Thread Steven Schveighoffer via Digitalmars-d-learn

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


Error: 'this' is only defined in non-static member functions

2017-11-22 Thread A Guy With a Question via Digitalmars-d-learn
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.


Error: 'this' is only defined in non-static member functions, not main

2015-07-29 Thread remi thebault via Digitalmars-d-learn

Hello

I have this weird error trying to achieve something simple:


module list_test;

// import wayland.util;


template Id(alias a) { alias Id = a; }


template ParentOf(alias member)
{
alias ParentOf = Id!(__traits(parent, member));
}


template wl_container_of(alias member)
{
ParentOf!(member)*
get(wl_list* ptr)
{
return 
cast(ParentOf!(member)*)(cast(ptrdiff_t)(ptr)-member.offsetof);

}
}


struct wl_list {
wl_list *prev;
wl_list *next;
}


struct item {
int num;
wl_list link;

this(int num) {
this.num = num;
}
}

int main (string[] args)
{
auto i1 = item(1);
auto i2 = item(2);
auto i3 = item(3);

wl_list lst;
// wl_list_init(lst);
// wl_list_insert(lst, i1.link);
// wl_list_insert(lst, i2.link);
// wl_list_insert(i2.link, i3.link);

item *it = wl_container_of!(item.link).get(i2.link); // 
error on this line


return 0;
}


If I change the definition of wl_container_of to:
template wl_container_of(alias member)
{
ParentOf!(member)*
wl_container_of(wl_list* ptr)
{
return 
cast(ParentOf!(member)*)(cast(ptrdiff_t)(ptr)-member.offsetof);

}
}

and call to:
wl_container_of!(item.link)(i2.link);

I now get a different error:
Error: need 'this' for 'wl_container_of' of type 'pure nothrow 
@nogc @system item*(wl_list* ptr)'



I run dmd v2.067 on linux 64bits.

Any idea?

thanks
Rémi




Re: Error: 'this' is only defined in non-static member functions, not main

2015-07-29 Thread anonymous via Digitalmars-d-learn

On Wednesday, 29 July 2015 at 21:33:16 UTC, remi thebault wrote:

Hello

I have this weird error trying to achieve something simple:


That's far from simple.

Here's a reduction:


template wl_container_of(alias member)
{
size_t get() {return member.offsetof;}
}

struct item { int link; }

void main()
{
wl_container_of!(item.link).get(); /* Error: 'this' is only 
defined in non-static member functions, not main */

}


I'm not sure what's going on here, if this should or shouldn't 
work. The error message isn't exactly good.


Slapping `static` on `get` seems to make it work:

static size_t get() {return member.offsetof;}


I guess the compiler thinks that since `item.link` is an instance 
member, it needs a `this`. And then `get` would need a `this` too 
as it's using `item.link` via `member`. When the compiler sees 
that there is no `this`, it errors out.


An explicit `static` forces the compiler to assume no `this`. And 
it would error out if you actually tried to make use of it.


Naively, I'd think the compiler should be able to figure that out 
itself.


Re: Error: 'this' is only defined in non-static member functions, not main

2015-07-29 Thread remi thebault via Digitalmars-d-learn

On Wednesday, 29 July 2015 at 22:12:38 UTC, anonymous wrote:

Slapping `static` on `get` seems to make it work:

static size_t get() {return member.offsetof;}



Good slap, thanks!
you solved my problem



I guess the compiler thinks that since `item.link` is an 
instance member, it needs a `this`. And then `get` would need a 
`this` too as it's using `item.link` via `member`. When the 
compiler sees that there is no `this`, it errors out.


An explicit `static` forces the compiler to assume no `this`. 
And it would error out if you actually tried to make use of it.


Naively, I'd think the compiler should be able to figure that 
out itself.


I guess that is a dmd bug. I'll fill a report in case of.


Re: Error: 'this' is only defined in non-static member functions, not parse

2012-01-17 Thread Matej Nanut
I will try to remove all snippets in my code that aren't relevant but still
exhibit the issue, when I find the time. What I forgot to mention is that
this error appeared when I did some so-called refactoring; I moved a
nested class out of its parent, since I wanted it visible on the outside.

I moved it back now and reference it by Parent.Child.stuff, which is
just as good, and the error isn't there anymore, but I still don't understand
it. I must have missed some variable renaming or something. Or I was
just plain sloppy. As mentioned I will post a complete snippet with the
error sometime until friday.

On 17 January 2012 02:33, Timon Gehr timon.g...@gmx.ch wrote:
 On 01/17/2012 12:49 AM, Matej Nanut wrote:

 Hey everyone,

 I, once again, have a problem with an error I can't seem to figure out!

 The situation:
 - a class, inherited by five other classes;
 - the class having a static function which returns one
   if its subclasses depending on the input of a string.

 Something like this:

 class Node
 {
   static Node parse(ref string s)
   {
     /* Get value to switch by, an enum. */
     auto switchable = /* ... */;
     final switch (switchable)
     {
       case Blah.one: return new OneNode(s);
       case Blah.two: return new TwoNode(s);
     /* ... */
     }
   }
 }

 And I get the mentioned error. I don't understand it:
 is it saying I'm using `this' in a static member function
 called `parse'? Am I insane; where am I referencing it?

 The other classes are in this form:

 class OneNode : Node
 {
   /* ... stuff ... */
   this(ref string s)
   {
     /* Does stuff with `s'. */
   }
 }

 Do you need more information?

 Yes; It is extremely hard to solve the problem when there is no code snippet
 given which exhibits the problematic behavior in question.



Re: Error: 'this' is only defined in non-static member functions, not parse

2012-01-17 Thread Alex Rønne Petersen

On 17-01-2012 12:23, Matej Nanut wrote:

I will try to remove all snippets in my code that aren't relevant but still
exhibit the issue, when I find the time. What I forgot to mention is that
this error appeared when I did some so-called refactoring; I moved a
nested class out of its parent, since I wanted it visible on the outside.

I moved it back now and reference it by Parent.Child.stuff, which is
just as good, and the error isn't there anymore, but I still don't understand
it. I must have missed some variable renaming or something. Or I was
just plain sloppy. As mentioned I will post a complete snippet with the
error sometime until friday.

On 17 January 2012 02:33, Timon Gehrtimon.g...@gmx.ch  wrote:

On 01/17/2012 12:49 AM, Matej Nanut wrote:


Hey everyone,

I, once again, have a problem with an error I can't seem to figure out!

The situation:
- a class, inherited by five other classes;
- the class having a static function which returns one
   if its subclasses depending on the input of a string.

Something like this:

class Node
{
   static Node parse(ref string s)
   {
 /* Get value to switch by, an enum. */
 auto switchable = /* ... */;
 final switch (switchable)
 {
   case Blah.one: return new OneNode(s);
   case Blah.two: return new TwoNode(s);
 /* ... */
 }
   }
}

And I get the mentioned error. I don't understand it:
is it saying I'm using `this' in a static member function
called `parse'? Am I insane; where am I referencing it?

The other classes are in this form:

class OneNode : Node
{
   /* ... stuff ... */
   this(ref string s)
   {
 /* Does stuff with `s'. */
   }
}

Do you need more information?


Yes; It is extremely hard to solve the problem when there is no code snippet
given which exhibits the problematic behavior in question.



DustMite is good for making test cases: 
https://github.com/CyberShadow/DustMite


--
- Alex


Re: Error: 'this' is only defined in non-static member functions, not parse

2012-01-17 Thread H. S. Teoh
On Tue, Jan 17, 2012 at 12:23:55PM +0100, Matej Nanut wrote:
 I will try to remove all snippets in my code that aren't relevant but still
 exhibit the issue, when I find the time. What I forgot to mention is that
 this error appeared when I did some so-called refactoring; I moved a
 nested class out of its parent, since I wanted it visible on the outside.
[...]

This may be the cause of your trouble. If the nested class references
members in the outer class, then moving it outside will break it, since
it won't have an outer scope anymore.


T

-- 
Only boring people get bored. -- JM


Re: Error: 'this' is only defined in non-static member functions, not parse

2012-01-17 Thread Matej Nanut
On 17 January 2012 16:54, H. S. Teoh hst...@quickfur.ath.cx wrote:
 This may be the cause of your trouble. If the nested class references
 members in the outer class, then moving it outside will break it, since
 it won't have an outer scope anymore.


 T

 --
 Only boring people get bored. -- JM

That was my guess too — but I'm not referencing the outer class. The
outer class is just using instances of the inner one. Also, the line number
of the error points to ‘new’ statements in the static method. (The calls
which instantiate subclasses of the inner class.) If I do return null it
works as well, without complaining. So it's not a referencing issue I think.

As you all seem eager to help, I will copy the entire class, without
subclasses, to here. I will be grateful for any comments regarding the
current issue at hand or about the code in general.

The ‘new’-ed Nodes are defined like ‘class CostNode : Node { ... }’.

Another note, the outer class is an inner class of another class as well,
if that makes a difference. Also, the outer class isn't really a class, it's
a struct, but renaming that to ‘class’ doesn't change anything either.

 code begin 
class Node
{
static Node parse(ref string line)
{
string mnemonic = munch(line, A-Z);
line = line.stripLeft();
auto op = mnemonic in mnemonics;
if (!op)
throw new Exception(Unknown mnemonic: ` ~ mnemonic ~');

final switch (*op)
{
case NodeType.COST:
return new CostNode(line);
case NodeType.PAUSE:
return new PauseNode(line);
case NodeType.COLDR:
return new ColDrNode(line);
case NodeType.COLRA:
return new ColRaNode(line);
case NodeType.DROP:
return new DropNode(line);
case NodeType.RAISE:
return new RaiseNode(line);
}

/* Doing something like `return new Node()' doesn't work either.
 * Only `return null' works here. */
}

enum NodeType : ubyte
{
COST,
PAUSE,
COLDR,
COLRA,
DROP,
RAISE
}

static immutable NodeType[string] mnemonics;
static this()
{
mnemonics = [
COST  : NodeType.COST,
PAUSE : NodeType.PAUSE,
COLDR : NodeType.COLDR,
COLRA : NodeType.COLRA,
DROP  : NodeType.DROP,
RAISE : NodeType.RAISE
];
}
}

 code end 


Re: Error: 'this' is only defined in non-static member functions, not parse

2012-01-17 Thread Timon Gehr

On 01/17/2012 06:02 PM, Matej Nanut wrote:

On 17 January 2012 16:54, H. S. Teohhst...@quickfur.ath.cx  wrote:

This may be the cause of your trouble. If the nested class references
members in the outer class, then moving it outside will break it, since
it won't have an outer scope anymore.


T

--
Only boring people get bored. -- JM


That was my guess too — but I'm not referencing the outer class. The
outer class is just using instances of the inner one. Also, the line number
of the error points to ‘new’ statements in the static method. (The calls
which instantiate subclasses of the inner class.) If I do return null it
works as well, without complaining. So it's not a referencing issue I think.

As you all seem eager to help, I will copy the entire class, without
subclasses, to here. I will be grateful for any comments regarding the
current issue at hand or about the code in general.

The ‘new’-ed Nodes are defined like ‘class CostNode : Node { ... }’.

Another note, the outer class is an inner class of another class as well,
if that makes a difference. Also, the outer class isn't really a class, it's
a struct, but renaming that to ‘class’ doesn't change anything either.

 code begin 
 class Node
 {
 static Node parse(ref string line)
 {
 string mnemonic = munch(line, A-Z);
 line = line.stripLeft();
 auto op = mnemonic in mnemonics;
 if (!op)
 throw new Exception(Unknown mnemonic: ` ~ mnemonic ~');

 final switch (*op)
 {
 case NodeType.COST:
 return new CostNode(line);
 case NodeType.PAUSE:
 return new PauseNode(line);
 case NodeType.COLDR:
 return new ColDrNode(line);
 case NodeType.COLRA:
 return new ColRaNode(line);
 case NodeType.DROP:
 return new DropNode(line);
 case NodeType.RAISE:
 return new RaiseNode(line);
 }

 /* Doing something like `return new Node()' doesn't work 
either.
  * Only `return null' works here. */
 }

 enum NodeType : ubyte
 {
 COST,
 PAUSE,
 COLDR,
 COLRA,
 DROP,
 RAISE
 }

 static immutable NodeType[string] mnemonics;
 static this()
 {
 mnemonics = [
 COST  : NodeType.COST,
 PAUSE : NodeType.PAUSE,
 COLDR : NodeType.COLDR,
 COLRA : NodeType.COLRA,
 DROP  : NodeType.DROP,
 RAISE : NodeType.RAISE
 ];
 }
 }

 code end 


I'm quite sure that the error in your code occurs for the same reason as 
in the following code snippet:


class C{
class D{}
static make(){return new D();} // error
}

You can fix it by making D static:

class C{
static class D{}
static make(){return new D();} // ok
}

The reason is that non-static inner classes have an implicit 'outer' 
property that links to the class it was created with. Therefore, to 
construct them inside a member function, the implicit 'this' pointer is 
needed. If the 'outer' property is actually unwanted, it is best to 
declare inner classes as static.


Re: Error: 'this' is only defined in non-static member functions, not parse

2012-01-17 Thread Matej Nanut
On 17 January 2012 18:29, Timon Gehr timon.g...@gmx.ch wrote:
 I'm quite sure that the error in your code occurs for the same reason as in
 the following code snippet:

 class C{
    class D{}
    static make(){return new D();} // error
 }

 You can fix it by making D static:

 class C{
    static class D{}
    static make(){return new D();} // ok
 }

 The reason is that non-static inner classes have an implicit 'outer'
 property that links to the class it was created with. Therefore, to
 construct them inside a member function, the implicit 'this' pointer is
 needed. If the 'outer' property is actually unwanted, it is best to declare
 inner classes as static.

Yes! If I move the class and its subclasses out of its outer class, and declare
them all static, it works!

Note that your `make' function is being called within class `D' in my example,
if I replace the names. However, the same thing applies.

Your explanation was nice, but now I'd like to know what the difference of a
non-static vs. a static class is, if they're defined top-level? Or are they then
the same? I don't expect anyone to thoroughly explain things to me, but
pointing out a good source, like a link or a book, would be really helpful.

I lack general knowledge in the OOP area and must really learn more about
it, as I've always been programming in C and could easily get away with it
as we were doing small-ish programs at university.


Re: Error: 'this' is only defined in non-static member functions, not parse

2012-01-17 Thread H. S. Teoh
On Tue, Jan 17, 2012 at 06:58:55PM +0100, Matej Nanut wrote:
[...]
 Your explanation was nice, but now I'd like to know what the
 difference of a non-static vs. a static class is, if they're defined
 top-level? Or are they then the same? I don't expect anyone to
 thoroughly explain things to me, but pointing out a good source, like
 a link or a book, would be really helpful.
[...]

Andrei's book (The D Programming Language) is quite thorough in
explaining these D constructs. It's a highly recommended buy if you're
doing serious work in D.


T

-- 
The two rules of success: 1. Don't tell everything you know. -- YHL


Re: Error: 'this' is only defined in non-static member functions, not parse

2012-01-17 Thread Matej Nanut
On 17 January 2012 19:07, H. S. Teoh hst...@quickfur.ath.cx wrote:
 Andrei's book (The D Programming Language) is quite thorough in
 explaining these D constructs. It's a highly recommended buy if you're
 doing serious work in D.


 T

 --
 The two rules of success: 1. Don't tell everything you know. -- YHL

I've been thinking on getting that for a while now. How up to date is it?
Or does it explain such general concepts that I shouldn't be worried
about that at all? Everyone seems to be recommending it so I don't see
why I shouldn't get it. A free university period is also coming up, so that
might be a great way to spend my available time.

I'm definitely serious about learning and using D. I've been impressed with
it since I first saw it and I intend to do as much work with it as possible.
I'm not _doing_ any serious work with it yet, though. In fact, none of the work
I do could be considered very serious at all. :)


Re: Error: 'this' is only defined in non-static member functions, not parse

2012-01-17 Thread Timon Gehr

On 01/17/2012 07:13 PM, Matej Nanut wrote:

On 17 January 2012 19:07, H. S. Teohhst...@quickfur.ath.cx  wrote:

Andrei's book (The D Programming Language) is quite thorough in
explaining these D constructs. It's a highly recommended buy if you're
doing serious work in D.


T

--
The two rules of success: 1. Don't tell everything you know. -- YHL


I've been thinking on getting that for a while now. How up to date is it?


I think it is mostly in a good state. There are a few errata and some 
unmentioned features because D is/was somewhat of a moving/improving 
target. On the other hand, many recent bug fixes were targeted at making 
the implementation consistent with the specification in TDPL.



Or does it explain such general concepts that I shouldn't be worried
about that at all?


I don't think you need to be worried, just be prepared that a few code 
samples may not compile without minimal fixes.



Everyone seems to be recommending it so I don't see
why I shouldn't get it. A free university period is also coming up, so that
might be a great way to spend my available time.



Indeed. When I read it, I have found my time well spent. It is very well 
written.



I'm definitely serious about learning and using D. I've been impressed with
it since I first saw it and I intend to do as much work with it as possible.
I'm not _doing_ any serious work with it yet, though. In fact, none of the work
I do could be considered very serious at all. :)




Re: Error: 'this' is only defined in non-static member functions, not parse

2012-01-17 Thread Jonathan M Davis
On Tuesday, January 17, 2012 19:13:02 Matej Nanut wrote:
 I've been thinking on getting that for a while now. How up to date is it?
 Or does it explain such general concepts that I shouldn't be worried
 about that at all? Everyone seems to be recommending it so I don't see
 why I shouldn't get it. A free university period is also coming up, so that
 might be a great way to spend my available time.

If anything, it's _too_ up-to-date. There are a few relatively minor changes 
which have been made to the language since its release (e.g. weak vs strong 
purity and attribute inference for templated functions), but for the most 
part if TDPL doesn't match what dmd is doing, it's because features aren't 
fully implemented yet which should be (e.g. alias this works, but according to 
TDPL, you should be able to have multiple alias this-es per type - which you 
can't currently do). There has been a recent push though to fix the remaining 
issues where the compiler doesn't yet match TDPL.

I actually think that TDPL is the best programming language book that I've 
ever read. It's very well written and _way_ more informative than the rather 
sparse online documentation. I'd been programming in D for a while when I read 
it, and there were all kinds of stuff in there that I didn't know about. I 
really think that it's a must have for any serious D programmer.

- Jonathan M Davis

P.S. TDPL's errata is here: http://erdani.com/tdpl/errata/


Re: Error: 'this' is only defined in non-static member functions, not parse

2012-01-17 Thread H. S. Teoh
On Tue, Jan 17, 2012 at 07:13:02PM +0100, Matej Nanut wrote:
 On 17 January 2012 19:07, H. S. Teoh hst...@quickfur.ath.cx wrote:
  Andrei's book (The D Programming Language) is quite thorough in
  explaining these D constructs. It's a highly recommended buy if you're
  doing serious work in D.
[...]
 I've been thinking on getting that for a while now. How up to date is it?
 Or does it explain such general concepts that I shouldn't be worried
 about that at all? Everyone seems to be recommending it so I don't see
 why I shouldn't get it. A free university period is also coming up, so that
 might be a great way to spend my available time.

Well, D2 is a fast-evolving language, so any book can't possibly be 100%
up to date. :) But having said that, what's covered in the book is
pretty close to the current state of D2. The basics haven't changed, so
you don't really have to worry about that. When you get to the level
where it starts to matter, you probably don't need the book anymore (or
only need it for reference) anyway. :)


 I'm definitely serious about learning and using D. I've been impressed
 with it since I first saw it and I intend to do as much work with it
 as possible.

I've also been impressed with D since I first stumbled across it late
last year. I've been using C/C++ for almost 2 decades, but after tasting
D's power in a recent small project, I have to confess that I just can't
go back to C/C++ for my personal projects anymore. The more I use D the
more I like it.


 I'm not _doing_ any serious work with it yet, though. In fact, none of
 the work I do could be considered very serious at all.  :)

Isn't that how we all start out, though? Tinker with the language in toy
projects that eventually become the basis for something more serious.


T

-- 
2+2=4. 2*2=4. 2^2=4. Therefore, +, *, and ^ are the same operation.


Re: Error: 'this' is only defined in non-static member functions, not parse

2012-01-17 Thread Timon Gehr

On 01/17/2012 06:58 PM, Matej Nanut wrote:

On 17 January 2012 18:29, Timon Gehrtimon.g...@gmx.ch  wrote:

I'm quite sure that the error in your code occurs for the same reason as in
the following code snippet:

class C{
class D{}
static make(){return new D();} // error
}

You can fix it by making D static:

class C{
static class D{}
static make(){return new D();} // ok
}

The reason is that non-static inner classes have an implicit 'outer'
property that links to the class it was created with. Therefore, to
construct them inside a member function, the implicit 'this' pointer is
needed. If the 'outer' property is actually unwanted, it is best to declare
inner classes as static.


Yes! If I move the class and its subclasses out of its outer class, and declare
them all static, it works!

Note that your `make' function is being called within class `D' in my example,
if I replace the names. However, the same thing applies.

Your explanation was nice, but now I'd like to know what the difference of a
non-static vs. a static class is, if they're defined top-level? Or are they then
the same?


Indeed they are the same. Anything top-level is implicitly static in D.


I don't expect anyone to thoroughly explain things to me, but
pointing out a good source, like a link or a book, would be really helpful.



I don't know if there is any, but I can explain to you the difference 
between static and non-static nested classes in detail:


class A{
int x;
static class B{void echo(){writeln(x);}} // n.g.
}

class A{
int x;
class B{void echo(){writeln(x);}} // ok
}

In other words, non-static nested classes can reference non-static 
fields of the enclosing class. In order to provide that functionality, 
non-static nested classes need the implicit 'outer' field. The first 
snippet is effectively rewritten to something like the following:


class A{
int x;
class B{A __outer; void echo(){writeln(__outer.x);}
}

Therefore, for constructing a class instance of type A.B, an instance of 
A must be provided as an initializer for the 'outer' field. If an 
instance of B is created in a member of A, the 'this' pointer gets used 
(and hence is required to be present), but you can also do:


void main() {
auto a = new A;
auto b = a.new B; // construct an 'A.B' with 'a' in implicit 
'outer' field

a.x = 100;
b.echo(); // writes '100'
}

This is probably one of the more obscure features of D. =)


I lack general knowledge in the OOP area and must really learn more about
it, as I've always been programming in C and could easily get away with it
as we were doing small-ish programs at university.




Re: Error: 'this' is only defined in non-static member functions, not parse

2012-01-17 Thread H. S. Teoh
On Tue, Jan 17, 2012 at 08:25:28PM +0100, Timon Gehr wrote:
[...]
 In other words, non-static nested classes can reference non-static
 fields of the enclosing class. [...]
[...]
 void main() {
 auto a = new A;
 auto b = a.new B; // construct an 'A.B' with 'a' in implicit
 'outer' field
 a.x = 100;
 b.echo(); // writes '100'
 }
 
 This is probably one of the more obscure features of D. =)
[...]

It totally makes sense though. In some of my past C++ projects, I've had
to use the inner class idiom quite often. Of course, it's not directly
supported by the language so I ended up writing lots of little nested
classes like this:

class outer {
...
class inner1 {
outer *ctxt;
...
inner1(outer *c) : ctxt(c) {}
};
...
class inner2 {
outer *ctxt;
...
inner2(outer *c) : ctxt(c) {}
};
...
void f() {
...
inner1 *helper1 = new inner1(this);
register_callback(helper1, ...);
...
inner2 *helper2 = new inner2(this);
register_callback(helper2, ...);
...
}
};

After a while, it just got really really tedious to keep writing the
same boilerplate code over and over again. In D, a lot of that
redundancy can be gotten rid of (no need for explicit outer pointers in
the inner classes, eliminate ctor parameters), just because (non-static)
inner classes automatically get an outer pointer, and you can just
instantiate them with:

auto helper1 = this.new inner1;

But D lets you do even better. Instead of creating an inner class, you
can just pass a delegate to do what needs to be done:

void f() {
...
register_callback((args) { this.state1++; }, ...);
register_callback((args) { this.state2++; }, ...);
...
}

Much more readable, and much less room for bugs to hide in.


T

-- 
Maybe is a strange word.  When mom or dad says it it means yes, but when my 
big brothers say it it means no! -- PJ jr.


Re: Error: 'this' is only defined in non-static member functions, not parse

2012-01-17 Thread Stewart Gordon
For future reference and to elaborate on what others have said, if you're asking for help 
solving a problem with your code, then please:


1. Post a small, self-contained testcase that demonstrates the problem straight out of the 
box.


Tips here:
http://www.sscce.org/

2. Post full compiler output (or runtime output, if it's a runtime problem) 
from the testcase.

3. State what compiler version and operating system you are using, as it may be 
relevant.

Stewart.


Error: 'this' is only defined in non-static member functions, not parse

2012-01-16 Thread Matej Nanut
Hey everyone,

I, once again, have a problem with an error I can't seem to figure out!

The situation:
- a class, inherited by five other classes;
- the class having a static function which returns one
  if its subclasses depending on the input of a string.

Something like this:

class Node
{
  static Node parse(ref string s)
  {
/* Get value to switch by, an enum. */
auto switchable = /* ... */;
final switch (switchable)
{
  case Blah.one: return new OneNode(s);
  case Blah.two: return new TwoNode(s);
/* ... */
}
  }
}

And I get the mentioned error. I don't understand it:
is it saying I'm using `this' in a static member function
called `parse'? Am I insane; where am I referencing it?

The other classes are in this form:

class OneNode : Node
{
  /* ... stuff ... */
  this(ref string s)
  {
/* Does stuff with `s'. */
  }
}

Do you need more information?

Thank you,
Matej


Re: Error: 'this' is only defined in non-static member functions, not parse

2012-01-16 Thread Timon Gehr

On 01/17/2012 12:49 AM, Matej Nanut wrote:

Hey everyone,

I, once again, have a problem with an error I can't seem to figure out!

The situation:
- a class, inherited by five other classes;
- the class having a static function which returns one
   if its subclasses depending on the input of a string.

Something like this:

class Node
{
   static Node parse(ref string s)
   {
 /* Get value to switch by, an enum. */
 auto switchable = /* ... */;
 final switch (switchable)
 {
   case Blah.one: return new OneNode(s);
   case Blah.two: return new TwoNode(s);
 /* ... */
 }
   }
}

And I get the mentioned error. I don't understand it:
is it saying I'm using `this' in a static member function
called `parse'? Am I insane; where am I referencing it?

The other classes are in this form:

class OneNode : Node
{
   /* ... stuff ... */
   this(ref string s)
   {
 /* Does stuff with `s'. */
   }
}

Do you need more information?

Yes; It is extremely hard to solve the problem when there is no code 
snippet given which exhibits the problematic behavior in question.