Can we make mixin applications first class at the type level?

FeepingCreature feepingcreature at gmail.com
Thu Mar 21 11:55:44 UTC 2019


On Thursday, 21 March 2019 at 09:59:48 UTC, sighoya wrote:
> On Thursday, 21 March 2019 at 09:07:03 UTC, FeepingCreature 
> wrote:
>> I have no idea what you are trying to achieve, but I am 
>> willing to bet putting a type in a string is the wrong way to 
>> do it.
>>
>> Why don't you give an example of what *outcome* you are trying 
>> to achieve first, maybe in a language you are more familiar 
>> with, and then we can tell you how to do it in D.
>
> Thanks for your open-heartedness.
>
> My intention is to allow type calculus. For this you would 
> normally need first class types, i.e. types as values:
>
> Type type = int;

Why don't you do alias type = int;?

You can't redefine it, sure, but I don't see what mixing in a 
string buys you there, because that needs to be a compiletime 
variable and you can't redefine that either.
>
> which is not possible in D and may never be. But there is the 
> possibility to model it over mixins if the parser would allow 
> us to do so.
>
> For instance a tuple constructor:
>
> *(Type type1, Type type2, Type type3)=(type1,type2,type3)
>
> could be modeled with:
>
> mixin template stringToType(String s)
> {
>    s
> }
>
> *(String type1, String type2, String 
> type3)=(stringToType!type1,stringToType!type1,stringToType!type1)
>
>
> The idea in general is to generate structures generically over 
> computed types:
>
> mixin template mapType(Map!(String,String) 
> typeToTypeMapping,String selectedType)
> {
> const char[] s= typeToTypeMapping[selectedType];
> }
>
> struct S(T,S)
> {
>    T t;
>    S s;
> }
>
> Map!(String,String) mapping= {"Int"=>"String", "String"=>"Char"}
> S!(mapType!(mapping,"Int"),mapType!(mapping,"String")) s;
>
> Of course we could simply write the complete struct S as string 
> mixin, but most existing code isn't saved in this format 
> because string mixins suck (no syntax highlighting).
>

I still do not understand in any way what you are trying to 
achieve. std.typecons.tuple exists. std.meta exists. Templates 
let you do basically anything with types... This all sounds like 
gibberish to me. I feel you're probably committing an X-Y 
problem; you're talking excitedly about an overspecific solution 
to a problem that I can only dimly guess at.

All I can tell you is, I am willing to bet money that whatever 
problem you have, it has a simpler solution in D and string 
template mixins aren't related to it.


More information about the Digitalmars-d mailing list