Thread overview
About variant
Jan 27, 2015
bioinfornatics
Jan 27, 2015
bioinfornatics
Jan 27, 2015
Justin Whear
Jan 27, 2015
bioinfornatics
Jan 27, 2015
ketmar
January 27, 2015
Dear that do a lot time wehere I not used std.variant. i would like to hide extra cast from user by using a generic ctor

import std.variant;

struct Alpha {
	Variant something;
	
	this(T)(T v){
		something = cast(Variant)v;
	}
	
}

void main(){
	auto a = Alpha!(int)( 6);
	auto b = Alpha!(string)( "hello");
	auto l = new Alpha[](2);
	l[0] = a;
	l[1] = b;
}

but that do not works.

Someone know a trick?

thanks
January 27, 2015
I can do this
import std.variant;

struct Alpha {
	Variant something;
	
	this(Variant v){
		something = v;
	}
	
	static Alpha build(T)(T v){
		return Alpha( cast(Variant)v );
	}
	
}

void main(){
	auto a = Alpha.build!(int)( 6);
	auto b = Alpha.build!(string)( "hello");
	auto l = new Alpha[](2);
	l[0] = a;
	l[1] = b;
}

If someone has better
January 27, 2015
On Tue, 27 Jan 2015 20:46:59 +0000, bioinfornatics wrote:

> void main(){
> 	auto a = Alpha!(int)( 6);
> 	auto b = Alpha!(string)( "hello");

The Alpha struct is not a template, only the constructor is.  Remove the explicit instantiations and IFTI does the work:
> void main(){
> 	auto a = Alpha( 6);
> 	auto b = Alpha( "hello");

January 27, 2015
On Tuesday, 27 January 2015 at 21:00:16 UTC, Justin Whear wrote:
> On Tue, 27 Jan 2015 20:46:59 +0000, bioinfornatics wrote:
>
>> void main(){
>> 	auto a = Alpha!(int)( 6);
>> 	auto b = Alpha!(string)( "hello");
>
> The Alpha struct is not a template, only the constructor is.  Remove the
> explicit instantiations and IFTI does the work:
>> void main(){
>> 	auto a = Alpha( 6);
>> 	auto b = Alpha( "hello");

Oh really cool
January 27, 2015
On Tue, 27 Jan 2015 21:55:37 +0000, bioinfornatics wrote:

> On Tuesday, 27 January 2015 at 21:00:16 UTC, Justin Whear wrote:
>> On Tue, 27 Jan 2015 20:46:59 +0000, bioinfornatics wrote:
>>
>>> void main(){
>>> 	auto a = Alpha!(int)( 6);
>>> 	auto b = Alpha!(string)( "hello");
>>
>> The Alpha struct is not a template, only the constructor is. Remove the explicit instantiations and IFTI does the work:
>>> void main(){
>>> 	auto a = Alpha( 6);
>>> 	auto b = Alpha( "hello");
> 
> Oh really cool

or this:

  import std.variant;

  struct Alpha {
    Variant something;

    this(T) (T v) {
      something = cast(Variant)v;
    }
  }

  void main () {
    Alpha a = 6;
    Alpha b = "hello";
  }