<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="">Comments inline:<div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Mar 13, 2017, at 6:38 AM, Dimitri Racordon via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" class="">
<div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" 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></div></blockquote><div><br class=""></div><div>This is a compiler limitation that isn’t going to go away. Structs are value types whose size is equal to the sum of their property sizes. Here, we have a circular dependency, so it is impossible to compute the size.</div><div><br class=""></div><div>This is not an arbitrary restriction. Consider:</div><div><br class=""></div><div><div style="margin: 0px 0px 0px 33.7px; text-indent: -33.8px; line-height: normal; font-family: Menlo;" class=""><span style="color: #c900a4" class="">struct</span> List<span style="color: #c800a4" class=""><</span>T<span style="color: #c800a4" class="">></span> {</div><div style="margin: 0px 0px 0px 67.4px; text-indent: -67.5px; line-height: normal; font-family: Menlo;" class=""> <span style="color: #c900a4" class="">var</span> head: T</div><div style="margin: 0px 0px 0px 67.4px; text-indent: -67.5px; line-height: normal; font-family: Menlo;" class=""> <span style="color: #c900a4" class="">var</span> tail: List<span style="color: #c800a4" class=""><</span>T<span style="color: #c800a4" class="">></span>?</div><div style="margin: 0px 0px 0px 33.7px; text-indent: -33.8px; line-height: normal; font-family: Menlo;" class="">}</div></div><div><br class=""></div><div>The size of `List` is infinite! Every `Optional<T>` must be big enough to store the type `T` in case the value is not `nil`. Thus, `List` must be big enough to store a `T` AND another list (of the same size):</div><div><br class=""></div><div>size(of: List<T>) = size(of: T) + size(of: List<T>)</div><div><br class=""></div><div>It’s easy to see that, assuming `size(of: T)` is non-zero, it is impossible to satisfy this constraint. Yes, Swift could special case the case where there are no other members, but this would be entirely useless (you can’t store state) and would considerably muck up the semantics.</div><div><br class=""></div><div>Note that enums allow members to be marked `indirect`. This would be a reasonable feature for Swift to eventually support for structs as well. In this case, the indirect member would be the size of a pointer, so the size would be statically known and satisfied.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="margin: 0px; line-height: normal;" 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></div></blockquote><div><br class=""></div><div>This is an identical problem to #1. The error message could probably be improved. This isn’t a “reference” in the sense of a pointer, but in the sense that there is literally a dependency. </div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="margin: 0px; line-height: normal;" 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></div></div></blockquote><div><br class=""></div><div>This is an identical problem to #1. An option does *not* introduce any indirection. Swift will however allow the following:</div><div><br class=""></div><div><div style="margin: 0px 0px 0px 33.7px; text-indent: -33.8px; line-height: normal; font-family: Menlo;" class=""><span style="color: #c900a4" class="">struct</span> First {</div><div style="margin: 0px 0px 0px 67.4px; text-indent: -67.5px; line-height: normal; font-family: Menlo;" class=""> <span style="color: #c900a4" class="">let</span> item: [First]</div><div style="margin: 0px 0px 0px 33.7px; text-indent: -33.8px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px 0px 0px 33.7px; text-indent: -33.8px; font-size: 14px; line-height: normal; font-family: Menlo; min-height: 16px;" class=""><br class=""></div></div><div>This is because `Array<T>`—if you look at its declaration—does not *directly* store any type `T`. Instead, it stores a pointer to a buffer holding `T`s, allocated at runtime. These semantics may be a big confusing at first, but this is the tradeoff for the better performance value types can sometimes bring.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="margin: 0px; line-height: normal;" class=""><div 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></div></div></blockquote><div><br class=""></div><div>This is not a bug IMO. It is not the compiler’s job to prevent you from compiling code that includes a type that cannot be instantiated.</div><div><br class=""></div><div>Unlike the previous cases, the compiler actually *can* generate code for these types. You _could_ even unsafely instantiate these types. In the previous cases, the compiler could not compute the size of the types.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="margin: 0px; line-height: normal;" class=""><div 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></div></div></blockquote><div><br class=""></div><div>Ditto #4.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="margin: 0px; line-height: normal;" class=""><div class=""><span style="font-variant-ligatures: no-common-ligatures" 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></div></div></blockquote><div><br class=""></div><div>Ditto #4.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="margin: 0px; line-height: normal;" class=""><div 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></div></div></blockquote><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="margin: 0px; line-height: normal;" class="">
<div class=""><br class="">
</div>
</div>
</div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></div><div class=""><br class=""></div><div class="">I hope I was able to clear up your confusion. Let me know if you have any other questions.</div><div class=""><br class=""></div><div class="">Cheers,</div><div class="">Jaden Geller</div><div class=""><br class=""></div></body></html>