PageRenderTime 267ms CodeModel.GetById 59ms app.highlight 2ms RepoModel.GetById 205ms 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
 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
 9result = Macros.input(view, &#8220;<span class="quote">Type something here.</span>&#8221;);
10
11// this is our predefined, current View
12result = view;
13
14// this is an &#8220;<span class="quote">int</span>&#8221; (for integer);
15// in Java and BeanShell, int is one of a small number
16// of &#8220;<span class="quote">primitive</span>&#8221; data types which are not classes
17result = 14;</pre></td></tr></table></div><p>
18    However, if you first declared <tt class="varname">result</tt> to be type
19    <tt class="classname">String</tt> and and then tried these reassignments,
20    BeanShell would complain. While avoiding explicit type declaration makes
21    writing macro code simpler, using them can act as a check to make sure you are
22    not using the wrong variable type of object at a later point in your
23    script. It also makes it easier (if you are so inclined) to take a
24    BeanShell &#8220;<span class="quote">prototype</span>&#8221; and incorporate it in a Java program.
25  </p><p>
26    One last thing before we bury our first macro. The double slashes in the
27    examples just above signify that everything following them on that line
28    should be ignored by BeanShell as a comment. As in Java and C/C++, you
29    can also embed comments in your BeanShell code by setting them off with
30    pairs of <b class="userinput"><tt>/* */</tt></b>, as in the following example:
31  </p><div class="informalexample"><table border="0" bgcolor="#E0E0E0"><tr><td><pre class="programlisting">/* This is a long comment that covers several lines
32and will be totally ignored by BeanShell regardless of how
33many 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
34  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>