<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="">I think this is a manifestation of a more general problem, that default arguments cannot capture values from outer scope. Saying they’re evaluated in “type context” and not “instance context” is one way to skirt around the issue, but you can still trigger capturing anyway, and crash in SILGen:<div class=""><br class=""></div><div class="">public func foo() {</div><div class=""> let x = 10</div><div class=""><br class=""></div><div class=""> func bar(y: Int = x) {}</div><div class="">}</div><div class=""><br class=""></div><div class="">Fixing this would not be too difficult, just refactoring some code in Sema so that we actually compute the captures of each default argument generator, and then emit the captures when we call one.</div><div class=""><br class=""></div><div class="">Slava</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Feb 22, 2017, at 10:16 AM, Nate Cook 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=us-ascii" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hello all,<div class=""><br class=""></div><div class="">I was surprised to find that I can't use an instance member as the default value of a method parameter, only constants and the run-time calculated #file, #line, etc. Is it possible to remove this limitation?</div><div class=""><br class=""></div><div class="">I'd like to propose that we add an overload of the collection <font face="Menlo" class="">index(_:offsetBy:)</font> methods that use the collection's <font face="Menlo" class="">startIndex </font>as a default parameter, but we can't provide that without either this feature or an extra overload. If the declaration looks like this:</div><div class=""><br class=""></div><div class=""><font face="Menlo" class="">extension Collection {</font></div><div class=""><font face="Menlo" class=""> func index(_ i: Index = startIndex, offsetBy n: IndexDistance) -> Index {</font></div><div class=""><font face="Menlo" class=""> // ...</font></div><div class=""><font face="Menlo" class=""> }</font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><br class=""></div><div class="">then calling that method with an omitted first parameter would treat it as something like this:</div><div class=""><br class=""></div><div class=""><div class=""><font face="Menlo" class="">extension Collection {</font></div><div class=""><font face="Menlo" class=""> func index(offsetBy </font><span style="font-family: Menlo;" class="">n: IndexDistance</span><font face="Menlo" class="">) -> Index {<span class="Apple-tab-span" style="white-space:pre">        </span></font></div><div class=""><font face="Menlo" class=""> let i = startIndex</font></div><div class=""><font face="Menlo" class=""> // ...</font></div><div class=""><font face="Menlo" class=""> }</font></div><div class=""><font face="Menlo" class="">}</font></div></div><div class=""><br class=""></div><div class="">Is this just syntactic sugar, or am I missing something that makes this harder than it looks? I can see how more complex expressions could be useful there, too, and can't find an obvious reason we couldn't use any code that could be inserted at the top of the method body.</div><div class=""><br class=""></div><div class="">Thanks!</div><div class="">Nate</div><div class=""><br class=""></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></body></html>