News for hackers. Stuff that matters.
Re:Go Home Swift Compiler, You’re Drunk by jopamer on Tuesday April 26, 2016 @08:40PM (#11577003)
(FWIW, I just pushed a fix for this bug.)

EDIT: To be clear, addressing these kinds of issues is something we've been aggressively pursuing for Swift 3, and will continue to do so.

Re:Go Home Swift Compiler, You’re Drunk by kristianp on Tuesday April 26, 2016 @09:17PM (#11577150)
Re:Go Home Swift Compiler, You’re Drunk by nonamegdsa on Wednesday April 27, 2016 @01:56PM (#11582827)
Is this construct commonly used in swift or is it an edge case? I've seen people referring to it as an edge case, but at the same time this is code which I find my self writing pretty often in other languages, for example when initializing data for unit tests.
Re:Go Home Swift Compiler, You’re Drunk by kenferry on Tuesday April 26, 2016 @05:08PM (#11575642)
The problem, by the way, is that the use of nested literals (string and dictionary, here) causes the compiler to do a very large search to resolve the types. A quoted string, e.g. "foo", could be any type that conforms to StringLiteralConvertible. It's evaluating all permutations of potential type assignments to each literal that appears.

I thought they fixed this, though. I filed it for Array<Array<Int>> long ago, and they patched that case. https://twitter.com/kongtomorrow/status/565844856690339841

Re:Go Home Swift Compiler, You’re Drunk by draw_down on Tuesday April 26, 2016 @06:29PM (#11576287)
Jeez. Is there some sort of hint to indicate that a string is just a string? Otherwise that seems like it will always be slow.
Re:Go Home Swift Compiler, You’re Drunk by rsfinn on Tuesday April 26, 2016 @06:50PM (#11576405)
Well, yes; it's called type annotation (as noted in comments both here and at the original article). Telling the compiler it's "just a String" (or more precisely a dictionary containing strings) cuts the compile time to 100ms.

Yes, the type inference in this case could use some improvement -- maybe guess the simplest possible interpretation first, and search the space of alternatives in the background, assuming there's a mechanism to go back for a do-over if any are eventually found...?

Re:Go Home Swift Compiler, You’re Drunk by outworlder on Tuesday April 26, 2016 @07:07PM (#11576501)
The background check would still take 12 hours in this case.
Re:Go Home Swift Compiler, You’re Drunk by goldenkey on Wednesday April 27, 2016 @03:03AM (#11578369)
Unless you solve the [1] Entscheidungsproblem, taking two symbolic expressions (ie. two complex types) and finding if they are equal is unsolved and thought to be equivalent to solving the halting problem. [2] SAT style solvers are the best we have at the moment. Heuristics can be improved, of course. But we are really just shooting for common use cases in a turing complete language. Which really means...in a universe of infinite code and types that can be created - we are choosing to speed up our compiler for certain ones at the dismay of others. Overall though, since Swift is going to be used for making mostly trivial mobile apps, I don't really hold the academic high hat over it, let it take all the assumptions it wants.

[1] https://en.wikipedia.org/wiki/Entscheidungsproblem

[2] https://en.wikipedia.org/wiki/Boolean_satisfiability_problem

Re:Go Home Swift Compiler, You’re Drunk by coldcode on Tuesday April 26, 2016 @07:45PM (#11576708)
How many other two year old languages are there in large scale use? It's not surprising there are issues in compiling things. I've used so many languages early on since I started in the 80's and you can always find some syntax the compiler doesn't like. Given the pedigree of the people building Swift who also built the entire llvm system it's pretty good for something still in diapers.

Now take XCode, that is some real drunk.

Re:Go Home Swift Compiler, You’re Drunk by mrdrozdov on Tuesday April 26, 2016 @08:13PM (#11576901)
Swift development began in 2010.
Re:Go Home Swift Compiler, You’re Drunk by melling on Tuesday April 26, 2016 @04:21PM (#11575296)
The work around was discussed on reddit, which is add the type information for the dictionary:

https://www.reddit.com/r/programming/comments/4givdg/go_home...

"edit: for the code, it's just changing let myCompany to

let myCompany: [String: [String: [String: String]]]'

Re:Go Home Swift Compiler, You’re Drunk by stevejohnson on Tuesday April 26, 2016 @08:45PM (#11577017)
I recently wrote a tool to report Swift block compile times, since this kind of thing can be unpredictable. At Hipmunk we run it every night to find out how we can make our builds faster.

Another common culprit of slow compile times is usage of lazy var.

https://github.com/irskep/swift_compile_times_parser

Re:Go Home Swift Compiler, You’re Drunk by jopamer on Tuesday April 26, 2016 @08:57PM (#11577057)
Thanks, Steve! I'm well aware of the type checker's problems with 'lazy', and it's something I hope to address soon.
Re:Go Home Swift Compiler, You’re Drunk by stevejohnson on Tuesday April 26, 2016 @09:00PM (#11577075)
While you're here, I should say: first, thanks for your work, and second, it would be nice if the compiler could log the timing data in a consolidated form so my script wouldn't be necessary. :-) Regex-parsing and aggregating a bunch of log lines seems dicey.
Re:Go Home Swift Compiler, You’re Drunk by jopamer on Tuesday April 26, 2016 @09:49PM (#11577274)
Unfortunately, debug-time-function-bodies isn't an "officially" supported flag, so improving it isn't a high priority right now.

...That said, we'd be more than happy to take improvement PRs in this area, if you're feeling up for it!

Re:Go Home Swift Compiler, You’re Drunk by rudedogg on Wednesday April 27, 2016 @01:13AM (#11577956)
I'm going to have to try this tomorrow. My builds seem to have gotten slower in the past week or two; hopefully I find something.

Thanks!

Re:Go Home Swift Compiler, You’re Drunk by msoad on Tuesday April 26, 2016 @07:47PM (#11576722)
I guess this means in large application you should always annotate your types. It should be enforced with a linter.
Re:Go Home Swift Compiler, You’re Drunk by geophile on Wednesday April 27, 2016 @12:54AM (#11577903)
I've been using Swift for a couple of months now. While I appreciate that type inference makes for more concise and cleaner code, it also impedes code comprehension. The absence of explicit types forces me, reading an unfamiliar piece of code, to work harder to figure out the type of each expression. This is especially true for expressions of type Optional<T>.

(But at least each expression has a type. While I love Python and have used it for many years, I don't like the uncertainty about types.)

Re:Go Home Swift Compiler, You’re Drunk by mikeash on Wednesday April 27, 2016 @09:36AM (#11580273)
If you're using Xcode, you can option-click a variable and (if Xcode's indexing is working, which is not a certainty) it'll show you the inferred type for that variable. This can help when looking at some new code and trying to figure out what all the bits really are.
Re:Go Home Swift Compiler, You’re Drunk by geophile on Wednesday April 27, 2016 @02:00PM (#11582860)
Yeah, I know, but it's been unreliable.
Re:Go Home Swift Compiler, You’re Drunk by mpweiher on Wednesday April 27, 2016 @02:53AM (#11578333)
In fact, this documentation effect is the only positive effect of static typing that has actual scientific support, unlike the supposed and oft-claimed safety/correctness benefits.

So while type-inference is a neat idea in theory, maybe it's not such a good idea in practice.

Re:Go Home Swift Compiler, You’re Drunk by geophile on Wednesday April 27, 2016 @02:04PM (#11582903)
I'm not sure why you say the safety/correctness benefits are not real. In a large code base, with multiple developers, it is easy to get someone misusing a function, passing one type when a different one is expected. I know that some people in the python world recommend unit testing to guard against this sort of thing, but to me, that is a poor substitute for static type checking.
Re:Go Home Swift Compiler, You’re Drunk by mikeash on Tuesday April 26, 2016 @08:11PM (#11576890)
Not at all. Most code doesn't suffer from this problem. It's a pathological case.
Re:Go Home Swift Compiler, You’re Drunk by catnaroek on Wednesday April 27, 2016 @01:03AM (#11577933)
The code that triggered this long type inference loop seems pretty normal. It's not like Damas-Milner's pathological case (deeply nested instantiations of type schemas), which is genuinely uncommon.
Re:Go Home Swift Compiler, You’re Drunk by mikeash on Wednesday April 27, 2016 @09:34AM (#11580261)
Nested collection literals aren't very common. It certainly doesn't justify requiring something like `let x = f()` to have a type annotation.
Re:Go Home Swift Compiler, You’re Drunk by yandrypozo on Tuesday April 26, 2016 @06:28PM (#11576276)
Wow, this is so sad, that compiler has to be extremely bad implemented
Re:Go Home Swift Compiler, You’re Drunk by pshc on Tuesday April 26, 2016 @06:52PM (#11576419)
It's just a bug. It's pretty easy to accidentally have such cases in a generic type inference algorithm, particularly one with implicit conversions. Other similar languages have had pathological cases in type inference too.
Re:Go Home Swift Compiler, You’re Drunk by mpweiher on Wednesday April 27, 2016 @02:47AM (#11578311)
>It's just a bug [..]

>Other similar languages have had pathological cases in type inference, too

These two statements are slightly contradictory. If this is common, then it's not just a bug, but a difficulty in implementing type inference.