digitalmars.D - Checking if a function exists
- Michiel (8/8) Feb 21 2007 I'm not quite sure how to do this, but I'm sure one of you does. I need
- david (5/16) Feb 21 2007 Look in d.D.learn, there's a recent topic named
- Michiel (5/8) Feb 21 2007 See other reply.
- Max Samukha (4/10) Feb 21 2007 The way that I can think of is
- Michiel (5/10) Feb 21 2007 Thanks! That part works great. The only thing missing now is the
- Tyler Knott (9/20) Feb 21 2007 Check out this
- Michiel (13/34) Feb 21 2007 Well, not exactly. They only check for the function name, while I also
- Tyler Knott (15/54) Feb 21 2007 Hmm... I've only ever tried to get backwards (from knowing the function ...
- Tyler Knott (1/1) Feb 21 2007 For all types you can use T.init as the dummy value to the function (I j...
- Max Samukha (6/7) Feb 21 2007 This works for module scope functions:
- Michiel (7/14) Feb 21 2007 That doesn't work if there are multiple overloads of the same function,
- Max Samukha (2/14) Feb 21 2007 Yes, my mistake
- Frits van Bommel (3/5) Feb 21 2007 Doesn't work for static arrays IIRC, since typeof(T[N].init) == T (for
I'm not quite sure how to do this, but I'm sure one of you does. I need something like: static if ( functionExists(char[] .toString(T)) ) { ... } static if ( functionExists(char[] T.toString()) ) { ... } What is the real way to do this static check? Thanks! -- Michiel
Feb 21 2007
Look in d.D.learn, there's a recent topic named -> "Testing if a function is defined in a module" I guess that will help you. David Michiel schrieb:I'm not quite sure how to do this, but I'm sure one of you does. I need something like: static if ( functionExists(char[] .toString(T)) ) { ... } static if ( functionExists(char[] T.toString()) ) { ... } What is the real way to do this static check? Thanks!
Feb 21 2007
david wrote:Look in d.D.learn, there's a recent topic named -> "Testing if a function is defined in a module" I guess that will help you.See other reply. Thanks! -- Michiel
Feb 21 2007
On Wed, 21 Feb 2007 21:25:20 +0100, Michiel <nomail please.com> wrote:I'm not quite sure how to do this, but I'm sure one of you does. I need something like: static if ( functionExists(char[] .toString(T)) ) { ... } static if ( functionExists(char[] T.toString()) ) { ... } What is the real way to do this static check? Thanks!The way that I can think of is static if (is(typeof(&T.toString) == char[] function())) There might be others
Feb 21 2007
Max Samukha wrote:Thanks! That part works great. The only thing missing now is the toString(T) part. -- Michielstatic if ( functionExists(char[] T.toString()) ) { ... }The way that I can think of is static if (is(typeof(&T.toString) == char[] function()))
Feb 21 2007
Michiel wrote:I'm not quite sure how to do this, but I'm sure one of you does. I need something like: static if ( functionExists(char[] .toString(T)) ) { ... } static if ( functionExists(char[] T.toString()) ) { ... } What is the real way to do this static check? Thanks!Check out this (http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.le rn&article_id=6577) thread in digitalmars.D.learn. It answers your question exactly. For non-static member functions of classes you'll need to test against a reference to that class (although it doesn't need to be initialized). To get the return and parameter types you can use the templates in std.traits (note: those templates (and DMD itself) will only reveal the first overload of a function; there is no way to get the parameter list for others).
Feb 21 2007
Tyler Knott wrote:Well, not exactly. They only check for the function name, while I also need to check out the parameter. And if I really want to do it right, also the return type. But I suppose a function named toString will probably return char[], so that's less important.I'm not quite sure how to do this, but I'm sure one of you does. I need something like: static if ( functionExists(char[] .toString(T)) ) { ... } static if ( functionExists(char[] T.toString()) ) { ... } What is the real way to do this static check? Thanks!Check out this (http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=6577) thread in digitalmars.D.learn. It answers your question exactly.For non-static member functions of classes you'll need to test against a reference to that class (although it doesn't need to be initialized). To get the return and parameter types you can use the templates in std.traits (note: those templates (and DMD itself) will only reveal the first overload of a function; there is no way to get the parameter list for others).That's too bad. Because std.string.toString is exactly the function I want to check for (among other, self-defined toString functions). And there are 20 such functions, all with a different parameter type. I don't really need the whole list of parameters anyway. I only want to know if a function with a parameter type I specify exists or not. -- Michiel
Feb 21 2007
Michiel wrote:Tyler Knott wrote:Hmm... I've only ever tried to get backwards (from knowing the function name to knowing all the overloads). It seems that it's possible to find out if there's an overload for a specific parameter list: import std.string; class x{} class y{} extern char[] toString(x y); void main() { static if(is(typeof(toString(cast(x)null)))) pragma(msg, "This works."); //Note: you need to use the fully qualified name of toString if there's another toString in the local scope, //even if it uses a different overload which fits the type better. static if(is(typeof(std.string.toString(cast(uint)5)))) pragma(msg, "This too."); static if(is(typeof(toString(cast(y)null)))) pragma(msg, "Not this."); }Well, not exactly. They only check for the function name, while I also need to check out the parameter. And if I really want to do it right, also the return type. But I suppose a function named toString will probably return char[], so that's less important.I'm not quite sure how to do this, but I'm sure one of you does. I need something like: static if ( functionExists(char[] .toString(T)) ) { ... } static if ( functionExists(char[] T.toString()) ) { ... } What is the real way to do this static check? Thanks!Check out this (http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=6577) thread in digitalmars.D.learn. It answers your question exactly.For non-static member functions of classes you'll need to test against a reference to that class (although it doesn't need to be initialized). To get the return and parameter types you can use the templates in std.traits (note: those templates (and DMD itself) will only reveal the first overload of a function; there is no way to get the parameter list for others).That's too bad. Because std.string.toString is exactly the function I want to check for (among other, self-defined toString functions). And there are 20 such functions, all with a different parameter type. I don't really need the whole list of parameters anyway. I only want to know if a function with a parameter type I specify exists or not.
Feb 21 2007
For all types you can use T.init as the dummy value to the function (I just remembered its existence).
Feb 21 2007
On Wed, 21 Feb 2007 15:32:52 -0600, Tyler Knott <tywebmail mailcity.com> wrote:For all types you can use T.init as the dummy value to the function (I just remembered its existence).This works for module scope functions: static if (is(typeof(&.toString) == char[] function(T))) // '.' is the module scope operator or use fully qualified name. Trickier ways might exist.
Feb 21 2007
Max Samukha wrote:That doesn't work if there are multiple overloads of the same function, like with toString. I just tested it. Tyler's method worked, though. Thanks! -- MichielFor all types you can use T.init as the dummy value to the function (I just remembered its existence).This works for module scope functions: static if (is(typeof(&.toString) == char[] function(T))) // '.' is the module scope operator or use fully qualified name. Trickier ways might exist.
Feb 21 2007
On Wed, 21 Feb 2007 23:08:16 +0100, Michiel <nomail please.com> wrote:Max Samukha wrote:Yes, my mistakeThat doesn't work if there are multiple overloads of the same function, like with toString. I just tested it. Tyler's method worked, though. Thanks!For all types you can use T.init as the dummy value to the function (I just remembered its existence).This works for module scope functions: static if (is(typeof(&.toString) == char[] function(T))) // '.' is the module scope operator or use fully qualified name. Trickier ways might exist.
Feb 21 2007
Tyler Knott wrote:For all types you can use T.init as the dummy value to the function (I just remembered its existence).Doesn't work for static arrays IIRC, since typeof(T[N].init) == T (for constant integer N)...
Feb 21 2007