digitalmars.D - IsExpression to check for method
- John Demme (16/16) Jun 20 2005 OK... So far I'm liking this IsExpression thing, but either I'm hitting
-
Stewart Gordon
(19/25)
Jun 21 2005
-
Walter
(9/19)
Jun 23 2005
It's a feature, not a bug
. The feature is that if the type inside th... - Derek Parnell (14/37) Jun 23 2005 Can you supply an example of how to test for the existence of ...
- Walter (8/19) Jun 24 2005 existence
- Derek Parnell (11/34) Jun 24 2005 But that is exactly what I'd like to prevent! In other words I'd like to...
- Sean Kelly (24/54) Jun 24 2005 I like the new features, but using "is" in this way (since it's also a
- pragma (5/16) Jun 24 2005 I agree. D should probably use something like istype(), exists(), or j...
- Unknown W. Brackets (5/7) Jun 24 2005 Aha! This is what I got wrong when testing for function existance.
- Walter (4/8) Jun 24 2005 It's in the documentation: "The condition is satisfied if Type is
- Unknown W. Brackets (22/36) Jun 24 2005 Well, then, forgive me for being daft but upon reading that twice I
- Stewart Gordon (18/30) Jun 24 2005 For whatever reason? Then
- Walter (6/22) Jun 24 2005 the
-
Stewart Gordon
(12/21)
Jun 24 2005
- Sean Kelly (3/6) Jun 24 2005 I was wondering about this as well.
- Walter (3/20) Jun 24 2005 No, it isn't parseable as a type.
- Stewart Gordon (14/23) Jul 11 2005 Exactly. Therefore
-
Stewart Gordon
(31/34)
Jun 22 2005
OK... So far I'm liking this IsExpression thing, but either I'm hitting a limitation, or I'm not using it right. I basically want to determine if a method exists for a variable. I want the following snippet to print "hash", while it currently prints "nohash": Object o = new Object; static if( is(o.toHash()) ) { writefln("hash"); } else { writefln("nohash"); } This is in a template, so I can use the type instead of the instance, if needed. Is there any way for me to do this? If there isn't currently, can it be added? It'd be really, really useful. And cool. BTW, except for this issue I'm really liking this new addition. Props to Walter. Thanks John Demme
Jun 20 2005
John Demme wrote:OK... So far I'm liking this IsExpression thing, but either I'm hitting a limitation, or I'm not using it right. I basically want to determine if a method exists for a variable. I want the following snippet to print "hash", while it currently prints "nohash": Object o = new Object; static if( is(o.toHash()) ) {<snip> It would appear to be a bug that this complies - it isn't among the listed forms of IsExpression. I don't know if there's a way to do it, but I guess it wouldn't be this - it looks related to the _evaluation_ of o.toHash rather than the function itself. I'm guessing that something like this would work static if (is(typeof(&o.toHash) : T delegate())) { ... } but it might not work if the function name is overloaded, depending on how the compiler resolves the typeof. OTOH something like this might work: uint delegate() dummy; static if (is(typeof(dummy = &o.toHash))) { ... } But yes, there ought to be a clear way of doing this. Maybe when I'm in a position to experiment.... Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Jun 21 2005
"Stewart Gordon" <smjg_1998 yahoo.com> wrote in message news:d98uen$2tg1$1 digitaldaemon.com...John Demme wrote:It's a feature, not a bug <g>. The feature is that if the type inside the is( ) parentheses fails to compile *for whatever reason* then the IsExpression result is false. In this case, o.toHash() is not a type, so it fails to compile, and IsExpression returns false. This capability can be manipulated in diverse ways to test for the existence of methods, members, the types of them, etc.OK... So far I'm liking this IsExpression thing, but either I'm hitting a limitation, or I'm not using it right. I basically want to determine if a method exists for a variable. I want the following snippet to print "hash", while it currently prints "nohash": Object o = new Object; static if( is(o.toHash()) ) {<snip> It would appear to be a bug that this complies - it isn't among the listed forms of IsExpression.
Jun 23 2005
On Thu, 23 Jun 2005 22:30:10 -0700, Walter wrote:"Stewart Gordon" <smjg_1998 yahoo.com> wrote in message news:d98uen$2tg1$1 digitaldaemon.com...Can you supply an example of how to test for the existence of ... ... a module function ... a module variable ... a class member ... a struct member ... a class declaration ... a struct declaration ... a module importation ... a function member -- Derek Melbourne, Australia 24/06/2005 3:40:32 PMJohn Demme wrote:It's a feature, not a bug <g>. The feature is that if the type inside the is( ) parentheses fails to compile *for whatever reason* then the IsExpression result is false. In this case, o.toHash() is not a type, so it fails to compile, and IsExpression returns false. This capability can be manipulated in diverse ways to test for the existence of methods, members, the types of them, etc.OK... So far I'm liking this IsExpression thing, but either I'm hitting a limitation, or I'm not using it right. I basically want to determine if a method exists for a variable. I want the following snippet to print "hash", while it currently prints "nohash": Object o = new Object; static if( is(o.toHash()) ) {<snip> It would appear to be a bug that this complies - it isn't among the listed forms of IsExpression.
Jun 23 2005
"Derek Parnell" <derek psych.ward> wrote in message news:w3vzckxoa968.nxmmy66f775t$.dlg 40tude.net...existenceThis capability can be manipulated in diverse ways to test for theIn general, the idea is to construct an expression which will fail to compile if it is *not* one of the above. For example, to test for the existence of member 'm' of struct 'S': static if ( is(typeof(S.m)) ) ...of methods, members, the types of them, etc.Can you supply an example of how to test for the existence of ... ... a module function ... a module variable ... a class member ... a struct member ... a class declaration ... a struct declaration ... a module importation ... a function member
Jun 24 2005
On Fri, 24 Jun 2005 00:09:21 -0700, Walter wrote:"Derek Parnell" <derek psych.ward> wrote in message news:w3vzckxoa968.nxmmy66f775t$.dlg 40tude.net...But that is exactly what I'd like to prevent! In other words I'd like to do this sort of thing ... static_if (! exists(S.m)) { // declare 'm' } -- Derek Melbourne, Australia 24/06/2005 5:21:06 PMexistenceThis capability can be manipulated in diverse ways to test for theIn general, the idea is to construct an expression which will fail to compile if it is *not* one of the above. For example, to test for the existence of member 'm' of struct 'S': static if ( is(typeof(S.m)) ) ...of methods, members, the types of them, etc.Can you supply an example of how to test for the existence of ... ... a module function ... a module variable ... a class member ... a struct member ... a class declaration ... a struct declaration ... a module importation ... a function member
Jun 24 2005
In article <1f1np8srt6jk1.j36513qm0sd7.dlg 40tude.net>, Derek Parnell says...On Fri, 24 Jun 2005 00:09:21 -0700, Walter wrote:I like the new features, but using "is" in this way (since it's also a non-static binary operator) is somewhat confusing. In some respects I'd prefer a new keyowrd for this purpose--istype perhaps? That said, I think we may need the implementation as it is now if it's to work effectively with template code. Consider this (bad) example: Here we're filtering on whether or not E.T is a type, rather than whether E.T exists at all (and I just realized I could make a terrible joke about aliens, but I'm resisting the urge). Sean"Derek Parnell" <derek psych.ward> wrote in message news:w3vzckxoa968.nxmmy66f775t$.dlg 40tude.net...But that is exactly what I'd like to prevent! In other words I'd like to do this sort of thing ... static_if (! exists(S.m)) { // declare 'm' }existenceThis capability can be manipulated in diverse ways to test for theIn general, the idea is to construct an expression which will fail to compile if it is *not* one of the above. For example, to test for the existence of member 'm' of struct 'S': static if ( is(typeof(S.m)) ) ...of methods, members, the types of them, etc.Can you supply an example of how to test for the existence of ... ... a module function ... a module variable ... a class member ... a struct member ... a class declaration ... a struct declaration ... a module importation ... a function member
Jun 24 2005
In article <d9hb8g$2ucm$1 digitaldaemon.com>, Sean Kelly says...In article <1f1np8srt6jk1.j36513qm0sd7.dlg 40tude.net>, Derek Parnell says...I agree. D should probably use something like istype(), exists(), or just anything_other_than_is(). Overloading 'is' like this is a tad confusing to the eye. At first glance, it just doesn't look like valid D code (IMO). - EricAnderton at yahooBut that is exactly what I'd like to prevent! In other words I'd like to do this sort of thing ... static_if (! exists(S.m)) { // declare 'm' }I like the new features, but using "is" in this way (since it's also a non-static binary operator) is somewhat confusing. In some respects I'd prefer a new keyowrd for this purpose--istype perhaps?
Jun 24 2005
Aha! This is what I got wrong when testing for function existance. I don't mean to repeat myself, but is this going to find its way into the documentation? I would argue it's very useful (in fact I did, possibly not-to-successfully.) -[Unknown]static if ( is(typeof(S.m)) ) ...
Jun 24 2005
"Unknown W. Brackets" <unknown simplemachines.org> wrote in message news:d9hdfk$30f5$2 digitaldaemon.com...Aha! This is what I got wrong when testing for function existance. I don't mean to repeat myself, but is this going to find its way into the documentation? I would argue it's very useful (in fact I did, possibly not-to-successfully.)It's in the documentation: "The condition is satisfied if Type is semantically correct (it must be syntactically correct regardless)."
Jun 24 2005
Well, then, forgive me for being daft but upon reading that twice I still did not consider that it could be used to check for the existance of functions, types, and methods which do not currently exist. I now understand that, but it seems like a more useful feature than just one sentence. Maybe the documentation could at least describe that this: int main() { static if (is(typeof(someTypeOrFunctionThatDoesNotExist))) writef("This will never be output."); static if (is(typeof(Object.someMethodOrMemberThatDoesNotExist))) writef("This will never be output either."); } Will compile (and run) fine. You could probably give a better example, though. I just mean that the word "type" is used quite a number of times in the IsExpression documentation, and it is not immediately logical that this means that a typeof expression can be used with a value that does not exist. Now that I understand this, the various forms of is() make much more sense, in fact this looks interesting: static if (is(typeof(myfunc) T)) T* myfunc_fp = &myfunc; -[Unknown]"Unknown W. Brackets" <unknown simplemachines.org> wrote in message news:d9hdfk$30f5$2 digitaldaemon.com...Aha! This is what I got wrong when testing for function existance. I don't mean to repeat myself, but is this going to find its way into the documentation? I would argue it's very useful (in fact I did, possibly not-to-successfully.)It's in the documentation: "The condition is satisfied if Type is semantically correct (it must be syntactically correct regardless)."
Jun 24 2005
Walter wrote:"Stewart Gordon" <smjg_1998 yahoo.com> wrote in message news:d98uen$2tg1$1 digitaldaemon.com...<snip>For whatever reason? Then static if (is(/.,mnbv\][)) { ... } should compile?It would appear to be a bug that this complies - it isn't among the listed forms of IsExpression.It's a feature, not a bug <g>. The feature is that if the type inside the is( ) parentheses fails to compile *for whatever reason* then the IsExpression result is false.In this case, o.toHash() is not a type, so it fails to compile, and IsExpression returns false.You tell us that the IsExpression must be syntactically valid regardless. IsExpression: is ( Type ) is ( Type : TypeSpecialization ) is ( Type == TypeSpecialization ) is ( Type Identifier ) is ( Type Identifier : TypeSpecialization ) is ( Type Identifier == TypeSpecialization ) The OP's code isn't parseable as any of these, so according to this it's a syntax error.This capability can be manipulated in diverse ways to test for the existence of methods, members, the types of them, etc.-- My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Jun 24 2005
"Stewart Gordon" <smjg_1998 yahoo.com> wrote in message news:d9gj4d$1vge$1 digitaldaemon.com...Walter wrote:the"Stewart Gordon" <smjg_1998 yahoo.com> wrote in message news:d98uen$2tg1$1 digitaldaemon.com...<snip>It would appear to be a bug that this complies - it isn't among the listed forms of IsExpression.It's a feature, not a bug <g>. The feature is that if the type insideNo, as that is not syntactically correct.is( ) parentheses fails to compile *for whatever reason* then the IsExpression result is false.For whatever reason? Then static if (is(/.,mnbv\][)) { ... } should compile?Yes. I miswrote the "for whatever reason", as it still must be syntactically valid.In this case, o.toHash() is not a type, so it fails to compile, and IsExpression returns false.You tell us that the IsExpression must be syntactically valid regardless.
Jun 24 2005
Walter wrote:"Stewart Gordon" <smjg_1998 yahoo.com> wrote in message news:d9gj4d$1vge$1 digitaldaemon.com...<snip><snip> Exactly. Neither is the OP's snippet according to the forms of IsExpression allowed by the spec. Or is o.toHash() parseable as a type by some obscure feature none of us have discovered? Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.For whatever reason? Then static if (is(/.,mnbv\][)) { ... } should compile?No, as that is not syntactically correct.
Jun 24 2005
In article <d9hgh1$2j4$1 digitaldaemon.com>, Stewart Gordon says...Or is o.toHash() parseable as a type by some obscure feature none of us have discovered?I was wondering about this as well. Sean
Jun 24 2005
"Stewart Gordon" <smjg_1998 yahoo.com> wrote in message news:d9hgh1$2j4$1 digitaldaemon.com...Walter wrote:No, it isn't parseable as a type."Stewart Gordon" <smjg_1998 yahoo.com> wrote in message news:d9gj4d$1vge$1 digitaldaemon.com...<snip><snip> Exactly. Neither is the OP's snippet according to the forms of IsExpression allowed by the spec. Or is o.toHash() parseable as a type by some obscure feature none of us have discovered?For whatever reason? Then static if (is(/.,mnbv\][)) { ... } should compile?No, as that is not syntactically correct.
Jun 24 2005
Walter wrote:"Stewart Gordon" <smjg_1998 yahoo.com> wrote in message news:d9hgh1$2j4$1 digitaldaemon.com...<snip>Exactly. Therefore is(o.toHash()) isn't a syntactically valid IsExpression. Stewart. -- -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS/M d- s:- a->--- UB P+ L E W++ N+++ o K- w++ O? M V? PS- PE- Y? PGP- t- 5? X? R b DI? D G e++>++++ h-- r-- !y ------END GEEK CODE BLOCK------ My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.Or is o.toHash() parseable as a type by some obscure feature none of us have discovered?No, it isn't parseable as a type.
Jul 11 2005
John Demme wrote:OK... So far I'm liking this IsExpression thing, but either I'm hitting a limitation, or I'm not using it right. I basically want to determine if a method exists for a variable.<snip> This code tests for the presence of a method having the given signature. The dummy delegate is needed to disambiguate between overloadings. ---------- import std.stdio; class Qwert { void yuiop(int asdfg) {} int yuiop() { return 0; } } void main() { Qwert hjkl; //void delegate(int) zxcvb; //real delegate(char[]) zxcvb; int delegate() zxcvb; static if (is(typeof(zxcvb = &hjkl.yuiop))) { writefln("yes"); } else { writefln("no"); } } ---------- OTOH, if you merely want to test for the presence of a method with a given name without caring about its signature (don't ask me why you'd want to do this), static if (is(typeof(&hjkl.yuiop))) is adequate. Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Jun 22 2005