digitalmars.D.learn - Get template and its instantiation parameters
- Michal Minich (26/26) Oct 07 2009 If one has a template instance, is it possible to get template name and ...
- BCS (2/15) Oct 07 2009 You could try parsing T.stringof at compiletime to extract the parts you...
- Jarrett Billingsley (10/26) Oct 07 2009 This is *exactly* the kind of bullshit that I hate about string
- Michal Minich (32/59) Oct 07 2009 Jarrett,
- BCS (4/11) Oct 07 2009 The question was how to do somthing now. If the best solution isn't that...
- Christopher Wright (5/23) Oct 07 2009 No, you can't. You can try parsing demangle!(T.mangleof) at compiletime
If one has a template instance, is it possible to get template name and parameter type that was used for instantiating, at compile time? consider: class List (T) {} List!(int) lst; Foo (lst); I want to create such template Foo which prints: List!(int) List int Something simiar for Arrays can be created: int[char] x; Foo (x); template Foo (X : T[U], T, U) { void Foo (X arr) { writeln(typeof(arr).stringof); writeln(T.stringof); writeln(U.stringof); } } but similar template for template instances does not work: template Foo (X : T!(U), T, U) // does not matches List!(int) when is changed to something more specific, it starts working: template Foo (X : List!(U), U) // matches List!(int)
Oct 07 2009
Hello Michal,If one has a template instance, is it possible to get template name and parameter type that was used for instantiating, at compile time? consider: class List (T) {} List!(int) lst; Foo (lst); I want to create such template Foo which prints: List!(int) List intYou could try parsing T.stringof at compiletime to extract the parts you need.
Oct 07 2009
On Wed, Oct 7, 2009 at 12:54 PM, BCS <none anon.com> wrote:Hello Michal,This is *exactly* the kind of bullshit that I hate about string mixins. The thought process just ends up going "oh, why bother having any terse, elegant mechanisms to get at program information when you can *parse arbitrary code at compile time*? And why do you need macros when a string substitution will do?" Is it powerful? Sure. But it's lame, ugly, slow, easy to mess up, lacks hygiene, and is unidiomatic. String mixins are basically just a text preprocessor with user-defined functionality. Neat, but it can only get you so far before you're in tarpit territory.If one has a template instance, is it possible to get template name and parameter type that was used for instantiating, at compile time? consider: class List (T) {} List!(int) lst; Foo (lst); I want to create such template Foo which prints: List!(int) List intYou could try parsing T.stringof at compiletime to extract the parts you need.
Oct 07 2009
On Wed, 07 Oct 2009 13:15:46 -0400, Jarrett Billingsley wrote:On Wed, Oct 7, 2009 at 12:54 PM, BCS <none anon.com> wrote:Jarrett, I agree with you except the "lame" and "preprocessor" part. But I wouldn't be so harsh on D. The prospect of parsing D code really does not appeal to me. Also the usage of advanced templates is not very intuitive. But please don't forget that features that are made possible by D templates and/or the ".stringof" + CTFE are not available in many languages. Languages that have such or better type level expressivity as of Scala). Also these features in D are more general, and even if kludgy, they are *available*. The answer to my question is: The problem was that parameter T needs to be "alias" because it should match "List" which is not complete type, until is applied to some argument (int); it is just template name. The result for T.stringof is surprising for me, but anyway, I get what I needed. class List (T) {} void main () { List!(int) lst; Foo (lst); } template Foo (C : T!(U), alias T, U) { void Foo (C container) { writeln(C.stringof); // List writeln(T.stringof); // List(T) writeln(U.stringof); // int } } (tested on DMD 2.033)Hello Michal,This is *exactly* the kind of bullshit that I hate about string mixins. The thought process just ends up going "oh, why bother having any terse, elegant mechanisms to get at program information when you can *parse arbitrary code at compile time*? And why do you need macros when a string substitution will do?" Is it powerful? Sure. But it's lame, ugly, slow, easy to mess up, lacks hygiene, and is unidiomatic. String mixins are basically just a text preprocessor with user-defined functionality. Neat, but it can only get you so far before you're in tarpit territory.If one has a template instance, is it possible to get template name and parameter type that was used for instantiating, at compile time? consider: class List (T) {} List!(int) lst; Foo (lst); I want to create such template Foo which prints: List!(int) List intYou could try parsing T.stringof at compiletime to extract the parts you need.
Oct 07 2009
Hello Jarrett,On Wed, Oct 7, 2009 at 12:54 PM, BCS <none anon.com> wrote:The question was how to do somthing now. If the best solution isn't that good, it says a few things, but what it does /not/ say is that you shouldn't do the best solution.You could try parsing T.stringof at compiletime to extract the parts you need.This is *exactly* the kind of bullshit that I hate about string mixins.
Oct 07 2009
BCS wrote:Hello Michal,No, you can't. You can try parsing demangle!(T.mangleof) at compiletime to extract the parts you need. stringof is a morass of inconsistency and partial information. Whenever a template gets within a mile of a type, all bets are off.If one has a template instance, is it possible to get template name and parameter type that was used for instantiating, at compile time? consider: class List (T) {} List!(int) lst; Foo (lst); I want to create such template Foo which prints: List!(int) List intYou could try parsing T.stringof at compiletime to extract the parts you need.
Oct 07 2009