emacs.d/clones/lisp/www.cliki.net/change-class dangers.html
2022-10-07 15:47:14 +02:00

72 lines
No EOL
5.9 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>CLiki: change-class dangers</title>
<link rel="alternate" type="application/atom+xml" title="ATOM feed of edits to current article"
href="https://www.cliki.net/site/feed/article.atom?title=change-class%20dangers">
<link rel="stylesheet" href="static/css/style.css">
<link rel="stylesheet" href="static/css/colorize.css">
</head>
<body>
<span class="hidden">CLiki - change-class dangers</span>
<div id="content"><div id="content-area"><div id="article-title">change-class dangers</div><div id="article"><a href="https://www.cliki.net/site/HyperSpec/Body/stagenfun_change-class.html" class="hyperspec">change-class</a> changes the class of the <a href="CLOS.html" class="category">CLOS</a> object involved; in
the specification you'll see a "Notes" section that points
out the problems of changing the class of an object in the presence
of a smart compiler.<p>Experience has shown that sometimes you'll get into problems using
<a href="https://www.cliki.net/site/HyperSpec/Body/fun_slot-value.html" class="hyperspec">slot-value</a> or variables introduced with <a href="https://www.cliki.net/site/HyperSpec/Body/mac_with-slots.html" class="hyperspec">with-slots</a>. It may
appear the the EQ-ness of objects in not preserved because the
compiler was "smart enough" to optimize these features, so for instance: if it's possible
to access the same object (EQ) with another symbol, the compiler can
return different slot-value's for the original or the other symbol. Even
if they are EQ! This can be very confusing.<p>For instance:<p><div class="code"><span class="nonparen"><span class="paren1">(<span class="nonparen"><a href="https://www.cliki.net/site/HyperSpec/Body/mac_defparametercm_defvar.html" class="symbol"><i><span class="symbol">defvar</span></i></a> <span class="special">*A*</span> <span class="paren2">(<span class="nonparen"><a href="https://www.cliki.net/site/HyperSpec/Body/stagenfun_make-instance.html" class="symbol">make-instance</a> 'bar</span>)</span></span>)</span>
<span class="paren1">(<span class="nonparen"><a href="https://www.cliki.net/site/HyperSpec/Body/mac_defmethod.html" class="symbol"><i><span class="symbol">defmethod</span></i></a> foo <span class="paren2">(<span class="nonparen"><span class="paren3">(<span class="nonparen">x bar</span>)</span></span>)</span>
<span class="paren2">(<span class="nonparen"><a href="https://www.cliki.net/site/HyperSpec/Body/stagenfun_change-class.html" class="symbol">change-class</a> x 'baz</span>)</span>
<span class="paren2">(<span class="nonparen"><a href="https://www.cliki.net/site/HyperSpec/Body/fun_format.html" class="symbol">format</a> <a href="https://www.cliki.net/site/HyperSpec/Body/any_t.html" class="symbol">t</a> <span class="string">"~&amp;from x: ~S from *A*: ~S"</span>
<span class="paren3">(<span class="nonparen"><a href="https://www.cliki.net/site/HyperSpec/Body/fun_slot-value.html" class="symbol">slot-value</a> x 'quux</span>)</span>
<span class="paren3">(<span class="nonparen"><a href="https://www.cliki.net/site/HyperSpec/Body/fun_slot-value.html" class="symbol">slot-value</a> <span class="special">*A*</span> 'quux</span>)</span></span>)</span></span>)</span>
<span class="paren1">(<span class="nonparen">foo <span class="special">*A*</span></span>)</span></span></div><p>Can produce output like: "from x: 1 from *A*: 2", depending
on the compiler and/or optimization qualities.<p>To solve this just call a helper function just after
changing the class. The compiler will not optimize over function calls
so the slot-value and with-slots features work again.<p><hr>
<a href="programming&#32;tips.html" class="category">programming tips</a></div></div>
<div id="footer" class="buttonbar"><ul><li><a href="change-class&#32;dangers.html">Current version</a></li>
<li><a href="https://www.cliki.net/site/history?article=change-class%20dangers">History</a></li>
<li><a href="https://www.cliki.net/site/backlinks?article=change-class%20dangers">Backlinks</a></li><li><a href="https://www.cliki.net/site/edit-article?title=change-class%20dangers&amp;from-revision=3839896253">Edit</a></li><li><a href="https://www.cliki.net/site/edit-article?create=t">Create</a></li></ul></div>
</div>
<div id="header-buttons" class="buttonbar">
<ul>
<li><a href="https://www.cliki.net/">Home</a></li>
<li><a href="https://www.cliki.net/site/recent-changes">Recent Changes</a></li>
<li><a href="CLiki.html">About</a></li>
<li><a href="Text&#32;Formatting.html">Text Formatting</a></li>
<li><a href="https://www.cliki.net/site/tools">Tools</a></li>
</ul>
<div id="search">
<form action="https://www.cliki.net/site/search">
<label for="search_query" class="hidden">Search CLiki</label>
<input type="text" name="query" id="search_query" value="" />
<input type="submit" value="search" />
</form>
</div>
</div>
<div id="pageheader">
<div id="header">
<span id="logo">CLiki</span>
<span id="slogan">the common lisp wiki</span>
<div id="login"><form method="post" action="https://www.cliki.net/site/login">
<label for="login_name" class="hidden">Account name</label>
<input type="text" name="name" id="login_name" class="login_input" />
<label for= "login_password" class="hidden">Password</label>
<input type="password" name="password" id="login_password" class="login_input" />
<input type="submit" name="login" value="login" id="login_submit" /><br />
<div id="register"><a href="https://www.cliki.net/site/register">register</a></div>
<input type="submit" name="reset-pw" value="reset password" id="reset_pw" />
</form>
</div>
</div>
</div>
</body></html>