Re: `this` and nested structs

2018-05-10 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, May 10, 2018 06:31:09 Mike Franklin via Digitalmars-d-learn 
wrote:
> On Thursday, 10 May 2018 at 06:22:37 UTC, Jonathan M Davis wrote:
> > Structs don't have that.
>
> Should they?

Honestly, I don't think that classes should have it, but changing it now
would break code (most notably, DWT). IMHO, it would have been far better to
make it explicit like it is in C++, especially since there's no technical
reason why it needs to be built in.

But regardless, when you consider how structs work, having a nested struct
inside a struct have a pointer to its outer struct would be a serious
problem, because the pointer would become invalid as soon as the struct was
moved, which happens quite frequently with structs in D when they're passed
around. Having a nested struct within a class have a reference to its parent
would be more debatable, but it would probably run afoul of how init works,
since the init value for the struct would have to be known at compile-time,
whereas the reference for the class couldn't be. And anyone who wants any
kind of reference to the outer class to be in the struct can just pass it to
the struct's constructor. Adding such a feature to structs would just be
unnecessary magic.

- Jonathan M Davis



Re: `this` and nested structs

2018-05-10 Thread Mike Franklin via Digitalmars-d-learn

On Thursday, 10 May 2018 at 06:22:37 UTC, Jonathan M Davis wrote:


Structs don't have that.


Should they?




Re: `this` and nested structs

2018-05-10 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, May 10, 2018 03:23:50 Mike Franklin via Digitalmars-d-learn 
wrote:
> My understanding is that nested structs have an implicit context
> pointer to their containing scope.

A non-static struct inside a function does, but I suspect that you're
thinking about non-static nested classes (within classes), which I believe
was done to be compatible with Java (in order to make porting code easier).
Structs don't have that.

- Jonathan M Davis


Re: `this` and nested structs

2018-05-09 Thread Radu via Digitalmars-d-learn

On Thursday, 10 May 2018 at 03:23:50 UTC, Mike Franklin wrote:

Consider the following code:

---
struct S
{
// intentionally not `static`
struct SS
{
int y() { return x; }  // Error: need `this` for `x` of 
type `int`

}

int x;
SS ss;
}

void main()
{
S s;
s.ss.y();
}
---

If I change `return x;` to `return this.x;` then of course it 
emits the following error:


Error: no property `x` for type `SS`

My understanding is that `SS` should have a context pointer to 
an instance of `S`, but how do I navigate the members of `S` 
and `SS`.  Is this a bug?


Thanks,
Mike

My understanding is that nested structs have an implicit 
context pointer to their containing scope.


Nesting with hidden context pointer is only for nested structs 
inside functions.

https://dlang.org/spec/struct.html#nested

This is a source a confusion unfortunately.


`this` and nested structs

2018-05-09 Thread Mike Franklin via Digitalmars-d-learn

Consider the following code:

---
struct S
{
// intentionally not `static`
struct SS
{
int y() { return x; }  // Error: need `this` for `x` of 
type `int`

}

int x;
SS ss;
}

void main()
{
S s;
s.ss.y();
}
---

If I change `return x;` to `return this.x;` then of course it 
emits the following error:


Error: no property `x` for type `SS`

My understanding is that `SS` should have a context pointer to an 
instance of `S`, but how do I navigate the members of `S` and 
`SS`.  Is this a bug?


Thanks,
Mike

My understanding is that nested structs have an implicit context 
pointer to their containing scope.


Re: How to handle nested structs when converting C headers?

2013-12-12 Thread Regan Heath
On Thu, 12 Dec 2013 00:04:07 -, H. S. Teoh hst...@quickfur.ath.cx  
wrote:



On Thu, Dec 12, 2013 at 12:54:58AM +0100, Gary Willoughby wrote:

On Wednesday, 11 December 2013 at 23:38:13 UTC, Adam D. Ruppe wrote:
On Wednesday, 11 December 2013 at 23:35:04 UTC, Gary Willoughby
wrote:
   static union internalRep


