Swift Programming Language

31 thoughts
last posted Aug. 6, 2014, 9:41 p.m.

30 earlier thoughts

0

I just spent a couple of hours debugging my port of Minilight from Python (itself a port) to Swift.

The bug turned out to be that in the Python version I could test for non-None and non-zero at same time; in Swift I need to test separately.

So my Python was:

distance = triangle.get_intersection(...)

if distance and (distance < nearest_distance):
    ...

Now distance can be None as well as 0.0 and the above code handles both.

My original Swift port, however, neglected the 0.0 case and only handled the nil case in the clever type-safe way that if let supports:

if let distance = triangle.getIntersection(...) {
    if distance < nearestDistance {
        ...

The fix was to make it:

if let distance = triangle.getIntersection(...) {
    if distance > 0 && distance < nearestDistance {
        ...

I wonder if that experience is actually an argument for if clauses only taking explicit booleans (contra what I posted here earlier).