Re: [NTLK] Methods in the app's base view - how do I actually INVOKE them?

From: Simon Bell <>
Date: Sat Oct 04 2008 - 07:33:14 EDT

On 4 Oct 2008, at 03:18, Anthony Martinez wrote:

> I've started working on my first NewtonScript app

Welcome to the NewtonScript brotherhood!

> trying to call [a] function from inside, say, a buttonClickScript,
> I just can't do it.
> I'm sure I'm missing something trivial here

You’re going to kick yourself...

> I've tried saying
> x := nstrToCstr(foo); /* NewtonScript compiler complains about
> undefined global function */

This is the global function call syntax; and you haven’t defined a
global function.

> x := myApp:nstrToCstr(foo); /* Compiles, but newton gives an
> undefined variable error */

This is the right idea, a message send; but the receiver myApp would
need to be a global variable (which you haven’t defined). All you
need to do is omit the myApp and so long as the nstrToCstr slot is in
the inheritance chain of the caller (which it should be if your
button is in your app’s base view) you‘re golden

       x := :nstrToCstr(foo);

> x := |myApp:MTP|.nstrToCstr(foo); /* Compiles, but newton gives
> an "expected frame, got nil" sort of error */

Wrong on a number of levels, I’m afraid. If |myApp:MTP| is your app
symbol you need to say
to get the app frame in the root view, but then you would need to
send it a message with the colon operator; using the dot operator
will return the function object itself.

By the way, your function
nstrToCstr: func(str) StuffCString(MakeBinary(StrLen(str)+1,
'binary), 0, str);

doesn’t do anything useful since StuffCString returns nil. I think
what you meant to say is

nstrToCstr: func(str)
    local bin := MakeBinary(StrLen(str)+1, 'binary);
    StuffCString(bin, 0, str);
    return bin

> I've also tried declaring this function (nstrToCstr := func...) in
> another file and putting it before my main layout in the compilation
> order, with the same undefined-global-function error. I'd rather not
> create a truly global function, since as I understand that's rather
> antisocial.

Not always, but in this case there’s no need for it. There are two
types of global function -- those you define at compile time such as

DefineGlobalConstant('kNstrToCstrFunc, func(str)
    local bin := MakeBinary(StrLen(str)+1, 'binary);
    StuffCString(bin, 0, str);
    return bin
    end );

which creates a function object within the package (ie is global only
within the scope of the package) and you call with the call with
syntax (sic)

cstr := call kNstrToCstrFunc with ("something");

-- and those you copy into the Newton’s global functions frame and
call as in your first attempt above

DefGlobalFn('nstrToCstr, kNstrToCstrFunc);
cstr := nstrToCStr("something");

So, to recap:

o your nstrToCStr function needs to return the binary it makes
o send a message up the inheritance chain to find a function in your
app’s base view
    x := :nstrToCstr(foo);

Hope this gets you started

The NewtonTalk Mailing List -
The Official Newton FAQ -
The Newton Glossary -
WikiWikiNewt -
Received on Sat Oct 4 07:35:01 2008

This archive was generated by hypermail 2.1.8 : Sat Oct 04 2008 - 13:30:00 EDT