<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Masayuki Uchida on Medium]]></title>
        <description><![CDATA[Stories by Masayuki Uchida on Medium]]></description>
        <link>https://medium.com/@antilogic?source=rss-6dd3d276b45e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*lcavSNR-xiojJPxh.jpg</url>
            <title>Stories by Masayuki Uchida on Medium</title>
            <link>https://medium.com/@antilogic?source=rss-6dd3d276b45e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 20:36:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@antilogic/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[About shallow copy,deep copy of Crystal lang]]></title>
            <link>https://medium.com/@antilogic/test-37c2b2a6862d?source=rss-6dd3d276b45e------2</link>
            <guid isPermaLink="false">https://medium.com/p/37c2b2a6862d</guid>
            <dc:creator><![CDATA[Masayuki Uchida]]></dc:creator>
            <pubDate>Fri, 11 Dec 2015 05:41:02 GMT</pubDate>
            <atom:updated>2015-12-11T15:01:09.664Z</atom:updated>
            <content:encoded><![CDATA[<p>About shallow copy,deep copy of Crystal lang</p><p>slack上での<a href="http://crystal.pine.moe/">crystal-jaコミュニティ</a>で、いろんな議論（大体私からは不明点を聞くだけ）してるのですが、 ちょっとcyrstalで気になってたのが、シャローコピー、ディープコピーはどうするのか、というもの。</p><p>例えば、以下のコードはrubyだと</p><pre><strong>class</strong> <strong>ClassA</strong><br>  <strong>attr_accessor</strong> :a<br><strong>end</strong></pre><pre>clsa <strong>=</strong> ClassA<strong>.</strong>new<br>clsa<strong>.</strong>a <strong>=</strong> 1<br>clsb <strong>=</strong> clsa<strong>.</strong>clone<br>clsb<strong>.</strong>a <strong>=</strong> 2<br>p clsb<strong>.</strong>a <em>#=&gt; 1</em><br>p clsa<strong>.</strong>a <em>#=&gt; 2</em></pre><p>となります。 では、crystalではどうなるかというと</p><pre>(同上)<br>p clsb<strong>.</strong>a <em>#=&gt; 2</em><br>p clsa<strong>.</strong>a <em>#=&gt; 2</em></pre><p>となってしまいます。（attr_accessorはpropertyで読み替え）</p><p>どうもcyrstalでのやり方では、</p><p><a href="https://github.com/manastech/crystal/blob/b3b1223108806f31fdb29cca6110a5d11d82f504/src/object.cr#L173-L179">https://github.com/manastech/crystal/blob/b3b1223108806f31fdb29cca6110a5d11d82f504/src/object.cr#L173-L179</a></p><p>や</p><p><a href="https://github.com/manastech/crystal/blob/0f4e5866db1d559fdb3c833e8de8a1f96ab7c957/src/array.cr#L604-L622">https://github.com/manastech/crystal/blob/0f4e5866db1d559fdb3c833e8de8a1f96ab7c957/src/array.cr#L604-L622</a></p><p>を見る限りだと、「子クラスで override して使う前提」みたいです。 (<a href="http://qiita.com/pine613">@pine613</a>さんの見立てでは) なので、ディープコピーの各クラスの実装例だと、以下のような感じになります。(同<a href="http://qiita.com/pine613">@pine613</a>さんのコード)</p><pre>class ClassA<br>  property :a</pre><pre>  def self.new <br>    i = self.new <br>    yield i<br>    i <br>  end</pre><pre>  def clone <br>    ClassA.new { |i|<br>      i.a = self.a<br>    }<br>  end<br>end</pre><p>因みにですが、crystalではStructクラスも用意されています。</p><p>classクラスとの違いは</p><p><a href="http://ja.crystal-lang.org/docs/syntax_and_semantics/structs.html">http://ja.crystal-lang.org/docs/syntax_and_semantics/structs.html</a></p><p>を見ると</p><ul><li>構造体に対して new を実行するとヒープではなくスタック領域が割り当てられる</li><li>クラスが参照渡しであるのに対して、構造体は値渡しである</li><li>構造体は暗黙的に Struct を継承し、Struct は Value を継承している。一方クラスは Reference を継承する</li></ul><p>とあります。オブジェクト間で値をやり取りするようなことがあるなら、こっちを使うべき、という設計みたいですね。</p><p><a href="https://github.com/MakeNowJust">@makenowjust</a>さん曰く</p><p>「それと、 record マクロを使って定義した構造体には clone メソッドが定義されてます」<br>とのことで、実際に調べてみると以下のようになっています。</p><p><a href="https://github.com/manastech/crystal/blob/0f4e5866db1d559fdb3c833e8de8a1f96ab7c957/src/macros.cr#L29-L42">https://github.com/manastech/crystal/blob/0f4e5866db1d559fdb3c833e8de8a1f96ab7c957/src/macros.cr#L29-L42</a></p><p>上記情報に従って書き直すと</p><pre>struct ClassA<br>  property :a<br>end<br>​<br>record ClassA<br>​<br>classA = ClassA.new<br>classA.a = 1<br>​<br>classB = classA.clone<br>classB.a = 2</pre><pre>p classA.a #=&gt; 1<br>p classB.a #=&gt; 2</pre><p>となり期待する動作になりました。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=37c2b2a6862d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>