<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">
Oh yes you’re right.
<div class="">I didn’t consider that case; thanks.<br class="">
<br class="">
<div>
<blockquote type="cite" class="">
<div class="">On 13 Mar 2017, at 15:34, rintaro ishizaki <<a href="mailto:fs.output@gmail.com" class="">fs.output@gmail.com</a>> wrote:</div>
<br class="Apple-interchange-newline">
<div class="">
<div dir="ltr" class="">FWIW, there *is* a way to instantiate #4 case.
<div class=""><br class="">
<font face="monospace, monospace" class="">class First {<br class="">
let item: First<br class="">
init(item: First) {<br class="">
self.item = item<br class="">
}<br class="">
}<br class="">
<br class="">
class Second : First {<br class="">
init() {<br class="">
super.init(item: self)<br class="">
}<br class="">
}<br class="">
</font><br class="">
<br class="">
let a: First = Second()<br class="">
<div class="gmail_extra"><br class="">
<div class="gmail_quote">2017-03-13 22:38 GMT+09:00 Dimitri Racordon via swift-evolution
<span dir="ltr" class=""><<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>></span>:<br class="">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div style="word-wrap:break-word" class="">Hello swift-evolution,
<div class=""><br class="">
</div>
<div class="">I noticed there’s some inconsistencies with recursive types and how the compiler handles them. Consider the following cases:</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<div class=""><b class="">1. <span style="font-family:Menlo;font-size:11px;font-variant-ligatures:no-common-ligatures" class=""><span style="font-family:Helvetica;font-size:12px" class="">Swift is right to refuse compiling this, since there’s no way to initialise
an instance of `</span>First<span style="font-family:Helvetica;font-size:12px" class=""> `:</span></span></b></div>
<div class=""><br class="">
</div>
<div class="">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">struct</span><span style="font-variant-ligatures:no-common-ligatures" class=""> First {</span></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="font-variant-ligatures:no-common-ligatures" class=""> <span style="color:rgb(186,45,162);font-variant-ligatures:no-common-ligatures" class="">let</span><span style="font-variant-ligatures:no-common-ligatures" class=""> </span>item: </span>First</div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="font-variant-ligatures:no-common-ligatures" class="">}</span></div>
</div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<div style="margin:0px;line-height:normal;color:rgb(0,132,0)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">// Error: Value type 'First' cannot have a stored property that references itself</span></div>
</div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<br class="">
</div>
<div style="margin:0px;line-height:normal" class="">However, the message suggests more a compiler limitation rather than the actual impossibility to initialize the declared type.</div>
<div style="margin:0px;line-height:normal" class=""><br class="">
</div>
<div style="margin:0px;line-height:normal" class=""><br class="">
</div>
<div style="margin:0px;line-height:normal" class=""><b class="">2. Swift is also right not to compile this, but the error messages are even less insightful:</b></div>
<div style="margin:0px;line-height:normal" class=""><br class="">
</div>
<div style="margin:0px;line-height:normal" class="">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">struct</span><span style="font-variant-ligatures:no-common-ligatures" class=""> First {</span></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">let</span><span style="font-variant-ligatures:no-common-ligatures" class=""> item:
</span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="">Second</span></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="font-variant-ligatures:no-common-ligatures" class="">}</span></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="font-variant-ligatures:no-common-ligatures" class=""><span style="color:rgb(0,132,0)" class="">// Error: Value type 'First' cannot have a stored property that references itself</span></span></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="font-variant-ligatures:no-common-ligatures" class=""><br class="">
</span></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class="">
<span style="font-variant-ligatures:no-common-ligatures" class=""></span><br class="">
</div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">struct</span><span style="font-variant-ligatures:no-common-ligatures" class=""> Second {</span></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">let</span><span style="font-variant-ligatures:no-common-ligatures" class=""> item:
</span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="">First</span></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="font-variant-ligatures:no-common-ligatures" class="">}</span></div>
<div style="margin:0px;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><font color="#008400" face="Menlo" class=""><span style="font-size:11px" class="">// Error: Value type </span></font><span style="color:rgb(0,132,0);font-family:Menlo;font-size:11px" class="">'</span><font color="#008400" face="Menlo" class=""><span style="font-size:11px" class="">Second'
cannot have a stored property that references itself</span></font></span></div>
<div class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><br class="">
</span></div>
</div>
<div style="margin:0px;line-height:normal" class="">The problem isn’t that the value types reference
<i class="">themselves</i>. Instead, the problem is that there’s a cyclic dependency between `First` and `Second` that makes it impossible for neither of these structures to be instantiated.</div>
<div style="margin:0px;line-height:normal" class=""><br class="">
</div>
<div style="margin:0px;line-height:normal" class=""><br class="">
</div>
<div style="margin:0px;line-height:normal" class=""><b class="">3. Swift should let me do that:</b></div>
<div style="margin:0px;line-height:normal" class=""><br class="">
</div>
<div style="margin:0px;line-height:normal" class="">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">struct</span><span style="font-variant-ligatures:no-common-ligatures" class=""> First {</span></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">let</span><span style="font-variant-ligatures:no-common-ligatures" class=""> item:
</span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="">First</span><span style="font-variant-ligatures:no-common-ligatures" class="">?</span></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="font-variant-ligatures:no-common-ligatures" class="">}</span></div>
<div class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><br class="">
</span></div>
<div class="">The compiler prevents me to declare the above struct, even if there actually is a way to initialise an instance of `First` (e.g. `First(item: nil)`). The message is identical to that of case #1 (maybe it actually
<i class="">is</i> a compiler limitation after all?)</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<div class=""><b class="">4. Swift shouldn’t compile this:</b></div>
<div class=""><br class="">
</div>
<div class="">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">class</span><span style="font-variant-ligatures:no-common-ligatures" class=""> First {</span></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">let</span><span style="font-variant-ligatures:no-common-ligatures" class=""> item:
</span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="">First</span></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">init</span><span style="font-variant-ligatures:no-common-ligatures" class="">(item:
</span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="">First</span><span style="font-variant-ligatures:no-common-ligatures" class="">) {</span></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="font-variant-ligatures:no-common-ligatures" class=""> </span>
<span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">self</span><span style="font-variant-ligatures:no-common-ligatures" class="">.</span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="">item</span><span style="font-variant-ligatures:no-common-ligatures" class="">
= item</span></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="font-variant-ligatures:no-common-ligatures" class=""> }</span></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="font-variant-ligatures:no-common-ligatures" class="">}</span></div>
</div>
<div class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><br class="">
</span></div>
<div class="">Like in case #1, there’s no way to instantiate `First`. The fact that it’s a reference rather than a value doesn’t change the problem.</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<div class=""><b class="">5. Similarly to case #4, Swift shouldn’t compile this:</b></div>
<div class=""><br class="">
</div>
<div class="">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(186,45,162)" class="">
<span style="font-variant-ligatures:no-common-ligatures" class="">indirect</span><span style="font-variant-ligatures: no-common-ligatures;" class="">
</span><span style="font-variant-ligatures:no-common-ligatures" class="">enum</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> First {</span></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">case</span><span style="font-variant-ligatures:no-common-ligatures" class=""> item(</span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="">First</span><span style="font-variant-ligatures:no-common-ligatures" class="">)</span></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="font-variant-ligatures:no-common-ligatures" class="">}</span></div>
</div>
<div class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><br class="">
</span></div>
<div class=""><br class="">
</div>
<div class=""><b class="">6. Cases #4 #5 could be written like case #2, and should also raise compilation errors.</b></div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<div class="">Does someone know if these issues have already been discussed and/or addressed?</div>
<div class=""><br class="">
</div>
<div class="">Thanks,</div>
<div class="">Dimitri Racordon</div>
<div class=""><br class="">
</div>
</div>
</div>
<br class="">
______________________________<wbr class="">_________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/<wbr class="">mailman/listinfo/swift-<wbr class="">evolution</a><br class="">
<br class="">
</blockquote>
</div>
<br class="">
</div>
</div>
</div>
</div>
</blockquote>
</div>
<br class="">
</div>
</body>
</html>