try

static union InternalRep { /* note the capital letter */
  /* snip */
}
InternalRep internalRep;; // still need a decl

Right. But why use the static keyword here?


Because nested structs by default carry a pointer to the containing
struct (or scope), which means it adds extra baggage and you can't
create the nested without also having an instance of the containing
struct.


I would stop nesting the struct definition.  I think that is both cleaner  
and closer to the original intent - the only reason it is nested in C is  
because C allows definition and declaration that way, and D does not.   
Then you don't need static at all.


R

--
Using Opera's revolutionary email client: http://www.opera.com/mail/


Re: How to handle nested structs when converting C headers?

2013-12-12 Thread bearophile

Regan Heath:

I would stop nesting the struct definition.  I think that is 
both cleaner and closer to the original intent - the only 
reason it is nested in C is because C allows definition and 
declaration that way, and D does not.  Then you don't need 
static at all.


It's mostly a matter of style, but here I prefer a little more 
the nested form. It's syntactically closer to the original C 
file, for a D programmer static struct has a known and clear 
meaning, and putting the structs inside the other keeps the 
global namespace clean of those inner names. Like with nested 
functions putting names inside other scoped helps the programmer 
see they are just needed there.


Bye,
bearophile


Re: How to handle nested structs when converting C headers?

2013-12-12 Thread Jacob Carlborg

On 2013-12-11 23:45, Gary Willoughby wrote:

How to handle nested structs when converting C headers?

In the following snippet i'm currently converting, how would you convert
the nested typed union and structures? Would you declare them separately
then use their types in the Tcl_Obj struct? or is there a nice way in D
to nest them like C?


Convert using DStep[1] and see what happens :)

[1] https://github.com/jacob-carlborg/dstep

--
/Jacob Carlborg


How to handle nested structs when converting C headers?

2013-12-11 Thread Gary Willoughby

How to handle nested structs when converting C headers?

In the following snippet i'm currently converting, how would you 
convert the nested typed union and structures? Would you declare 
them separately then use their types in the Tcl_Obj struct? or is 
there a nice way in D to nest them like C?


typedef struct Tcl_Obj {
int refCount;   /* When 0 the object will be freed. */
char *bytes;/* This points to the first byte of the
 * object's string representation. The array
 * must be followed by a null byte (i.e., at
 * offset length) but may also contain
 * embedded null characters. The array's
 * storage is allocated by ckalloc. NULL means
 * the string rep is invalid and must be
 * regenerated from the internal rep.  Clients
 * should use Tcl_GetStringFromObj or
 * Tcl_GetString to get a pointer to the byte
 * array as a readonly value. */
int length; /* The number of bytes at *bytes, not
 * including the terminating null. */
Tcl_ObjType *typePtr;   /* Denotes the object's type. Always
 * corresponds to the type of the object's
 * internal rep. NULL indicates the object has
 * no internal rep (has no type). */
union { /* The internal representation: */
long longValue; /*   - an long integer value. */
double doubleValue; /*   - a double-precision floating value. */
VOID *otherValuePtr;/*   - another, type-specific value. */
Tcl_WideInt wideValue;  /*   - a long long value. */
struct {/*   - internal rep as two pointers. */
VOID *ptr1;
VOID *ptr2;
} twoPtrValue;
struct {/*   - internal rep as a wide int, tightly
 * packed fields. */
VOID *ptr;  /* Pointer to digits. */
unsigned long value;/* Alloc, used, and signum packed into a
 * single word. */
} ptrAndLongRep;
} internalRep;
} Tcl_Obj;


Re: How to handle nested structs when converting C headers?

2013-12-11 Thread Adam D. Ruppe
On Wednesday, 11 December 2013 at 22:45:35 UTC, Gary Willoughby 
wrote:

How to handle nested structs when converting C headers?


Nested structs and unions like in your example are supported in D 
too, same syntax, same effect.


Re: How to handle nested structs when converting C headers?

