digitalmars.D.learn - Enum type deduction inside templates is not working
- Uranuz (24/24) Jun 26 2014 Compiler can't deduce type for template struct Pair when using it
- pgtkda (15/39) Jun 26 2014 is this a solution for your problem?
- pgtkda (3/4) Jun 26 2014 sorry, i should read better
- Uranuz (5/9) Jun 26 2014 Ok. Maybe it was discussed already somewhere, but I am not god in
- pgtkda (19/29) Jun 26 2014 I think, D is a typesafe language, therefore you can't use
- Uranuz (5/23) Jun 27 2014 Ok. I know that D is typesafe language, but I'm not going to do
- Uranuz (6/6) Jun 27 2014 Seems that I found answer myself. As far as I understand type
- Uranuz (2/2) Jun 27 2014 There is proposal exists for this topic
- Chris Nicholson-Sauls (16/18) Jun 27 2014 Sometimes we get spoiled by all the amazing/nifty things that do
- H. S. Teoh via Digitalmars-d-learn (18/48) Jun 27 2014 Try this:
- Chris Nicholson-Sauls (3/4) Jun 27 2014 Get out of my head!
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
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
On Friday, 27 June 2014 at 06:12:57 UTC, pgtkda wrote: How I could make thissorry, i should read betterworking without explicit specifying of types?
Jun 26 2014
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 thisOk. 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?sorry, i should read betterworking without explicit specifying of types?
Jun 26 2014
On Friday, 27 June 2014 at 06:21:11 UTC, Uranuz wrote:On Friday, 27 June 2014 at 06:14:48 UTC, pgtkda wrote: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); }On Friday, 27 June 2014 at 06:12:57 UTC, pgtkda wrote: How I could make thisOk. 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?sorry, i should read betterworking without explicit specifying of types?
Jun 26 2014
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
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
There is proposal exists for this topic http://wiki.dlang.org/DIP40
Jun 27 2014
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
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
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