Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

check if a function exists.

8 views
Skip to first unread message

maximean...@gmail.com

unread,
Jul 22, 2008, 8:15:37 AM7/22/08
to
Hello,

Having the name of a function, I would like to know if the function is
defined without testing it. Is it possible ?

Regards,

mathmax

maximean...@gmail.com

unread,
Jul 22, 2008, 8:21:58 AM7/22/08
to
For example, having this :


cMacro := "TestFunc( nNumber )"

I would like to test if TestFunc exists before executing it. I don't
want to use a try-catch because I want to know if the function existe
before calling it.

luiz Rafael Culik Guimaraes

unread,
Jul 22, 2008, 8:27:10 AM7/22/08
to
hi

simple do


type("FUNCNAME")

it will return
UI if the function exist, otherwise will return U if the function dont exist

Regards
Luiz


maximean...@gmail.com

unread,
Jul 22, 2008, 10:01:40 AM7/22/08
to
On Jul 22, 2:27 pm, "luiz Rafael Culik Guimaraes"

Doesn't work by my side. Always return U.

FabioNery

unread,
Jul 22, 2008, 10:16:59 AM7/22/08
to
Try

TYPE( "MINHAFUNC()" )

and

FUNCTION MINHAFUNC() // don´t be "static"

///...
RETURN NIL


<maximean...@gmail.com> escreveu na mensagem
news:1299aedc-346e-46a0...@m45g2000hsb.googlegroups.com...

Eugene F.

unread,
Jul 22, 2008, 10:25:14 AM7/22/08
to
Luiz,

PMJI

I believe that Clipper's TYPE actually macro-evaluates the
expression. Is it the same with xHarbour?

If so, wouldn't it present a problem for a function with STATIC
variables?

For example, if you test for an existence of the following function:

FUNCTION Test()
STATIC nNum:=0
RETURN ++nNum

TIA, Eugene

======================================================
On Jul 22, 8:27 am, "luiz Rafael Culik Guimaraes"

Enrico Maria Giordano

unread,
Jul 22, 2008, 11:58:47 AM7/22/08
to

"Eugene F." <pm77...@gmail.com> ha scritto nel messaggio
news:97d6a26c-569f-43bd...@x35g2000hsb.googlegroups.com...

> I believe that Clipper's TYPE actually macro-evaluates the
> expression. Is it the same with xHarbour?
>
> If so, wouldn't it present a problem for a function with STATIC
> variables?
>
> For example, if you test for an existence of the following function:
>
> FUNCTION Test()
> STATIC nNum:=0
> RETURN ++nNum

Neither Clipper nor xHarbour evaluate (call) the function.

EMG

--
EMAG Software Homepage: http://www.emagsoftware.it
The EMG's ZX-Spectrum Page: http://www.emagsoftware.it/spectrum
The Best of Spectrum Games: http://www.emagsoftware.it/tbosg
The EMG Music page: http://www.emagsoftware.it/emgmusic


Xavi

unread,
Jul 22, 2008, 12:14:14 PM7/22/08
to
maximean...@gmail.com escribió:

Also .-

if __DynsIsFun( __DynsGetIndex( 'TestFunc' ) )
HB_ExecFromArray( {'TestFunc', nNumber} )
else
? "Not exist TestFunc."
endif

Best regards,
Xavi

Maurizio la Cecilia

unread,
Jul 22, 2008, 12:47:35 PM7/22/08
to
I think you could try:

#pragma BEGINDUMP

#include <hbapi.h>

HB_FUNC( ISFUNCTION )
{
PHB_DYNS sym = hb_dynsymFindName(hb_parc(1));

hb_retl( sym ? hb_dynsymIsFunction( sym ) : 0);
}

#pragma ENDDUMP

then call

IsFunction("symbolname") -> .t. if the symbol exists and is a function
reference

Some internal needed, but this worked here.
The function expects a symbolic function name, so in your example you have
to call
IsFunction("TestFunc")

Best regards.
Maurizio la Cecilia

<maximean...@gmail.com> ha scritto nel messaggio
news:3b5894a0-ac26-41ff...@34g2000hsh.googlegroups.com...

Ron Pinkas

unread,
Jul 22, 2008, 1:05:00 PM7/22/08
to
The standard Clipper Run-Time supports this functionality, using the Type()
function, as was already answered. Please read the docs instead of creating
superfluous solutions.

"Maurizio la Cecilia" <m.lac...@libero.it> wrote in message
news:Haohk.31659$Ca.1...@twister2.libero.it...