2013-12-11 Thread Adam D. Ruppe
On Wednesday, 11 December 2013 at 22:54:04 UTC, Adam D. Ruppe 
wrote:
Nested structs and unions like in your example are supported in 
D too, same syntax, same effect.


Actually, no, not quite the same syntax, I didn't notice the name 
at the end of the C one (in D, the anonymous nested structs and 
unions are supported).


But almost the same then: make them a nested type with a name 
after the struct or union keyword then the member. So


struct TwoPtrValue {/*   - internal rep as 
two pointers. */

VOID *ptr1;
VOID *ptr2;
}
 TwoPtrValue twoPtrValue;

that's how I'd do it


Re: How to handle nested structs when converting C headers?

2013-12-11 Thread bearophile

Adam D. Ruppe:

Nested structs and unions like in your example are supported in 
D too, same syntax, same effect.


But don't forget to add to use static struct instad of struct.

Bye,
bearophile


Re: How to handle nested structs when converting C headers?

2013-12-11 Thread Gary Willoughby

On Wednesday, 11 December 2013 at 23:12:39 UTC, bearophile wrote:

Adam D. Ruppe:

Nested structs and unions like in your example are supported 
in D too, same syntax, same effect.


But don't forget to add to use static struct instad of 
struct.


Bye,
bearophile


Have you got an example because i always get:

tcl.d(713): Error: no identifier for declarator twoPtrValue
tcl.d(718): Error: no identifier for declarator ptrAndLongRep
tcl.d(719): Error: no identifier for declarator internalRep


Re: How to handle nested structs when converting C headers?

2013-12-11 Thread Gary Willoughby
On Wednesday, 11 December 2013 at 23:27:57 UTC, Gary Willoughby 
wrote:
On Wednesday, 11 December 2013 at 23:12:39 UTC, bearophile 
wrote:

Adam D. Ruppe:

Nested structs and unions like in your example are supported 
in D too, same syntax, same effect.


But don't forget to add to use static struct instad of 
struct.


Bye,
bearophile


Have you got an example because i always get:

tcl.d(713): Error: no identifier for declarator twoPtrValue
tcl.d(718): Error: no identifier for declarator ptrAndLongRep
tcl.d(719): Error: no identifier for declarator internalRep


Like this perhaps:

struct Tcl_Obj
{
int refCount;
char* bytes;
int length;
Tcl_ObjType* typePtr;

static union internalRep
{
c_long longValue;
double doubleValue;
void* otherValuePtr;
Tcl_WideInt wideValue;

static struct twoPtrValue
{
void* ptr1;
void* ptr2;
}

static struct ptrAndLongRep
{
void* ptr;
c_ulong value;
}
}
}


Re: How to handle nested structs when converting C headers?

2013-12-11 Thread Adam D. Ruppe
On Wednesday, 11 December 2013 at 23:35:04 UTC, Gary Willoughby 
wrote:

static union internalRep



try

static union InternalRep { /* note the capital letter */
  /* snip */
}
InternalRep internalRep;; // still need a decl



Re: How to handle nested structs when converting C headers?

2013-12-11 Thread bearophile

Gary Willoughby:


Have you got an example because i always get:

tcl.d(713): Error: no identifier for declarator twoPtrValue
tcl.d(718): Error: no identifier for declarator ptrAndLongRep
tcl.d(719): Error: no identifier for declarator internalRep


In D you can't define a struct/union and use it to define an 
instance on the fly. You have to split definition and use in two 
parts.


Bye,
bearophile


Re: How to handle nested structs when converting C headers?

2013-12-11 Thread Adam D. Ruppe

On Wednesday, 11 December 2013 at 23:36:11 UTC, bearophile wrote:
In D you can't define a struct/union and use it to define an 
instance on the fly.


Well, you can if it is anonymous.

struct Foo {
union {
struct { ubyte a; ubyte b; }
ubyte[2] arr;
}
}

That works in D, and it makes foo.a == arr[0] and foo.b == arr[1];


Re: How to handle nested structs when converting C headers?

2013-12-11 Thread bearophile

