PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/www/tags/NOV_07_2009/htdocs/42docs/users-guide/dynamic-typing.html

#
HTML | 34 lines | 32 code | 2 blank | 0 comment | 0 complexity | a0be54ee9fd8e675ba68319d52ef3839 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. <html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>BeanShell Dynamic Typing</title><meta name="generator" content="DocBook XSL Stylesheets V1.65.1"><link rel="home" href="index.html" title="jEdit 4.2 User's Guide"><link rel="up" href="macro-basics.html" title="Chapter 13. Macro Basics"><link rel="previous" href="helpful-methods.html" title="Helpful Methods in the
  2. Macros Class"><link rel="next" href="something-useful.html" title="Now For Something Useful"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">BeanShell Dynamic Typing</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="helpful-methods.html">Prev</a> </td><th width="60%" align="center">Chapter 13. Macro Basics</th><td width="20%" align="right"> <a accesskey="n" href="something-useful.html">Next</a></td></tr></table><hr></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="dynamic-typing"></a>BeanShell Dynamic Typing</h2></div></div><div></div></div><p>
  3. Without an explicit <i class="glossterm">type declaration</i> like
  4. <tt class="classname">String</tt> <tt class="varname">result</tt>, BeanShell
  5. variables can change their type at runtime depending on the object or
  6. data assigned to it. This dynamic typing allows you to write code like
  7. this (if you really wanted to):
  8. </p><div class="informalexample"><table border="0" bgcolor="#E0E0E0"><tr><td><pre class="programlisting">// note: no type declaration
  9. result = Macros.input(view, &#8220;<span class="quote">Type something here.</span>&#8221;);
  10. // this is our predefined, current View
  11. result = view;
  12. // this is an &#8220;<span class="quote">int</span>&#8221; (for integer);
  13. // in Java and BeanShell, int is one of a small number
  14. // of &#8220;<span class="quote">primitive</span>&#8221; data types which are not classes
  15. result = 14;</pre></td></tr></table></div><p>
  16. However, if you first declared <tt class="varname">result</tt> to be type
  17. <tt class="classname">String</tt> and and then tried these reassignments,
  18. BeanShell would complain. While avoiding explicit type declaration makes
  19. writing macro code simpler, using them can act as a check to make sure you are
  20. not using the wrong variable type of object at a later point in your
  21. script. It also makes it easier (if you are so inclined) to take a
  22. BeanShell &#8220;<span class="quote">prototype</span>&#8221; and incorporate it in a Java program.
  23. </p><p>
  24. One last thing before we bury our first macro. The double slashes in the
  25. examples just above signify that everything following them on that line
  26. should be ignored by BeanShell as a comment. As in Java and C/C++, you
  27. can also embed comments in your BeanShell code by setting them off with
  28. pairs of <b class="userinput"><tt>/* */</tt></b>, as in the following example:
  29. </p><div class="informalexample"><table border="0" bgcolor="#E0E0E0"><tr><td><pre class="programlisting">/* This is a long comment that covers several lines
  30. and will be totally ignored by BeanShell regardless of how
  31. many lines it covers */</pre></td></tr></table></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="helpful-methods.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="macro-basics.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="something-useful.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Helpful Methods in the
  32. Macros Class </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Now For Something Useful</td></tr></table></div></body></html>