<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Nov 1, 2017, at 21:13, Chris Lattner 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=""><div class=""><blockquote type="cite" class=""><div class="">On Nov 1, 2017, at 3:20 AM, Richard Wei <<a href="mailto:rxrwei@gmail.com" class="">rxrwei@gmail.com</a>> wrote:</div><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class=""><blockquote type="cite" class=""><div class=""><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;"><div class=""><div class=""><br class=""></div><div class="">Since you bring it up, Python exceptions will be annoying - As with other languages, Python can throw from an arbitrary expression. Modeling everything as throws in Swift would be super-annoying and unergonomic for the programmer, because we'd require 'try' everywhere. Thoughts on what to do about that are welcome!</div></div></div></blockquote><div class=""><br class=""></div><div class="">Requiring ‘try’ on every statement is annoying, but not having the ability to catch python exceptions is annoying too. We could probably make python exception handling an opt-in feature. For example:</div><div class=""><br class=""></div><div class=""><font face="Menlo" class="">try Python.do {</font></div><div class=""><font face="Menlo" class=""> let a = np.array([1, 2, 3])</font></div><div class=""><font face="Menlo" class=""> let b = np.array([[2], [4]])</font></div><div class=""><font face="Menlo" class=""> print(a.dot(b)) // matrix mul with incompatible shapes</font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><font face="Menlo" class="">catch let error as PythonException {</font></div><div class=""><font face="Menlo" class=""> // Handle PythonError.valueError(“objects are not aligned”)</font></div><div class=""><font face="Menlo" class="">}</font></div></div></div></blockquote><div class=""><br class=""></div><div class="">To correct my example: </div><div class=""><br class=""></div><div class=""><div class=""><font face="Menlo" class="">do { </font></div><div class=""><font face="Menlo" class=""> try Python.do {</font></div><div class=""><font face="Menlo" class=""> let a = np.array([1, 2, 3])</font></div><div class=""><font face="Menlo" class=""> let b = np.array([[2], [4]])</font></div><div class=""><font face="Menlo" class=""> print(a.dot(b)) // matrix mul with incompatible shapes</font></div><div class=""><font face="Menlo" class=""> }</font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><font face="Menlo" class="">catch let error as PythonException {</font></div><div class=""><font face="Menlo" class=""> // Handle PythonError.valueError(“objects are not aligned”)</font></div><div class=""><font face="Menlo" class="">}</font></div></div><div class=""><br class=""></div><div class="">Maybe ‘Python.do {}’ should be called something like ‘Python.safely {}’.</div></div></div></div></blockquote><br class=""></div><div class="">That’s a super interesting way to model this. I’ll need to ponder on it more, but it is certainly a nice ergonomic solution.</div><div class=""><br class=""></div><div class="">Question though: how does it work? Say the first np.array call threw a python exception:</div><div class=""><br class=""></div><div class=""><blockquote type="cite" class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space;"><div class=""><div class=""><div class=""><font face="Menlo" class="">try Python.do {</font></div><div class=""><font face="Menlo" class=""> let a = np.array([1, 2, 3])</font></div><div class=""><font face="Menlo" class=""> let b = np.array([[2], [4]])</font></div><div class=""><font face="Menlo" class=""> print(a.dot(b)) // matrix mul with incompatible shapes</font></div><div class=""><font face="Menlo" class=""> }</font></div></div></div></div></blockquote></div><div class=""><br class=""></div><div class="">We can definitely make the python glue code notice it, catch it and squirrel it away somewhere, but without compiler hacks we couldn’t make it jump out of the closure. This means that np.array would have to return something, and the calls below it would still execute, or am I missing something?</div></div></div></blockquote><div><br class=""></div><div>We make PythonObjects internally nullable (only in the exception-caught state). The second np.array would just return a null PythonObject. </div><div><br class=""></div><div>To be specific, we define three states in the python overlay:</div><div>- Normal state: PythonObjects are guaranteed to be non-null. Any exception traps.</div><div>- Exception-catching state: PythonObjects are still guaranteed to be non-null. Any exception triggers the exception-caught state.</div><div>- Exception-caught state: PythonObjects are nullable — all python expressions return a null PythonObject.</div><div><br class=""></div><div>The exception-catching state is entered during the execution of Python.do’s body.</div><div><br class=""></div><div>-Richard</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 class=""><br class=""></div><div class="">-Chris</div><div class=""><br class=""></div><br class=""></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=""></body></html>