Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Exploring context and globals with Nu

From Tim in mailing list:

Global name definitions are put into the symbol table; that is
slightly more efficient than normal assignments (such as those made
with the set, macro, and function operators), which are kept in
NSDictionaries. It also makes them easier to use in Nu code that is
called from Objective-C without specifying a context, which you can do
by sending the evalWithContext: message to a parsed code object with a
nil argument. Usually that happens in Objective-C, but since you can
send nearly any ObjC message from Nu, here's a little demo in nush
[ed: I expanded it to show more information]:

   1  
   2  $ nush
   3  Nu Shell.
   4  % (parse "(puts 22)")
   5  (progn (puts 22))
   6  % (set prog (parse "(puts 22)"))
   7  (progn (puts 22))
   8  % (prog class)
   9  NuCell
  10  % (set progx (parse "(puts x)"))
  11  (progn (puts x))
  12  % (progx class)
  13  NuCell
  14  % (set x 23)
  15  23
  16  % (x class)
  17  NSCFNumber
  18  % (eval progx)
  19  23
  20  ()
  21  % (prog evalWithContext: nil)
  22  22
  23  ()
  24  % (progx evalWithContext: nil)
  25  NuUndefinedSymbol: undefined symbol: x
  26  % (progx evalWithContext: (context))
  27  23
  28  ()
  29  % (global x 50)
  30  50
  31  % (progx evalWithContext: (context))
  32  23
  33  ()
  34  % (progx evalWithContext: nil)
  35  50
  36  ()
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS