Thread overview | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
June 27, 2014 Enum type deduction inside templates is not working | ||||
---|---|---|---|---|
| ||||
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? |
June 27, 2014 Re: Enum type deduction inside templates is not working | ||||
---|---|---|---|---|
| ||||
Posted in reply to Uranuz | 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);
}
|
June 27, 2014 Re: Enum type deduction inside templates is not working | ||||
---|---|---|---|---|
| ||||
Posted in reply to pgtkda | 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
|
June 27, 2014 Re: Enum type deduction inside templates is not working | ||||
---|---|---|---|---|
| ||||
Posted in reply to pgtkda | 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?
|
June 27, 2014 Re: Enum type deduction inside templates is not working | ||||
---|---|---|---|---|
| ||||
Posted in reply to Uranuz | 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);
}
|
June 27, 2014 Re: Enum type deduction inside templates is not working | ||||
---|---|---|---|---|
| ||||
Posted in reply to pgtkda | > 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)
|
June 27, 2014 Re: Enum type deduction inside templates is not working | ||||
---|---|---|---|---|
| ||||
Posted in reply to Uranuz | 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. |
June 27, 2014 Re: Enum type deduction inside templates is not working | ||||
---|---|---|---|---|
| ||||
Posted in reply to Uranuz | There is proposal exists for this topic http://wiki.dlang.org/DIP40 |
June 27, 2014 Re: Enum type deduction inside templates is not working | ||||
---|---|---|---|---|
| ||||
Posted in reply to Uranuz | 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... |
June 27, 2014 Re: Enum type deduction inside templates is not working | ||||
---|---|---|---|---|
| ||||
Posted in reply to Uranuz | 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);
}
|
Copyright © 1999-2021 by the D Language Foundation