www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - DMD won't compile re-init of variable

reply Simon <lehmayr+d gmail.com> writes:
Hi dlang community,

I'm trying to implement a "reset" functionality which should 
revert all variables to the program start initial state.

Example:

import Graph;
protected Edge[string] m_string2edge;

int main()
{
     // adding some elements
     // not important how it works
     // m_string2edge[name] = e;

     // resetting it
     m_string2edge = null;
     m_string2edge = new Edge[string]; // <- won't compile

     return 0;
}

How do I revert my variable to the init state?

Thanks in advance,
Simon
Jan 30 2020
next sibling parent reply MoonlightSentinel <moonlightsentinel disroot.org> writes:
On Thursday, 30 January 2020 at 21:09:41 UTC, Simon wrote:
 Hi dlang community,

 I'm trying to implement a "reset" functionality which should 
 revert all variables to the program start initial state.

 Example:

 import Graph;
 protected Edge[string] m_string2edge;

 int main()
 {
     // adding some elements
     // not important how it works
     // m_string2edge[name] = e;

     // resetting it
     m_string2edge = null;
     m_string2edge = new Edge[string]; // <- won't compile

     return 0;
 }

 How do I revert my variable to the init state?

 Thanks in advance,
 Simon
You can use m_string2edge.clear() if you want to remove all entries from m_string2edge. See https://dlang.org/spec/hash-map.html#properties
Jan 30 2020
parent Simon <lehmayr+d gmail.com> writes:
On Thursday, 30 January 2020 at 21:18:04 UTC, MoonlightSentinel 
wrote:
 On Thursday, 30 January 2020 at 21:09:41 UTC, Simon wrote:
 Hi dlang community,

 I'm trying to implement a "reset" functionality which should 
 revert all variables to the program start initial state.

 Example:

 import Graph;
 protected Edge[string] m_string2edge;

 int main()
 {
     // adding some elements
     // not important how it works
     // m_string2edge[name] = e;

     // resetting it
     m_string2edge = null;
     m_string2edge = new Edge[string]; // <- won't compile

     return 0;
 }

 How do I revert my variable to the init state?

 Thanks in advance,
 Simon
You can use m_string2edge.clear() if you want to remove all entries from m_string2edge. See https://dlang.org/spec/hash-map.html#properties
Hi MoonlightSentinel, in this case it won't compile: ..\Electrics.d(42): Error: function Electrics.clear() is not callable using argument types (Edge[string]) ..\Electrics.d(42): expected 0 argument(s), not 1
Feb 03 2020
prev sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 30 January 2020 at 21:09:41 UTC, Simon wrote:
 How do I revert my variable to the init state?
null is the initial state for those.
Jan 30 2020
parent reply Minty Fresh <mintiewute+dlang gmail.com> writes:
On Thursday, 30 January 2020 at 21:36:53 UTC, Adam D. Ruppe wrote:
 On Thursday, 30 January 2020 at 21:09:41 UTC, Simon wrote:
 How do I revert my variable to the init state?
null is the initial state for those.
More generally, .init can be used as to get the initial state for any type. ie. m_string2edge = typeof(m_string2edge).init;
Jan 31 2020
parent Simon <lehmayr+d gmail.com> writes:
On Friday, 31 January 2020 at 14:01:04 UTC, Minty Fresh wrote:
 On Thursday, 30 January 2020 at 21:36:53 UTC, Adam D. Ruppe 
 wrote:
 On Thursday, 30 January 2020 at 21:09:41 UTC, Simon wrote:
 How do I revert my variable to the init state?
null is the initial state for those.
More generally, .init can be used as to get the initial state for any type. ie. m_string2edge = typeof(m_string2edge).init;
Thank you, Minty Fresh, this was the solution!
Feb 03 2020