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

From: Anthony Martinez <pi_at_pihost.us>
Date: Sat Oct 04 2008 - 12:37:54 EDT

On Sat, Oct 04, 2008 at 12:33:14PM +0100, Simon Bell wrote:
> 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.
> <snip>
> > I'm sure I'm missing something trivial here
>
> You’re going to kick yourself...

:kick(self);

> > 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.

Noted.

> > 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);

I'd tried this and forgot to mention it, in fact, but now that you
mention it down below...

> > 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
> GetRoot(|myApp:MTP|)

This was me grasping at straws, to be honest. :)

> 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

I think _this_ is where I was getting one of my nil-ish errors from.
That'll teach me to read developer documentation late at night.

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

Thank you! This works perfectly. Print(StrHexDump()); gives me the
expected results. (knew it was something trivial, self successfully
kicked.)

> 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)
<snip>
> 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");

I saw that in another package and found it to be rather
inelegant-seeming and verbose.

> -- 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");

Which pollutes the global namespace, correct?

> Hope this gets you started

Indeed!

-- Pi

And it appears my signature is slightly long but relevant:

-- 
So like these two strings walk into a bar. One goes to the bartender...
"i'd like a jack and cokeEWdc#@@$@23Dwqxsa.xq.  qwqw0xx32"
The second one says, "you'll have to excuse my friend, he isn't null terminated."
<h3n> but the first one wan't really a c string. because null termination is
part of the definition. i guess you could say two pointers to char walked into
a bar.
==================================================================== 
The NewtonTalk Mailing List - http://www.newtontalk.net/
The Official Newton FAQ     - http://www.splorp.com/newton/faq/
The Newton Glossary         - http://www.splorp.com/newton/glossary/
WikiWikiNewt                - http://tools.unna.org/wikiwikinewt/
====================================================================
Received on Sat Oct 4 12:37:58 2008

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