<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Scala-fr &#187; symbol</title>
	<atom:link href="http://www.scala-fr.org/tags/symbol/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.scala-fr.org</link>
	<description>communauté francophone autour du langage Scala</description>
	<lastBuildDate>Sat, 10 Oct 2009 08:01:07 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Interêt du Type Symbol en Scala</title>
		<link>http://www.scala-fr.org/interet-du-type-symbol-en-scala/</link>
		<comments>http://www.scala-fr.org/interet-du-type-symbol-en-scala/#comments</comments>
		<pubDate>Fri, 02 Oct 2009 21:25:44 +0000</pubDate>
		<dc:creator>Maxime Biais</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[jvm]]></category>
		<category><![CDATA[symbol]]></category>

		<guid isPermaLink="false">http://www.scala-fr.org/?p=160</guid>
		<description><![CDATA[Présentation du type Symbol
La classe Symbol est une classe de la bibliothèque standard du langage Scala. Il permet de façon simple de créer un objet unique pour une chaîne de caractères donnée. La comparaison de 2 symbols est une simple comparaison de références. On dit que l&#8217;objet est interné.
Pour simplifier son usage, le compilateur Scala [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.scala-fr.org%2Finteret-du-type-symbol-en-scala%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.scala-fr.org%2Finteret-du-type-symbol-en-scala%2F" height="61" width="51" /></a></div><h1>Présentation du type <code>Symbol</code></h1>
<p>La classe <code><a href="http://www.scala-lang.org/docu/files/api/scala/Symbol.html">Symbol</a></code> est une classe de la bibliothèque standard du langage Scala. Il permet de façon simple de créer un objet unique pour une chaîne de caractères donnée. La comparaison de 2 <code>symbols</code> est une simple comparaison de références. On dit que l&#8217;objet est interné.</p>
<p>Pour simplifier son usage, le compilateur Scala reconnait l&#8217;instanciation d&#8217;un Symbol pour tous les identifiants préfixés par un simple quote <code>'</code>.</p>
<p>Exemple d&#8217;utilisation :</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">Symbol<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;test&quot;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">==</span> <span style="color: #6666FF;">'test</span></pre></div></div>

<h1>Interêt</h1>
<p>Les symbols sont des éléments indispensable pour un langage tel Lisp qui les utilise pour représenter toutes sortes d&#8217;objet : classe, méthodes, variables, etc&#8230; Lisp permet de mélanger code et donnée de façon totalement transparente et des manipuler les uns ou les autres de la même manière. En Lisp, Les symbols sont aussi utilisés par exemple pour imiter des énumérations.</p>
<p>En Scala il est impossible d&#8217;utiliser les symbols pour <em>identifier</em> une méthode par exemple (comme ce que l&#8217;on ferait en Lisp). Les symbols perdent donc pas mal de leur interêt&#8230;</p>
<p>On pourrait se dire qu&#8217;en utilisant des symbols comme identifiants, on peut gagner de la place par rapport à des chaînes de caractères, mais alors pourquoi ne pas utiliser directement un <code>enum</code>, ou dans des cas plus évolué une <code>case class</code> ? C&#8217;est peut être intéressant pour gagner du temps, cela évite de déclarer un <code>enum</code>. Ou bien une utilisation dans une table de hashage comme identifiants à la place de <code>string</code>, mais là encore même chose, les <code>strings</code> litérales (cad définies via le biais de double quote, <code>"ma chaine"</code>) sont internées (voir suite de l&#8217;article) et donc cela ne prend pas plus de mémoire et les comparaisons sont aussi rapides.</p>
<p>Et surtout si vous trouvez un avantage majeur à utiliser les Symbols, postez un commentaire, je suis curieux de trouver un vrai cas d&#8217;utilisation.</p>
<h1>Exemple décompilé</h1>
<p>La suite de l&#8217;article s&#8217;adresse à ceux qui cherchent à voir en détails ce qui se passe dans la machine virtuelle.  Le but est de comprendre comment est représenté une <em>string litteral</em> comment le compilateur les interne.</p>
<p>Source d&#8217;un exemple tout simple en scala</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">class</span> SymbolTest <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> run <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
        <span style="color: #6666FF;">&quot;test&quot;</span> <span style="color: #000080;">==</span> <span style="color: #6666FF;">&quot;test&quot;</span>
    <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>Décompilation avec la commande <code>javap</code></p>
<pre>$ javap -c -verbose scalafr.StringTest</pre>
<p>Cette commande produit la sortie suivante (j&#8217;ai supprimé les lignes qui ne sont pas pertinentes à la démonstration) :</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;"><span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #339933;">...</span><span style="color: #009900; font-weight: bold;">&#93;</span>
<span style="color: #339933;">//</span> Pool de constantes
<span style="color: #000000; font-weight: bold;">const</span> #<span style="color: #0000ff;">7</span> = Asciz	java<span style="color: #339933;">/</span>lang<span style="color: #339933;">/</span>Object<span style="color: #666666; font-style: italic;">;</span>
<span style="color: #000000; font-weight: bold;">const</span> #<span style="color: #0000ff;">8</span> = class	#<span style="color: #0000ff;">7</span><span style="color: #666666; font-style: italic;">;	//  java/lang/Object</span>
<span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #339933;">...</span><span style="color: #009900; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">const</span> #<span style="color: #0000ff;">13</span> = Asciz	<span style="color: #00007f; font-weight: bold;">test</span><span style="color: #666666; font-style: italic;">;</span>
<span style="color: #000000; font-weight: bold;">const</span> #<span style="color: #0000ff;">14</span> = String	#<span style="color: #0000ff;">13</span><span style="color: #666666; font-style: italic;">;	//  test</span>
<span style="color: #000000; font-weight: bold;">const</span> #<span style="color: #0000ff;">15</span> = Asciz	equals<span style="color: #666666; font-style: italic;">;</span>
<span style="color: #000000; font-weight: bold;">const</span> #<span style="color: #0000ff;">16</span> = Asciz	<span style="color: #009900; font-weight: bold;">&#40;</span>Ljava<span style="color: #339933;">/</span>lang<span style="color: #339933;">/</span>Object<span style="color: #666666; font-style: italic;">;)Z;</span>
<span style="color: #000000; font-weight: bold;">const</span> #<span style="color: #0000ff;">17</span> = NameAndType	#<span style="color: #0000ff;">15</span><span style="color: #339933;">:</span>#<span style="color: #0000ff;">16</span><span style="color: #666666; font-style: italic;">;//  equals:(Ljava/lang/Object;)Z</span>
<span style="color: #000000; font-weight: bold;">const</span> #<span style="color: #0000ff;">18</span> = Method	#<span style="color: #0000ff;">8.</span>#<span style="color: #0000ff;">17</span><span style="color: #666666; font-style: italic;">;	//  java/lang/Object.equals:(Ljava/lang/Object;)Z</span>
<span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #339933;">...</span><span style="color: #009900; font-weight: bold;">&#93;</span>
<span style="color: #009900; font-weight: bold;">&#123;</span>
<span style="color: #000000; font-weight: bold;">public</span> scalafr<span style="color: #339933;">.</span>StringTest<span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #009900; font-weight: bold;">&#41;</span><span style="color: #666666; font-style: italic;">;</span>
<span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #339933;">...</span><span style="color: #009900; font-weight: bold;">&#93;</span>
<span style="color: #339933;">//</span> <span style="color: #000000; font-weight: bold;">Code</span> de la méthode run<span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #009900; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> boolean run<span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #009900; font-weight: bold;">&#41;</span><span style="color: #666666; font-style: italic;">;</span>
  <span style="color: #000000; font-weight: bold;">Code</span><span style="color: #339933;">:</span>
   <span style="color: #0000ff;">0</span><span style="color: #339933;">:</span>	ldc	#<span style="color: #0000ff;">14</span><span style="color: #666666; font-style: italic;">; //String test</span>
   <span style="color: #0000ff;">2</span><span style="color: #339933;">:</span>	ldc	#<span style="color: #0000ff;">14</span><span style="color: #666666; font-style: italic;">; //String test</span>
   <span style="color: #0000ff;">4</span><span style="color: #339933;">:</span>	astore_1
   <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #339933;">...</span><span style="color: #009900; font-weight: bold;">&#93;</span>
   <span style="color: #0000ff;">17</span><span style="color: #339933;">:</span>	aload_1
   <span style="color: #0000ff;">18</span><span style="color: #339933;">:</span>	invokevirtual	#<span style="color: #0000ff;">18</span><span style="color: #666666; font-style: italic;">; //Method java/lang/Object.equals:(Ljava/lang/Object;)Z</span>
   <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #339933;">...</span><span style="color: #009900; font-weight: bold;">&#93;</span></pre></div></div>

<p>La commande <a href="http://java.sun.com/docs/books/jvms/second_edition/html/Instructions2.doc8.html#ldc">ldc</a> prend une référence du <a href="http://java.sun.com/docs/books/jvms/second_edition/html/ConstantPool.doc.html#73272">pool de constantes</a> et la pose sur la pile d&#8217;opérande. Le pool de constantes est représenté au dessus du code, <code>ldc #14</code>va chercher <code>const #14</code> et la pose sur la pile. La ligne 18 invoque la méthode réprésenté par #18 (dans ce cas, java.lang.Object.equals()) en utilisant les 2 derniers éléments posés sur la pile (les <code>ldc</code> des lignes 19 et 20. On remarque que ces deux <code>ldc</code> charge la même réference de constante : #14 qui représente la chaîne de caractère &laquo;&nbsp;test&nbsp;&raquo;, c&#8217;est ça l&#8217;internement de string litérales.</p>
<p>Pour aller plus loin :</p>
<ul>
<li><a href="http://chable.developpez.com/tutoriel/java/binaire/">Comprendre le binaire Java</a></li>
<li><a href="http://java.sun.com/docs/books/jvms/second_edition/html/VMSpecTOC.doc.html">The Java Virtual Machine Specification</a></li>
</ul>
     ]]></content:encoded>
			<wfw:commentRss>http://www.scala-fr.org/interet-du-type-symbol-en-scala/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