Adam D. Ruppe:


Well, you can if it is anonymous.

struct Foo {
union {
struct { ubyte a; ubyte b; }
ubyte[2] arr;
}
}

That works in D, and it makes foo.a == arr[0] and foo.b == 
arr[1];


Right :-) I like D structs.

Bye,
bearophile


Re: How to handle nested structs when converting C headers?

2013-12-11 Thread Gary Willoughby
On Wednesday, 11 December 2013 at 23:38:13 UTC, Adam D. Ruppe 
wrote:
On Wednesday, 11 December 2013 at 23:35:04 UTC, Gary Willoughby 
wrote:

   static union internalRep



try

static union InternalRep { /* note the capital letter */
  /* snip */
}
InternalRep internalRep;; // still need a decl


Right. But why use the static keyword here?


Re: How to handle nested structs when converting C headers?

2013-12-11 Thread H. S. Teoh
On Thu, Dec 12, 2013 at 12:54:58AM +0100, Gary Willoughby wrote:
 On Wednesday, 11 December 2013 at 23:38:13 UTC, Adam D. Ruppe wrote:
 On Wednesday, 11 December 2013 at 23:35:04 UTC, Gary Willoughby
 wrote:
static union internalRep
 
 
 try
 
 static union InternalRep { /* note the capital letter */
   /* snip */
 }
 InternalRep internalRep;; // still need a decl
 
 Right. But why use the static keyword here?

Because nested structs by default carry a pointer to the containing
struct (or scope), which means it adds extra baggage and you can't
create the nested without also having an instance of the containing
struct.


T

-- 
It is of the new things that men tire --- of fashions and proposals and
improvements and change. It is the old things that startle and
intoxicate. It is the old things that are young. -- G.K. Chesterton


Ddoc: no docs generated for nested structs?

2012-03-07 Thread H. S. Teoh
I wrote a whole bunch of documentation for a struct that I later decided
to transplant inside another struct (because the two are closely
linked), and now the former struct's docs have vanished. Is this
expected behaviour?


T

-- 
I am a consultant. My job is to make your job redundant. -- Mr Tom


Re: Ddoc: no docs generated for nested structs?

2012-03-07 Thread H. S. Teoh
On Wed, Mar 07, 2012 at 03:49:26PM -0800, H. S. Teoh wrote:
 I wrote a whole bunch of documentation for a struct that I later decided
 to transplant inside another struct (because the two are closely
 linked), and now the former struct's docs have vanished. Is this
 expected behaviour?
[...]

Argh, nevermind what I wrote. In the course of moving the code I
accidentally inserted another declaration between the struct and the
ddoc comment.


T

-- 
Nearly all men can stand adversity, but if you want to test a man's
character, give him power. -- Abraham Lincoln


Re: Ddoc: no docs generated for nested structs?

2012-03-07 Thread Jonathan M Davis
On Wednesday, March 07, 2012 16:37:44 H. S. Teoh wrote:
 On Wed, Mar 07, 2012 at 03:49:26PM -0800, H. S. Teoh wrote:
  I wrote a whole bunch of documentation for a struct that I later decided
  to transplant inside another struct (because the two are closely
  linked), and now the former struct's docs have vanished. Is this
  expected behaviour?
 
 [...]
 
 Argh, nevermind what I wrote. In the course of moving the code I
 accidentally inserted another declaration between the struct and the
 ddoc comment.

If it's nested inside of a function, then any docs on the struct shouldn't end 
up in the documentation. But documentation on structs or classes nested inside 
of other structs or classes should definitely show up.

- Jonathan M Davis


Nested Structs

2010-12-27 Thread d coder
Greetings All

I have a situation where I have a struct nested inside a class. I
would like to make the enclosing class' members visible inside the
nested struct's constructor. I see that such preposition is feasible
for nested classes, but not for nested structs. Why so?

Are there some alternative constructs which could give me this
behavior for nested constructs?

Regards
- Cherry


Re: Nested Structs

2010-12-27 Thread Steven Schveighoffer

