Thread overview | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
February 21, 2007 Checking if a function exists | ||||
---|---|---|---|---|
| ||||
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
|
February 21, 2007 Re: Checking if a function exists | ||||
---|---|---|---|---|
| ||||
Posted in reply to Michiel | 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!
>
|
February 21, 2007 Re: Checking if a function exists | ||||
---|---|---|---|---|
| ||||
Posted in reply to Michiel | 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
|
February 21, 2007 Re: Checking if a function exists | ||||
---|---|---|---|---|
| ||||
Posted in reply to Michiel | 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.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). |
February 21, 2007 Re: Checking if a function exists | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tyler Knott | Tyler Knott 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.learn&article_id=6577) thread in digitalmars.D.learn. It answers your question exactly. 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. > 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 |
February 21, 2007 Re: Checking if a function exists | ||||
---|---|---|---|---|
| ||||
Posted in reply to Max Samukha | Max Samukha wrote: >> static if ( functionExists(char[] T.toString()) ) { ... } > > The way that I can think of is > > static if (is(typeof(&T.toString) == char[] function())) Thanks! That part works great. The only thing missing now is the toString(T) part. -- Michiel |
February 21, 2007 Re: Checking if a function exists | ||||
---|---|---|---|---|
| ||||
Posted in reply to david | 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 |
February 21, 2007 Re: Checking if a function exists | ||||
---|---|---|---|---|
| ||||
Posted in reply to Michiel | Michiel wrote: > Tyler Knott 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.learn&article_id=6577) >> thread in digitalmars.D.learn. It answers your question exactly. > > 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. > >> 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. > 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."); } |
February 21, 2007 Re: Checking if a function exists | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tyler Knott | For all types you can use T.init as the dummy value to the function (I just remembered its existence). |
February 21, 2007 Re: Checking if a function exists | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tyler Knott | 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.
|
Copyright © 1999-2021 by the D Language Foundation