Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
December 06, 2009 Foo!("a").????? == `Foo!("a")` | ||||
---|---|---|---|---|
| ||||
I don't suppose there's an easy general way to get the paramaters of a templated type just from the type itself? Ie, a way to get around this: class Foo(char[] str) {} static assert(Foo!("a").stringof != Foo!("b").stringof) // ^ fails because it evaluates to "Foo" != "Foo" I can probably work around it in my current case by sticking str into a const member of Foo and using that together with .stringof, but thought I'd see if there was a better way. |
December 06, 2009 Re: Foo!("a").????? == `Foo!("a")` | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky | On Sat, Dec 5, 2009 at 5:36 PM, Nick Sabalausky <a@a.a> wrote:
> I don't suppose there's an easy general way to get the paramaters of a templated type just from the type itself? Ie, a way to get around this:
>
> class Foo(char[] str) {}
> static assert(Foo!("a").stringof != Foo!("b").stringof)
> // ^ fails because it evaluates to "Foo" != "Foo"
>
> I can probably work around it in my current case by sticking str into a const member of Foo and using that together with .stringof, but thought I'd see if there was a better way.
That should just be considered a bug I think. I think what stringof does is not detailed in the spec beyond "provides a string representation". Clearly just returning the base name of the template is not the most useful representation of a template type. Stringof clearly needs to have more thought put into it (as has been brought up many times in the past).
--bb
|
December 06, 2009 Re: Foo!("a").????? == `Foo!("a")` | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky | On Sun, 06 Dec 2009 02:36:58 +0100, Nick Sabalausky <a@a.a> wrote: > I don't suppose there's an easy general way to get the paramaters of a > templated type just from the type itself? Ie, a way to get around this: > > class Foo(char[] str) {} > static assert(Foo!("a").stringof != Foo!("b").stringof) > // ^ fails because it evaluates to "Foo" != "Foo" > > I can probably work around it in my current case by sticking str into a > const member of Foo and using that together with .stringof, but thought I'd > see if there was a better way. Use more parentheses. ( Foo!( "a" ) ).stringof gives the right result. Of course, this still seems rather weird. -- Simen |
December 06, 2009 Re: Foo!("a").????? == `Foo!("a")` | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen kjaeraas | "Simen kjaeraas" <simen.kjaras@gmail.com> wrote in message news:op.u4h55pbcvxi10f@biotronic-pc.home... > On Sun, 06 Dec 2009 02:36:58 +0100, Nick Sabalausky <a@a.a> wrote: > >> I don't suppose there's an easy general way to get the paramaters of a templated type just from the type itself? Ie, a way to get around this: >> >> class Foo(char[] str) {} >> static assert(Foo!("a").stringof != Foo!("b").stringof) >> // ^ fails because it evaluates to "Foo" != "Foo" >> >> I can probably work around it in my current case by sticking str into a >> const member of Foo and using that together with .stringof, but thought >> I'd >> see if there was a better way. > > Use more parentheses. ( Foo!( "a" ) ).stringof gives the right result. Of > course, this still seems rather weird. > Not in D1 unfortunately (or at least 1.051): ----------------------------------- class Foo(char[] str) {} pragma(msg, `Foo!("a").stringof: ` ~ Foo!("a").stringof); pragma(msg, `Foo!("b").stringof: ` ~ Foo!("b").stringof); pragma(msg, `(Foo!("a")).stringof: ` ~ (Foo!("a")).stringof); pragma(msg, `(Foo!("b")).stringof: ` ~ (Foo!("b")).stringof); template Bar1(T) { const char[] Bar1 = T.stringof; } template Bar2(T) { const char[] Bar2 = (T).stringof; } pragma(msg, `Bar1!(Foo!("a")): ` ~ Bar1!(Foo!("a"))); pragma(msg, `Bar1!(Foo!("b")): ` ~ Bar1!(Foo!("b"))); pragma(msg, `Bar2!(Foo!("a")): ` ~ Bar2!(Foo!("a"))); pragma(msg, `Bar2!(Foo!("b")): ` ~ Bar2!(Foo!("b"))); void main() {} ----------------------------------- Compiler output: ----------------------------------- Foo!("a").stringof: class Foo Foo!("b").stringof: class Foo (Foo!("a")).stringof: Foo (Foo!("b")).stringof: Foo Bar1!(Foo!("a")): Foo Bar1!(Foo!("b")): Foo Bar2!(Foo!("a")): Foo Bar2!(Foo!("b")): Foo ----------------------------------- Odder still, notice the "class " prepended to a couple of them. stringof truly is shit atm. |
December 08, 2009 Re: Foo!("a").????? == `Foo!("a")` | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky | "Nick Sabalausky" <a@a.a> wrote in message news:hff1qc$1h6e$1@digitalmars.com... >I don't suppose there's an easy general way to get the paramaters of a templated type just from the type itself? Ie, a way to get around this: > > class Foo(char[] str) {} > static assert(Foo!("a").stringof != Foo!("b").stringof) > // ^ fails because it evaluates to "Foo" != "Foo" > > I can probably work around it in my current case by sticking str into a const member of Foo and using that together with .stringof, but thought I'd see if there was a better way. > > Just for reference for anyone interested, in my own case I've worked around it like this: class Foo(char[] str) { static const char[] StringOf = "Foo!("~str.stringof~")"; } static assert(Foo!("a").StringOf == `Foo!("a")`); Similar workarounds can be done for other template signatures, like a custom-made function for enums, or a "static if(StringOf exists) use StringOf else use stringof" for general T types. |
Copyright © 1999-2021 by the D Language Foundation