www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Can't print enum values

reply Andrey <saasecondbox yandex.ru> writes:
Hello,
This code doesn't print enum values:
import std.meta;
import std.traits;
import std.stdio;

enum MyEnum : string
{
    First = "F_i_r_s_t",
    Second = "S_e_c_o_n_d"
}

alias QW(alias arg) = 
Alias!(cast(OriginalType!(typeof(arg)))arg);

void print(T...)(T args)
{
    writeln(cast(OriginalType!(typeof(args[0])))args[0]); // 
 Works...
    writeln(QW!(args[0]));                                // 
 Doesn't work... MUST!
 
    writeln(staticMap!(QW, args));                        // 
 Doesn't work... MUST!
}

void main()
{
    bool runTimeBool = true;
    print(MyEnum.First, 7, runTimeBool, MyEnum.Second);
}
Must print "F_i_r_s_t" and "S_e_c_o_n_d", not just "First" and "Second".
Aug 30 2018
parent reply vit <vit vit.vit> writes:
On Thursday, 30 August 2018 at 10:41:58 UTC, Andrey wrote:
 Hello,
 This code doesn't print enum values:
import std.meta;
import std.traits;
import std.stdio;

enum MyEnum : string
{
    First = "F_i_r_s_t",
    Second = "S_e_c_o_n_d"
}

alias QW(alias arg) = 
Alias!(cast(OriginalType!(typeof(arg)))arg);

void print(T...)(T args)
{
    writeln(cast(OriginalType!(typeof(args[0])))args[0]); // 
 Works...
    writeln(QW!(args[0]));                                // 
 Doesn't work... MUST!
 
    writeln(staticMap!(QW, args));                        // 
 Doesn't work... MUST!
}

void main()
{
    bool runTimeBool = true;
    print(MyEnum.First, 7, runTimeBool, MyEnum.Second);
}
Must print "F_i_r_s_t" and "S_e_c_o_n_d", not just "First" and "Second".
args are runtime arguments. import std.experimental.all; enum MyEnum : string { First = "F_i_r_s_t", Second = "S_e_c_o_n_d" } ///alias QW(alias arg) = Alias!(cast(OriginalType!(typeof(arg)))arg); auto QW(T)(const auto ref T x){ return cast(OriginalType!T)x; } void print(T...)(T args) { writeln(cast(OriginalType!(typeof(args[0])))args[0]); // Works... ///writeln(QW!(args[0])); // Doesn't work... MUST! writeln(QW(args[0])); static foreach(alias arg; args){ static if(is(typeof(arg) : MyEnum))write(QW(arg)); else write(arg); } write('\n'); //writeln(staticMap!(QW, args)); // Doesn't work... MUST! } void main() { bool runTimeBool = true; print(MyEnum.First, 7, runTimeBool, MyEnum.Second); }
Aug 30 2018
parent reply Andrey <saasecondbox yandex.ru> writes:
On Thursday, 30 August 2018 at 11:09:40 UTC, vit wrote:
 args are runtime arguments.

 import std.experimental.all;

 enum MyEnum : string
 {
     First = "F_i_r_s_t",
     Second = "S_e_c_o_n_d"
 }

 ///alias QW(alias arg) = 
 Alias!(cast(OriginalType!(typeof(arg)))arg);
 auto QW(T)(const auto ref T x){
 	return cast(OriginalType!T)x;
 }

 void print(T...)(T args)
 {
     writeln(cast(OriginalType!(typeof(args[0])))args[0]); // 
 Works...
     ///writeln(QW!(args[0]));                                // 
 Doesn't work... MUST!
     writeln(QW(args[0]));

     static foreach(alias arg; args){
         static if(is(typeof(arg) : MyEnum))write(QW(arg));
     	else write(arg);
     }
     write('\n');
     //writeln(staticMap!(QW, args));                        // 
 Doesn't work... MUST!
 }

 void main()
 {
     bool runTimeBool = true;
     print(MyEnum.First, 7, runTimeBool, MyEnum.Second);
 }
I want to create a reusable template for this purpose. Why I can't use "staticMap" so that compiler it self would do this:
void print(T...)(T args)
{
    writeln(cast(OriginalType!(typeof(args[0])))args[0], 
 cast(OriginalType!(typeof(args[1])))args[1], 
 cast(OriginalType!(typeof(args[2])))args[2], 
 cast(OriginalType!(typeof(args[3])))args[3]);
}
Just wrap some some symbol "args" with some expression at compile time. It is the same as in C++:
template<Args...>
void print(Args ...args)
{
    writeln(UnderlyingType(args)...); // UnderlyingType is a 
 function here
}
Aug 30 2018
parent reply vit <vit vit.vit> writes:
On Thursday, 30 August 2018 at 11:34:36 UTC, Andrey wrote:
 On Thursday, 30 August 2018 at 11:09:40 UTC, vit wrote:
 [...]
I want to create a reusable template for this purpose. Why I can't use "staticMap" so that compiler it self would do this:
[...]
Just wrap some some symbol "args" with some expression at compile time. It is the same as in C++:
[...]
D doesn't have expanding like C++ Unfortunately D has only simple automatic expading.
Aug 30 2018
parent reply Andrey <saasecondbox yandex.ru> writes:
On Thursday, 30 August 2018 at 12:04:26 UTC, vit wrote:
 On Thursday, 30 August 2018 at 11:34:36 UTC, Andrey wrote:
 On Thursday, 30 August 2018 at 11:09:40 UTC, vit wrote:
 [...]
I want to create a reusable template for this purpose. Why I can't use "staticMap" so that compiler it self would do this:
[...]
Just wrap some some symbol "args" with some expression at compile time. It is the same as in C++:
[...]
D doesn't have expanding like C++ Unfortunately D has only simple automatic expading.
So how can one implement a reusable template "apply some function to each argument in template parameter pack" in D?
Aug 31 2018
parent reply aliak <something something.com> writes:
On Friday, 31 August 2018 at 10:51:51 UTC, Andrey wrote:
 On Thursday, 30 August 2018 at 12:04:26 UTC, vit wrote:
 On Thursday, 30 August 2018 at 11:34:36 UTC, Andrey wrote:
 On Thursday, 30 August 2018 at 11:09:40 UTC, vit wrote:
 [...]
I want to create a reusable template for this purpose. Why I can't use "staticMap" so that compiler it self would do this:
[...]
Just wrap some some symbol "args" with some expression at compile time. It is the same as in C++:
[...]
D doesn't have expanding like C++ Unfortunately D has only simple automatic expading.
So how can one implement a reusable template "apply some function to each argument in template parameter pack" in D?
auto ToUnderlyingType(alias a)() { return cast(OriginalType!(typeof(a)))a; } void print(T...)(T args) { writeln(staticMap!(ToUnderlyingType, args)); }
Aug 31 2018
parent Andrey <saasecondbox yandex.ru> writes:
On Friday, 31 August 2018 at 12:21:48 UTC, aliak wrote:
 auto ToUnderlyingType(alias a)() {
     return cast(OriginalType!(typeof(a)))a;
 }

 void print(T...)(T args) {
     writeln(staticMap!(ToUnderlyingType, args));
 }
Oohhh. So easy! Killed 2 days - and templates and mixins tried... And the solution was to use TEMPLATE function with alias not regular... Thank you!
Aug 31 2018