www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Enum type deduction inside templates is not working

reply "Uranuz" <neuranuz gmail.com> writes:
Compiler can't deduce type for template struct Pair when using it 
with enum argument.  There is an example

import std.stdio;

enum Category { first, second, third };

struct Pair(F, S)
{
	F first;
	S second;
	
	this(F f, S s)
	{
		first = f;
		second = s;
	}
}


void main()
{
	auto p = Pair(Category.first, "first"); //It fails
	
	writeln(p);
}

Is it not working for some reason or I'm doing something wrong or 
is it just lack of implementation? How I could make this working 
without explicit specifying of types?
Jun 26 2014
next sibling parent reply "pgtkda" <dlang byom.de> writes:
On Friday, 27 June 2014 at 06:04:20 UTC, Uranuz wrote:
 Compiler can't deduce type for template struct Pair when using 
 it with enum argument.  There is an example

 import std.stdio;

 enum Category { first, second, third };

 struct Pair(F, S)
 {
 	F first;
 	S second;
 	
 	this(F f, S s)
 	{
 		first = f;
 		second = s;
 	}
 }


 void main()
 {
 	auto p = Pair(Category.first, "first"); //It fails
 	
 	writeln(p);
 }

 Is it not working for some reason or I'm doing something wrong 
 or is it just lack of implementation? How I could make this 
 working without explicit specifying of types?
is this a solution for your problem? enum Category { first, second, third }; struct Pair { Category cat; string second; this(Category cat, string second){ this.cat = cat, this.second = second; } } void main(){ auto p = Pair(Category.first, "first"); writeln(p); }
Jun 26 2014
parent reply "pgtkda" <dlang byom.de> writes:
On Friday, 27 June 2014 at 06:12:57 UTC, pgtkda wrote:
How I could make this
 working without explicit specifying of types?
sorry, i should read better
Jun 26 2014
parent reply "Uranuz" <neuranuz gmail.com> writes:
On Friday, 27 June 2014 at 06:14:48 UTC, pgtkda wrote:
 On Friday, 27 June 2014 at 06:12:57 UTC, pgtkda wrote:
 How I could make this
 working without explicit specifying of types?
sorry, i should read better
Ok. Maybe it was discussed already somewhere, but I am not god in searching in English. Is there any directions about it? How could I work around it? Should I mail some proposal or bug report for it?
Jun 26 2014
parent reply "pgtkda" <dlang byom.de> writes:
On Friday, 27 June 2014 at 06:21:11 UTC, Uranuz wrote:
 On Friday, 27 June 2014 at 06:14:48 UTC, pgtkda wrote:
 On Friday, 27 June 2014 at 06:12:57 UTC, pgtkda wrote:
 How I could make this
 working without explicit specifying of types?
sorry, i should read better
Ok. Maybe it was discussed already somewhere, but I am not god in searching in English. Is there any directions about it? How could I work around it? Should I mail some proposal or bug report for it?
I think, D is a typesafe language, therefore you can't use variables with no type declaration. One thing you can search for, are templates but even there you have to define a type: import std.stdio; enum Category : string { first = "first"} template Pair(T) { T t; T cat; } void main() { alias Pair!(string) a; a.cat = Category.first; a.t = "first"; writeln(a.cat, " . ", a.t); }
Jun 26 2014
parent reply "Uranuz" <neuranuz gmail.com> writes:
 I think, D is a typesafe language, therefore you can't use 
 variables with no type declaration.

 One thing you can search for, are templates but even there you 
 have to define a type:

 import std.stdio;

 enum Category : string { first = "first"}

 template Pair(T)
 {
 	T t;
 	T cat;
 }


 void main()
 {
 	alias Pair!(string) a;
 	a.cat = Category.first;
 	a.t = "first";

 	writeln(a.cat, " . ", a.t);
 }
Ok. I know that D is typesafe language, but I'm not going to do some implicit type casts in there, because type of Category.first is Category itself but not string or something. In this example `a.cat = Category.first;` tries to make implicit cast (I don't remember is it allowed or not)
Jun 27 2014
parent reply "Uranuz" <neuranuz gmail.com> writes:
Seems that I found answer myself. As far as I understand type 
inference is working only for template functions but not struct 
or class templates. This is why this not working and enum is not 
responsible for that.

I don't know why I use D enough long but I did not remember this 
fact.
Jun 27 2014
next sibling parent "Uranuz" <neuranuz gmail.com> writes:
There is proposal exists for this topic
http://wiki.dlang.org/DIP40
Jun 27 2014
prev sibling parent "Chris Nicholson-Sauls" <ibisbasenji gmail.com> writes:
On Friday, 27 June 2014 at 07:43:27 UTC, Uranuz wrote:
 I don't know why I use D enough long but I did not remember 
 this fact.
Sometimes we get spoiled by all the amazing/nifty things that do work, and expect comparable things like this to Just Work. To be honest, at first I didn't see any issue in what you were doing either... One thing you could do in the meantime is to use an instantiator function. This works just fine: auto pair(F, S)(F f, S s) { return Pair!(F, S)(f, s); } void main() { auto p = pair(Category.first, "first"); writeln(p); }
Jun 27 2014
prev sibling parent reply "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Fri, Jun 27, 2014 at 06:04:18AM +0000, Uranuz via Digitalmars-d-learn wrote:
 Compiler can't deduce type for template struct Pair when using it with
 enum argument.  There is an example
 
 import std.stdio;
 
 enum Category { first, second, third };
 
 struct Pair(F, S)
 {
 	F first;
 	S second;
 	
 	this(F f, S s)
 	{
 		first = f;
 		second = s;
 	}
 }
 
 
 void main()
 {
 	auto p = Pair(Category.first, "first"); //It fails
 	
 	writeln(p);
 }
 
 Is it not working for some reason or I'm doing something wrong or is
 it just lack of implementation? How I could make this working without
 explicit specifying of types?
Try this: struct Pair(F, S) { F first; S second; } auto pair(F,S)(F f, S s) { return Pair!(F,S)(f,s); } void main() { auto p = pair(Category.first, "first"); } T -- Don't throw out the baby with the bathwater. Use your hands...
Jun 27 2014
parent "Chris Nicholson-Sauls" <ibisbasenji gmail.com> writes:
On Friday, 27 June 2014 at 14:27:46 UTC, H. S. Teoh via
Digitalmars-d-learn wrote:
 Try this:
Get out of my head!
Jun 27 2014