www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - variant .init value

reply Norm <norm.rowtree gmail.com> writes:
Hi,

I'm trying to use Variant in a struct and want a default init 
value like so:

---
struct S {
   Variant v = Variant(10);
}
void main() {auto s = S();}

but when I try to build this I get the following error:

dmd2/linux/bin64/../../src/phobos/std/variant.d(661): Error: 
memcpy cannot be interpreted at compile time, because it has no 
available source code
Error: cannot interpret <error> at compile time
---

I don't particularly need or want this to be available at compile 
time, I really only want the struct to have a default value when 
instantiated at runtime.

Is there a way to do this with a Variant member?

Thanks,
Norm
Feb 06 2019
next sibling parent reply Alex <sascha.orlov gmail.com> writes:
On Thursday, 7 February 2019 at 07:33:50 UTC, Norm wrote:
 Hi,

 I'm trying to use Variant in a struct and want a default init 
 value like so:

 ---
 struct S {
   Variant v = Variant(10);
 }
 void main() {auto s = S();}

 but when I try to build this I get the following error:

 dmd2/linux/bin64/../../src/phobos/std/variant.d(661): Error: 
 memcpy cannot be interpreted at compile time, because it has no 
 available source code
 Error: cannot interpret <error> at compile time
 ---

 I don't particularly need or want this to be available at 
 compile time, I really only want the struct to have a default 
 value when instantiated at runtime.

 Is there a way to do this with a Variant member?

 Thanks,
 Norm
Hmm... found something similar from 2014... https://issues.dlang.org/show_bug.cgi?id=11864
Feb 06 2019
parent Norm <norm.rowtree gmail.com> writes:
On Thursday, 7 February 2019 at 07:44:17 UTC, Alex wrote:
 On Thursday, 7 February 2019 at 07:33:50 UTC, Norm wrote:
 [...]
Hmm... found something similar from 2014... https://issues.dlang.org/show_bug.cgi?id=11864
Thanks, I've added a comment to that bug report. Cheers, Norm
Feb 06 2019
prev sibling parent bauss <jj_1337 live.dk> writes:
On Thursday, 7 February 2019 at 07:33:50 UTC, Norm wrote:
 Hi,

 I'm trying to use Variant in a struct and want a default init 
 value like so:

 ---
 struct S {
   Variant v = Variant(10);
 }
 void main() {auto s = S();}

 but when I try to build this I get the following error:

 dmd2/linux/bin64/../../src/phobos/std/variant.d(661): Error: 
 memcpy cannot be interpreted at compile time, because it has no 
 available source code
 Error: cannot interpret <error> at compile time
 ---

 I don't particularly need or want this to be available at 
 compile time, I really only want the struct to have a default 
 value when instantiated at runtime.

 Is there a way to do this with a Variant member?

 Thanks,
 Norm
I know this is not ideal, but here is a "workaround": struct S { Variant v; static S opCall() { S s; s.v = Variant(10); return s; } } Would have worked better if structs allowed default ctor in D, but they don't unfortunately.
Feb 06 2019