On Mon, 27 Dec 2010 08:54:37 -0500, d coder dlang.co...@gmail.com wrote:


Greetings All

I have a situation where I have a struct nested inside a class. I
would like to make the enclosing class' members visible inside the
nested struct's constructor. I see that such preposition is feasible
for nested classes, but not for nested structs. Why so?


A struct nested in a class does not have a hidden outer pointer as a  
nested class does.  It's because a struct is generally more bare-bones  
than a class (which has loads of hidden pieces: vtable, interfaces,  
classinfo, etc.).  Also, instantiating such a struct does not tie it to a  
class instance.




Are there some alternative constructs which could give me this
behavior for nested constructs?


You need to implement this behavior on your own:

class C
{
  struct S
  {
private C _outer;
this(C c) { this._outer = c; _outer.x = 5;} // access enclosing class  
instance via _outer.

  }

  int x;
}

-Steve


Re: Nested Structs

2010-12-27 Thread Steven Schveighoffer
On Mon, 27 Dec 2010 12:50:47 -0500, bearophile bearophileh...@lycos.com  
wrote:



Steven Schveighoffer:


A struct nested in a class does not have a hidden outer pointer as a
nested class does.


But I think this will eventually change, once this part is implemented:
http://www.digitalmars.com/d/2.0/struct.html

Nested Structs: A nested struct is a struct that is declared inside the  
scope of a function or a templated struct that has aliases to local  
functions as a template argument. Nested structs have member functions.  
It has access to the context of its enclosing scope (via an added  
hidden field).


That is implemented, but notice the conditions that form a nested struct  
do not include inside a class or inside a struct (that templated  
struct that has aliases to local functions as a template argument  
condition is pretty specific, I'm talking about just putting it in any old  
struct).


This is decidedly different from a nested class:

http://www.digitalmars.com/d/2.0/class.html#nested

A nested class is a class that is declared inside the scope of a function  
or another class. A nested class has access to the variables and other  
symbols of the classes and functions it is nested inside


This is what the OP was desiring.

-Steve


Re: Nested Structs

2010-12-27 Thread d coder
 A struct nested in a class does not have a hidden outer pointer as a
 nested class does.  It's because a struct is generally more bare-bones than
 a class (which has loads of hidden pieces: vtable, interfaces, classinfo,
 etc.).  Also, instantiating such a struct does not tie it to a class
 instance.


Thanks Steve

As far as I am concerned, this is a very limiting behavior wrt structs
nested inside classes. I have also observed that if you define a
constant (using enum for example) inside the enclosing class, the
constant remains visible in the nested struct too. So I believe the
language is making the hidden outer scope visible to the nested
struct (or maybe there is some other mechanism for enums that I am not
aware of).

 You need to implement this behavior on your own:


Actually I am trying to define a DSEL and the end users are not
typically programmers. I am in a situation where the end-user decides
the unit in an enclosing class (unit of length as an inch for example)
and later whenever he instantiates a line or a shape, the particular
unit is automatically considered at the time of instantiation. For my
application, I need value semantics and therefor using nested classes
in place of structs can not be considered.

Making the end-users pass the unit or its context (in form of this
pointer) everytime they instantiate a shape would be atrocious. It
would defeat the very purpose of letting the end-user define a unit.

I know I am not on the right mailing group, but I want to ask for this
particular language enhancement. I believe this would make the
behavior of the language more straight wrt nested structs inside
classes (are not these beasts expected to serve like nested classes or
even structs nested inside function scopes?). I hope my requirement is
generic enough and fits the bill for an enhancement request.

Regards
- Cherry


C-style nested structs convert to D

2009-03-18 Thread CodexArcanum
Hey all, I'm trying to rewrite a few .h files into D so I can call a C library. 
 One snag I've hit is this guy:

struct _tree_t {
_tree_t* next;
_tree_t* father;
_tree_t* sons;
}

D won't do the nested struct thing, so I need to convert this into something D 
will like, but which the library will still take in.  What should I do?