The docs say "In other languages [...] the functionality provided by GetRef is referred to as a
function pointer [...]", not that it IS a function pointer. <Emphasis on "the functionality
provided"...>
GetRef builds (on the fly) and returns an IDispatchEx object. Here's the full contents of a
3/23/1999 post from Eric Lippert on the subject of GetRef().
For those who don't know who Eric is, he's a member of the MS Scripting Dev team that makes all of
this stuff work <g>! I assume he's been *way* too busy lately to post in the NGs (but probably not
too busy to lurk ;-)...
======================================================
Re: what do I mean by "first class functions"?
We language designers call a particular entity "first class" if it can be
manipulated like any other data. An excellent example is functions in
JScript. In JScript, functions are objects. They can be passed around,
printed out, modified, stuck into local variables, etc:
Example:
function square(x) { return x * x; }
var s;
s = square; // this does NOT call square. square is an object.
alert(square(5)); // 25
alert(s(6)); // 36
See, in JScript there is a difference between
x = square; // assign the function to x.
and
x = square(); // call the function, assign the return value to x.
Or, for another example, functions are first class in C++. You can obtain a
pointer to a function and pass it around like data. But classes are not
first class. In C++ you CAN'T say
class foo {};
x = foo;
y = new x;
In C++, functions are first class but classes are second class. In Dylan,
functions and classes are first class.
In VBScript, neither functions nor classes are first class. In VBScript:
function square(x) : square = x * x : end function
dim s
s = square(5) ' 25
s = square ' error, expected argument - functions are not first class
There is no way to pass a function around -- the only thing you can do to
functions in VBScript is _call_ them.
Now, this deficiency is a real pain in IE. In JScript, people do this kind
of thing all the time:
function click() { /* what ever */ }
button1.onclick = click;
That assigns the function click to the button's event handler. When the
button is clicked, IE calls the function. It would be nice if you could do
this from VBScript, but VBScript doesn't have first class functions. So we
added a little hack to allow this. In V5 you can say
function click
' whatever
end function
set button1.onclick = GetRef("click")
What GetRef does is it looks up the string "click" in the global name table
and sees if it resolves to a function. If it does, it constructs an object
whose default method is to call the named function. It then returns that
object, which is assigned to the button property. (Internally, this is
exactly what JScript is doing -- in JScript, functions are implemented as
objects whose default method is to call the object. But JScript constructs
the function objects when the script starts up, whereas VBScript constructs
them only on-the-fly when requested by GetRef. This is because constructing
all those objects is _expensive_ -- since we can get away with not doing it
in VBScript, we do.)
Eric
======================================================
--
Michael Harris
MVP Scripting
uh, wait. Isn't "GetRef" a pointer to a subroutine?
to quote the doc: "In other scripting and programming languages, the
functionality provided by GetRef is referred to as a function pointer, that
is, it points to the address of a procedure to be executed when the
specified event occurs".
cheers, jw
Quote:
> <...brief pause while loading the magic gun with silver bullets...>
> No, VBScript (thankfully) doesn't do pointers... I'd hate to think what
kinds of questions/problems
Quote:
> we'd see in the NGs if it did!!!
> --
> Michael Harris
> Microsoft MVP - Scripting