Re: Functions with package protection
Sorry to dig up this old post, but I still don't understand why 'package' functions cannot be virtual? Is there a good reason for this? I can't see why we can't use polymorphism on 'package' functions! Is there way to make it virtual without making it public? (e.g. a 'virtual' keyword?) Thanks, Simon TRENY Jacob Carlborg Wrote: In the D documentation at http://www.digitalmars.com/d/1.0/function.html it says the following: All non-static non-private non-template member functions are virtual, but this seems not to be the case. What I've heard and as the following example shows functions declared as package are non-virtual. module main; import tango.io.Stdout; class A { package void foo () { Stdout(A).newline; } } class B : A { package void foo () { Stdout(B).newline; } } void main () { A a = new B; a.foo; } This will print A, but according to the documentation package is virtual and therefore this should print B but doesn't. Either the documentation is wrong or the compiler. Compiled with GDC on OSX, I think it will give the same result with dmd also.
Keeping a list of instances and garbage-collection
Hello, I have a class A and I'd like to keep a list of all the created instances of this class. To do that, I have a static List!(A) in the A class and, in the constructor, I add each new instance to this list. This gives me the following code: class A { private static List!(A) s_instances; public this() { s_instances.add(this); } public ~this() { s_instances.remove(this); } public static void printAll() { foreach (A instance; s_instances) print(instance.toString()); } } But then, since all the instances are referenced by the static list, they are never garbage-collected, which could be a problem. In some other languages, this can be solved using weak references, but I haven't found any informations about using weak references in D. Is there any way to solve this problem? Thanks, Simon
Returning a struct by reference
Hi there! I'm quite new at D and I'm still just playing with it, but there is a thing that I find currently missing. Sometimes, I'd like to be able to return a struct by reference and not by value. For example, in the following example: struct Position { float x; float y; } class Object { private Position m_position; public Position position() { return m_position; } } I'd like to be able to write things like this: myObject.position.x = 43 to actually change the position of the object. But right now, since position is a struct, it is returned by value and not by reference, and then the previous instruction won't change the position of the object, but it will work on a copy of the position field. Here is the solutions that I can see to this problem: - Returning a pointer to the position: public Position *position() { ... }, but I'd like to keep my code as free from pointers as possible. - Make Position a class and not a struct. That could be a solution, but then, when I'll do things like Position pos = object.position; pos.x = 43;, it will effectively change the position of the object, which I wouldn't like with this syntax. Actually, I'd like to be able to do a thing like this: public ref Position position() { return m_position; } which would be the equivalent form to passing structs by reference in a parameter. Is there a way to do this in D? Regards, Simon
Re: Returning a struct by reference
grauzone Wrote: Simon TRENY wrote: Hi there! I'm quite new at D and I'm still just playing with it, but there is a thing that I find currently missing. Sometimes, I'd like to be able to return a struct by reference and not by value. For example, in the following example: struct Position { float x; float y; } class Object { private Position m_position; public Position position() { return m_position; } } I'd like to be able to write things like this: myObject.position.x = 43 to actually change the position of the object. But right now, since position is a struct, it is returned by value and not by reference, and then the previous instruction won't change the position of the object, but it will work on a copy of the position field. Here is the solutions that I can see to this problem: - Returning a pointer to the position: public Position *position() { ... }, but I'd like to keep my code as free from pointers as possible. - Make Position a class and not a struct. That could be a solution, but then, when I'll do things like Position pos = object.position; pos.x = 43;, it will effectively change the position of the object, which I wouldn't like with this syntax. Actually, I'd like to be able to do a thing like this: public ref Position position() { return m_position; } which would be the equivalent form to passing structs by reference in a parameter. Is there a way to do this in D? Yes. Make the variable public. class Object { Position position; } This code is even simpler than your's above. Incredible, isn't it? Ok, but then, what if I'd like to make the variable read-only? i.e. preventing the user from writing things like this: myObject.position = pos2; Regards, Simon
Re: Returning a struct by reference
Daniel Keep Wrote: Simon TRENY wrote: Ok, but then, what if I'd like to make the variable read-only? i.e. preventing the user from writing things like this: myObject.position = pos2; So... you're rejecting a solution on the basis that it prevents you from doing the exact opposite of what you want to do? *boggle* -- Daniel Here is a complete example of what I'd like to achieve: struct Position { private float m_x; private float m_y; public float x() { return m_x; } public void x(float x) { m_x = x; EmitSignal(changed); } public float y() { return m_y; } public void y(float y) { m_y = y; EmitSignal(changed); } } class Object { private Position m_position; public this() { m_position.CallOnSignal(changed, onPositionChanged); } //This syntax is not working public ref Position position() { return m_position; } public void onPositionChanged() { writeln(Position Changed!!); } } With this fictional code, I could write things like: object.position.x = 14; and the object will be aware that its position has changed. Making the position-variable public will lead the user to be able to do things like this: object.position = pos2; and then, the object won't be aware that its position has changed. And this is a problem for me. I hope it's clearer now
Re: Returning a struct by reference
grauzone Wrote: Simon TRENY wrote: grauzone Wrote: Simon TRENY wrote: Hi there! I'm quite new at D and I'm still just playing with it, but there is a thing that I find currently missing. Sometimes, I'd like to be able to return a struct by reference and not by value. For example, in the following example: struct Position { float x; float y; } class Object { private Position m_position; public Position position() { return m_position; } } I'd like to be able to write things like this: myObject.position.x = 43 to actually change the position of the object. But right now, since position is a struct, it is returned by value and not by reference, and then the previous instruction won't change the position of the object, but it will work on a copy of the position field. Here is the solutions that I can see to this problem: - Returning a pointer to the position: public Position *position() { ... }, but I'd like to keep my code as free from pointers as possible. - Make Position a class and not a struct. That could be a solution, but then, when I'll do things like Position pos = object.position; pos.x = 43;, it will effectively change the position of the object, which I wouldn't like with this syntax. Actually, I'd like to be able to do a thing like this: public ref Position position() { return m_position; } which would be the equivalent form to passing structs by reference in a parameter. Is there a way to do this in D? Yes. Make the variable public. class Object { Position position; } This code is even simpler than your's above. Incredible, isn't it? Ok, but then, what if I'd like to make the variable read-only? i.e. preventing the user from writing things like this: myObject.position = pos2; Then you write a getter that simply returns the field by value. The D compiler will (hopefully) inline the getter function, so there shouldn't be a disadvantage in performance. If I add a getter-property that returns the field by value, the following instruction object.position.x = 12; won't modify the position of the object, but will only modify the returned copy of the position, right? That's actually why I'd like to have a getter that returns the field by reference and not by value. Note: I think D2.0 wants to introduce ref-returns at some point in the future. Regards, Simon