<div dir="ltr">Thank you for your thorough answer.<div><br></div><div>So when direct and indirect cases already look the same for the type system then why is there the <font face="monospace, monospace">indirect</font> modifier in the first place?</div><div>Couldn't the compiler just automatically box variables which would cause a recursion? For structs, enums and probably tuples.</div><div><br></div><div>From what I understand in indirected enum cases the compiler will basically just box either</div><div><ul><li>the case value as a whole or</li><li>just the associative values which cause the recursion.</li></ul><div>What is boxed?</div></div><div><br></div><div>I do not intend to propose to make <font face="monospace, monospace">indirect</font> part of the type itself. I'm checking whether the modifier can be moved closer to the declaration of the variables which cause the indirections.</div><div>What I need for my proposal is indeed indirection on structs and for that I'd like to understand how indirections work under the hood and what their limitations are.</div><div>Your answer already gives a lot of insight there.</div><div><br></div><div>Actually I once needed to <a href="https://gist.github.com/fluidsonic/6a8f1c1def2a92416519">design a recursive struct</a> and did so by just wrapping the recursive variable in an indirect enum case. Works fine in Swift 2.1</div><div>I could have done it differently - e.g. by storing an array of parents instead of using recursion - but the approach worked.</div><div><br></div><div>How can a recursive struct using indirection be an infinite type? Since the indirect variables are just boxes which have a fixed size (just a pointer?) independent of the struct's size the type is no longer infinite.</div><div><br></div><div>Thanks,</div><div> Marc</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Dec 11, 2015 at 4:03 PM, Slava Pestov <span dir="ltr"><<a href="mailto:spestov@apple.com" target="_blank">spestov@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">Hi Marc,<div><br><div><span class=""><blockquote type="cite"><div>On Dec 11, 2015, at 3:06 AM, Marc Knaup via swift-dev <<a href="mailto:swift-dev@swift.org" target="_blank">swift-dev@swift.org</a>> wrote:</div><br><div><div dir="ltr">Hey guys,<div><br></div><div>I'm working on a proposal and the question arose why the declaration modifier <font face="monospace, monospace">indirect</font> can only be specified for the whole enum <font face="monospace, monospace">case</font> and the whole <font face="monospace, monospace">enum</font> but not for the actual parameter which is indirect.</div><div><br></div><div>I.e. is there any technical reason which would prevent something like the following?</div><div><br></div><div><ol style="border:0px;font-size:14px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;line-height:1.6em;list-style:none;font-family:Helvetica,Arial,sans-serif;background-repeat:initial initial"><li style="border-width:0px 0px 0px 18px;border-left-style:solid;border-left-color:transparent;margin:0px;outline:0px;padding:0px 10px 0px 0px;vertical-align:baseline;white-space:pre-wrap;list-style-type:none;background:transparent"><code style="border:0px;font-size:0.85em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;font-family:Menlo,monospace;word-wrap:break-word;background:transparent"><span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(170,51,145);background:transparent">enum</span> <span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(63,110,116);background:transparent">ArithmeticExpression</span> {</code></li><li style="border-width:0px 0px 0px 18px;border-left-style:solid;border-left-color:transparent;margin:0px;outline:0px;padding:0px 10px 0px 0px;vertical-align:baseline;white-space:pre-wrap;list-style-type:none;background:transparent"><code style="border:0px;font-size:0.85em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;font-family:Menlo,monospace;word-wrap:break-word;background:transparent"> <span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(170,51,145);background:transparent">case</span> <span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(63,110,116);background:transparent">Number</span>(<span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(63,110,116);background:transparent">Int</span>)</code></li><li style="border-width:0px 0px 0px 18px;border-left-style:solid;border-left-color:transparent;margin:0px;outline:0px;padding:0px 10px 0px 0px;vertical-align:baseline;white-space:pre-wrap;list-style-type:none;background:transparent"><code style="border:0px;font-size:0.85em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;font-family:Menlo,monospace;word-wrap:break-word;background:transparent"> <span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(170,51,145);background:transparent">case</span> <span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(63,110,116);background:transparent">Addition</span>(<span style="color:rgb(170,51,145);font-size:11.9px;line-height:22.4px">indirect </span><span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(63,110,116);background:transparent">ArithmeticExpression</span>, <span style="color:rgb(170,51,145);font-size:11.9px;line-height:22.4px">indirect </span><span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(63,110,116);background:transparent">ArithmeticExpression</span>)</code></li><li style="border-width:0px 0px 0px 18px;border-left-style:solid;border-left-color:transparent;margin:0px;outline:0px;padding:0px 10px 0px 0px;vertical-align:baseline;white-space:pre-wrap;list-style-type:none;background:transparent"><code style="border:0px;font-size:0.85em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;font-family:Menlo,monospace;word-wrap:break-word;background:transparent"> <span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(170,51,145);background:transparent">case</span> <span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(63,110,116);background:transparent">Multiplication</span>(<span style="color:rgb(170,51,145);font-size:11.9px;line-height:22.4px">indirect </span><span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(63,110,116);background:transparent">ArithmeticExpression</span>, <span style="color:rgb(170,51,145);font-size:11.9px;line-height:22.4px">indirect </span><span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(63,110,116);background:transparent">ArithmeticExpression</span>)</code></li><li style="border-width:0px 0px 0px 18px;border-left-style:solid;border-left-color:transparent;margin:0px;outline:0px;padding:0px 10px 0px 0px;vertical-align:baseline;white-space:pre-wrap;list-style-type:none;background:transparent"><code style="border:0px;font-size:0.85em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;font-family:Menlo,monospace;word-wrap:break-word;background:transparent">}</code></li></ol></div></div></div></blockquote><div><br></div></span><div>Right now, notice that direct and indirect cases look the same from the perspective of the type system.</div><div><br></div>With your proposal, the fact that the entire tuple payload can be matched by a pattern implies that ‘indirect’ has to become a formal type in the language, since you will now be able to write down a value of type '(indirect ArithmeticExpression, Int)’, for example. It also raises the issue of how these indirect values are wrapped and unwrapped, since now the ‘indirect ArithmeticExpression’ is itself a value.</div><div><br></div><div>An alternative is to make ‘indirect’ a non-materializable type, like ‘inout’. ‘inout’ can appear inside tuple types, but is not a first class value. This creates lots of special cases for ‘inout’ and you can imagine it will be equally difficult for ‘indirect’.</div><div><br></div><div>If you want an ‘indirect’ that can be nested inside a tuple type, you could just define ‘class Box<T> { let payload: T }’ and tolerate a bit of verbosity when wrapping and unwrapping values. As long as the payload is immutable, you will still have value semantics using this trick.</div><div><span class=""><br><blockquote type="cite"><div><div dir="ltr"><div><div><br></div><div>Also is there any technical reason which would prevent <font face="monospace, monospace">indirect</font> from being used for structs?</div></div></div></div></blockquote><div><br></div></span>Like an ‘indirect’ modifier for stored properties on structs? Yes, this should be possible, however note that right now enum payloads are not themselves lvalues, which means that assignment of indirect enum cases only has to update the reference count of the box to maintain value semantics, since the contents of the box will never change. Presumably for structs you would need to be able to define an ‘indirect var’ with a mutable payload — immutable-only indirect stored properties don’t seem very useful.</div><div><br></div><div>Making the indirect payload mutable will require either copying the box upon assignment, or alternatively, a copy-on-write scheme could be used. The SIL @box type that implements indirect cases isn’t set up to support either of those right now. Also, unlike enums, indirect fields of structs won’t give you any new generality that was not possible before — its just a performance change. A value type that contains a cycle through an ‘indirect’ struct field is still invalid, for example, because the resulting type is still infinite.</div><div><br></div><div>Slava</div><div><br><blockquote type="cite"><div><div dir="ltr"><div><div><br></div>Thanks,</div><div> Marc</div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=NLTid1W7V2mxBEfr5Y3KfTntaxSmOQp5vjACZc9Eh1-2FH-2Fej3R0Z4mS437WqlI1ZHGDmechBex8wNnLcJAHS6eiylKs3WYt15t33yHp9mn7CB7YyRdAiuVdlIEZ5ZSycYp-2FI-2Fye5YAEe-2BJ4JGvHQKwIkodfRQ4j1PO0QRgL5XH9IBuZwtywxGv6RtPHTbFFl-2FWHkI317MHZLEjSP5REx3kUUnTjy4X4Zqh4JcvTGKFv0-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important">
_______________________________________________<br>swift-dev mailing list<br><a href="mailto:swift-dev@swift.org" target="_blank">swift-dev@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-dev" target="_blank">https://lists.swift.org/mailman/listinfo/swift-dev</a><br></div></blockquote></div><br></div></div></blockquote></div><br></div>