rebind of const class variables

2015-01-20 Thread qqiang via Digitalmars-d-learn

I am writing a tree data structure, and I have the following code:

```D
final class Node {
private {
int val_;
Node parent_;
Node left_;
Node right_;
}

@property
const(Node) maximum() const {
auto ret = this;

while (ret.right_) {
ret = ret.right_;
}

return ret;
}
}
```

It failed to compile and complaint that `cannot modify const 
expression ret`。


Since `ret` is just a binding to a const class object, why can't 
I rebind it to another const class variable?


Must I use pointers to cope with this?

Thx


Re: no size yet for forward reference for nested structures

2015-01-14 Thread qqiang via Digitalmars-d

On Wednesday, 14 January 2015 at 08:31:08 UTC, bearophile wrote:

qqiang:

I've googled and found no straightforward solution to this 
issue. The how can I modify my code to eliminate this error?


Your code gives me a different error (Error: PowerHeap!int is 
used as a type).


What if you replace the SList with a dynamic array?

Bye,
bearophile


Thanks for your reply.

I'm sorry that I've provided wrong code. This is the exact code:

---

template PowerHeap(T) {
import std.container : SList;

private alias PowerForest = SList!PowerNode;

private final class PowerNode {
private {
T payload_;
uint rank_;
PowerForest children_;
}
}

final class PowerHeap {
private {
PowerNode top_;
PowerForest forest_;
uint size_;
}
}
}

unittest {
PowerHeap!int h;
}

--
My compiler is v2.066.1


Re: no size yet for forward reference for nested structures

2015-01-14 Thread qqiang via Digitalmars-d

On Wednesday, 14 January 2015 at 08:31:13 UTC, Ali Çehreli wrote:
First a reminder that this sort of question is more suitable to 
the D.learn newsgroup.


On 01/13/2015 10:41 PM, qqiang wrote:

 The following code:
 ```D
 template PowerHeap(T) {
  import std.container : SList;

  private alias PowerForest = SList!PowerNode;

  private final class PowerNode {
  ...
  }

  final class PowerHead {
  ...
  }
 }

 unittest {
  PowerHeap!int h;
 }
 ```

I started with your code and produced the following one:

template PowerHeap(T) {
import std.container : SList;

private alias PowerForest = SList!PowerNode;

private final class PowerNode {
// ...
}

final class PowerHead {
// ...
}
}

unittest {
PowerHeap!int h;// -- Compilation error here
}

void main()
{}

 failed to compile and get the error: PowerHeap!int.PowerNode:
no size
 yet for forward reference.

What compiler and version are you using? I received a different 
error with the git head dmd:


  Error: PowerHeap!int is used as a type

The compiler is right, PowerHeap!int is a template 
instantiation that wraps a number of definitions. Did you mean 
one of those? The following compiles:


unittest {
PowerHeap!int.PowerHead h;// -- now compiles
}

 I've googled and found no straightforward solution to this
issue. The
 how can I modify my code to eliminate this error?

Please provide exact code so that... Oh, wait! I've just 
noticed that your error message has PowerHeap!int.PowerNode in 
it. Let me try with that:


unittest {
PowerHeap!int.PowerNode h;// -- this compiles as well
}

Yeah, please provide exact code. :)

Ali


Thanks for your reminder.

I'm sorry that I've provided wrong code. This is the exact code:

---

template PowerHeap(T) {
import std.container : SList;

private alias PowerForest = SList!PowerNode;

private final class PowerNode {
private {
T payload_;
uint rank_;
PowerForest children_;
}
}

final class PowerHeap {
private {
PowerNode top_;
PowerForest forest_;
uint size_;
}
}
}

unittest {
PowerHeap!int h;
}

--
My compiler is v2.066.1


no size yet for forward reference for nested structures

2015-01-13 Thread qqiang via Digitalmars-d

The following code:
```D
template PowerHeap(T) {
import std.container : SList;

private alias PowerForest = SList!PowerNode;

private final class PowerNode {
...
}

final class PowerHead {
...
}
}

unittest {
PowerHeap!int h;
}
```

failed to compile and get the error: PowerHeap!int.PowerNode: no 
size yet for forward reference.


I've googled and found no straightforward solution to this issue. 
The how can I modify my code to eliminate this error?