digitalmars.D.learn - Scope variables.
- Agustin (21/21) Oct 07 2013 I'm having a hard time trying to use "scoped".
- Agustin (2/23) Oct 07 2013 callEvent!MyEvent(); Being MyEvent a subclass of Event.
- Agustin (17/43) Oct 07 2013 So i found out that i cannot do this, may i ask why?
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (19/62) Oct 07 2013 Since classes are already references, you would normally pass A, not
- Agustin (4/76) Oct 07 2013 Doesn't ref means i'm passing the parameter by reference instead
- Justin Whear (4/7) Oct 07 2013 Class instances are by reference already, structs are by value. For
- Agustin (2/12) Oct 07 2013 Thank you!.
- Justin Whear (3/22) Oct 07 2013 Get rid of the "ref" in func's signature--objects are already by
I'm having a hard time trying to use "scoped". public T callEvent(T, A...)(auto ref A args) const { T pEvent = scoped!T(forward!args); postEvent(pEvent, typeid(T).toHash); return pEvent; } private void postEvent(ref Event event, Event.ID type) const { .... } src\event\EventManager.d(37): Error: function ghrum.event.EventManager.EventManager.postEvent (ref Event event, uint type) const is not callable using argument types (MyEvent,uint) src\event\EventManager.d(37): Error: function ghrum.event.EventManager.EventManager.postEvent (ref Event event, uint type) const is not callable using argument types (MyEvent,uint) const src\event\EventManager.d(37): Error: cast(Event)pEvent is not an lvalue
Oct 07 2013
On Monday, 7 October 2013 at 19:58:21 UTC, Agustin wrote:I'm having a hard time trying to use "scoped". public T callEvent(T, A...)(auto ref A args) const { T pEvent = scoped!T(forward!args); postEvent(pEvent, typeid(T).toHash); return pEvent; } private void postEvent(ref Event event, Event.ID type) const { .... } src\event\EventManager.d(37): Error: function ghrum.event.EventManager.EventManager.postEvent (ref Event event, uint type) const is not callable using argument types (MyEvent,uint) src\event\EventManager.d(37): Error: function ghrum.event.EventManager.EventManager.postEvent (ref Event event, uint type) const is not callable using argument types (MyEvent,uint) const src\event\EventManager.d(37): Error: cast(Event)pEvent is not an lvaluecallEvent!MyEvent(); Being MyEvent a subclass of Event.
Oct 07 2013
On Monday, 7 October 2013 at 19:59:09 UTC, Agustin wrote:On Monday, 7 October 2013 at 19:58:21 UTC, Agustin wrote:So i found out that i cannot do this, may i ask why? public class A { int x = 0; } public class B : A { } void func(ref A a) { } void main() { B b = new B(); func(b); }I'm having a hard time trying to use "scoped". public T callEvent(T, A...)(auto ref A args) const { T pEvent = scoped!T(forward!args); postEvent(pEvent, typeid(T).toHash); return pEvent; } private void postEvent(ref Event event, Event.ID type) const { .... } src\event\EventManager.d(37): Error: function ghrum.event.EventManager.EventManager.postEvent (ref Event event, uint type) const is not callable using argument types (MyEvent,uint) src\event\EventManager.d(37): Error: function ghrum.event.EventManager.EventManager.postEvent (ref Event event, uint type) const is not callable using argument types (MyEvent,uint) const src\event\EventManager.d(37): Error: cast(Event)pEvent is not an lvaluecallEvent!MyEvent(); Being MyEvent a subclass of Event.
Oct 07 2013
On 10/07/2013 03:52 PM, Agustin wrote:On Monday, 7 October 2013 at 19:59:09 UTC, Agustin wrote:Since classes are already references, you would normally pass A, not 'ref A'. If you really want to pass 'ref A', perhaps you want to change the actual object that an A is referring to: public class C : A {} void func(ref A a) { a = new C; // <-- A reason for taking 'ref A' } However, that would upset the caller, which thinks it has a reference to a B object: void main() { B b = new B(); func(b); // <-- Oops! Not a B anymore? } AliOn Monday, 7 October 2013 at 19:58:21 UTC, Agustin wrote:So i found out that i cannot do this, may i ask why? public class A { int x = 0; } public class B : A { } void func(ref A a) { } void main() { B b = new B(); func(b); }I'm having a hard time trying to use "scoped". public T callEvent(T, A...)(auto ref A args) const { T pEvent = scoped!T(forward!args); postEvent(pEvent, typeid(T).toHash); return pEvent; } private void postEvent(ref Event event, Event.ID type) const { .... } src\event\EventManager.d(37): Error: function ghrum.event.EventManager.EventManager.postEvent (ref Event event, uint type) const is not callable using argument types (MyEvent,uint) src\event\EventManager.d(37): Error: function ghrum.event.EventManager.EventManager.postEvent (ref Event event, uint type) const is not callable using argument types (MyEvent,uint) const src\event\EventManager.d(37): Error: cast(Event)pEvent is not an lvaluecallEvent!MyEvent(); Being MyEvent a subclass of Event.
Oct 07 2013
On Monday, 7 October 2013 at 22:57:17 UTC, Ali Çehreli wrote:On 10/07/2013 03:52 PM, Agustin wrote:Doesn't ref means i'm passing the parameter by reference instead of by value?. Isn't "a" being copied when calling func?, or does D always pass by reference when using classes and structures?On Monday, 7 October 2013 at 19:59:09 UTC, Agustin wrote:Since classes are already references, you would normally pass A, not 'ref A'. If you really want to pass 'ref A', perhaps you want to change the actual object that an A is referring to: public class C : A {} void func(ref A a) { a = new C; // <-- A reason for taking 'ref A' } However, that would upset the caller, which thinks it has a reference to a B object: void main() { B b = new B(); func(b); // <-- Oops! Not a B anymore? } AliOn Monday, 7 October 2013 at 19:58:21 UTC, Agustin wrote:So i found out that i cannot do this, may i ask why? public class A { int x = 0; } public class B : A { } void func(ref A a) { } void main() { B b = new B(); func(b); }I'm having a hard time trying to use "scoped". public T callEvent(T, A...)(auto ref A args) const { T pEvent = scoped!T(forward!args); postEvent(pEvent, typeid(T).toHash); return pEvent; } private void postEvent(ref Event event, Event.ID type) const { .... } src\event\EventManager.d(37): Error: function ghrum.event.EventManager.EventManager.postEvent (ref Event event, uint type) const is not callable using argument types (MyEvent,uint) src\event\EventManager.d(37): Error: function ghrum.event.EventManager.EventManager.postEvent (ref Event event, uint type) const is not callable using argument types (MyEvent,uint) const src\event\EventManager.d(37): Error: cast(Event)pEvent is not an lvaluecallEvent!MyEvent(); Being MyEvent a subclass of Event.
Oct 07 2013
On Tue, 08 Oct 2013 01:01:43 +0200, Agustin wrote:Doesn't ref means i'm passing the parameter by reference instead of by value?. Isn't "a" being copied when calling func?, or does D always pass by reference when using classes and structures?Class instances are by reference already, structs are by value. For further comparison, please see the table here: http://dlang.org/struct.html
Oct 07 2013
On Monday, 7 October 2013 at 23:18:13 UTC, Justin Whear wrote:On Tue, 08 Oct 2013 01:01:43 +0200, Agustin wrote:Thank you!.Doesn't ref means i'm passing the parameter by reference instead of by value?. Isn't "a" being copied when calling func?, or does D always pass by reference when using classes and structures?Class instances are by reference already, structs are by value. For further comparison, please see the table here: http://dlang.org/struct.html
Oct 07 2013
On Tue, 08 Oct 2013 00:52:19 +0200, Agustin wrote:So i found out that i cannot do this, may i ask why? public class A { int x = 0; } public class B : A { } void func(ref A a) { } void main() { B b = new B(); func(b); }Get rid of the "ref" in func's signature--objects are already by reference. With that fix, compiles just fine for me.
Oct 07 2013