Maurizio la Cecilia

unread,
Jul 22, 2008, 1:40:54 PM7/22/08
to
Sorry for the superfluous function, but i posted asyncronously with thread
reading.
Thanks for your courtesy, however, but i made this because was signaled
Type() returning always "U" and i tried to find a workaround, not to
reinvent the wheel.

About the docs reading, maybe something would be more accurately precised
as in doc/var.txt the Type() reference says:

* The data type of expression is checked by invoking a macro compiler
* and by evaluation of generated code (if there is no syntax errors).

and this is the description of the xHarbour Language Reference Guide:
"Type() is used to determine the data type of a macro expression by
evaluating or executing it with the macro operator. The expression is most
often the name of a dynamic memory variable, or a field variable."

You see, without reading the subsequent post i haved some doubt about the
way Type() works.
Regards.


"Ron Pinkas" <R...@xHarbour.com> ha scritto nel messaggio
news:g6548e$2rl$1...@aioe.org...

Ron Pinkas

unread,
Jul 22, 2008, 4:11:12 PM7/22/08
to
Maurizio

The desvription could be improved but the included sample clearly shows:

? Type( "Test()" ) // result: UI
? Type( "TestFunc()" ) // result: U

The answer posted by Luiz also provided the same info.

FWIW, my experience is that reports such as:

"Doesn't work by my side. Always return U."

Should not receive great weight when compared to detailed technical
responses.

Ron

Maurizio la Cecilia

unread,
Jul 22, 2008, 7:11:32 PM7/22/08
to
Just for puntualize:
1. The thread started on the request: "I would like to test if TestFunc
exists before executing it" and the documentation says that Type() evaluates
the expression.
2. I think the text of documentation have greater weight than samples and
the samples, however, shows the returned values, not if the function is
evaluated or not.
3. You saw i missed to read the documentation, this isn't true as was the
doc that left me in doubt.
4. If i've posted a superfluous function was only to contribute in the
thread and was due to the delay of Google Reader in updating the feeds; the
code i reported surely don't evaluates the function, nobody is obliged to
use it, neither to add to public code, and anyway could be an example on the
use of some xHarbour extensions. Not a great harm, i think, to receive a
reproach.

Maurizio

"Ron Pinkas" <R...@xHarbour.com> ha scritto nel messaggio

news:g65f5h$n3t$1...@aioe.org...

Rossine

unread,
Jul 23, 2008, 8:15:45 AM7/23/08
to
Hello all,

With this example you test, "functions", "vars" and "expressions":

[CODE]
*************
function MAIN
*************

cls

? "Variável <var1> existe ?: ", ExpValid( "var1" )

private var1 := "1"

? "Variável <var1> existe ?: ", ExpValid( "var1" )

? "Expressão é valida <alltrim(var1)> ?: ", ExpValid( "alltrim(var1)" )

? "Expressão é valida <allllltrim(var1)> ?: ", ExpValid(
"allllltrim(var1)" )

? "A função existe <TESTE> ?: ", ExpValid( "TESTE()" )

? "A função existe <TESTE_1> ?: ", ExpValid( "TESTE_1()" )

? "A função existe <TESTE_2> ?: ", ExpValid( "TESTE_2()" )

return NIL

*****************
FUNCTION ExpValid( cExp )
*****************

local lOk := .T., xResult
local bError := ErrorBlock( { |e| ExpError( e, bError ) } )

cExp := alltrim(cExp)

BEGIN SEQUENCE
if !Empty( cExp )
lOk := .t.
xResult := &cExp
if !lOk
? "Tipo de expressão inválida."
endif
endif
RECOVER
lOk := .F.
END SEQUENCE

ErrorBlock( bError )

return lOk

************************
STATIC FUNCTION ExpError( e, bOldErr )
************************

if e:subSystem == "BASE"
if procname(7) = "ENTRADA" .or. procname(8) = "ENTRADA" .or.
procname(10)= "EDITAREF"
? e:descriptio +". Expressão Inválida."
endif
BREAK e
endif

return Eval( bOldErr, e )

**************
function TESTE
**************

? "Olá eu sou uma função PUBLICA (public)"

return NIL

***********************
static function TESTE_1
***********************

? "Olá eu sou uma função ESTÁTICA (static)"

return NIL

[ENDCODE]

Regards,

Rossine.

"Maurizio la Cecilia" <m.lac...@libero.it> escreveu na mensagem
news:EOthk.121232$FR.3...@twister1.libero.it...

0 new messages