ralfd 2 hours ago

I have no experience with Swift.

> The invalid expression from above, where + was applied to String and Int, is still rejected, however with the new algorithm, it only takes the compiler 2 seconds to reach the limit.

I think failing is okay, as the expression can then be explicitly typed. But if it would be solved slowly by the type checker, does Xcode show a slow compile warning for the line that this code should be optimized?

> However, an integer literal such as 123 actually has two default types, Int and Double, and the resulting disjunction has three choices. It might be worth considering a language change where floating point literals must be spelled with a decimal point. Today, expressions involving mixed integer and double literals can be particularly tricky to type check, for this reason.

The habit to write 123.0 for floats is second nature. I think this is a good idea, don’t know if other programmers would find that annoying? (aside the annoyance of changing existing code bases).

  • tarentel 38 minutes ago

    For your first question, yes, it can be optionally turned on and set to a specific value. In the early days of Swift it was quite useful. I still have it on in one project but it rarely shows up anymore and when it does it is usually a mystery as to why it shows up and there's no clear way to fix it.

    For your second point, this is mostly the case for Swift as well. 123 defaults to Int so if you wanted a floating point you'd have to write let x: Double = 123 or let x = 123.0. Most people will default to the latter because it is less typing.

an0malous 12 hours ago

Wow that `let url` example taking 10 seconds and only down to 6 seconds now is shockingly bad. As someone who recently started getting into iOS/macOS development and has noticed these wildly slow type checking times on my M2 Air, this is enough to make me reconsider if I want to invest in this platform...

  • tarentel 28 minutes ago

    It is pretty rough but as someone who has been programming in Swift since 1.0 I can say it rarely happens in practice. In the early days it happened all the time where the compiler would trip up on random expressions. I can still get it to happen, especially on complex closure type things, but even if those did compile they're confusing enough where they really shouldn't make it past code review anyway. That url example should never either.

    I am very curious as to what you're running into where you're seeing wildly slow type checking times.

  • foobazgt 10 hours ago

    No joke, that's just wild. I'd expect an expression like that to type-check literally a million times faster - at the least. Even after reading the article, it's not clear why that particular expression is so egregiously poor.

    • recursivecaveat 7 hours ago

      From the post they link to:

      > The Swift standard library has 17 overloads of + and 9 types adopting the ExpressibleByStringLiteral Protocol. This leads to an exponential combination of types and operators for the constraint solver to try.

      I think the ExpressibleBy thing means that a string literal can be interpreted to mean any of those 9 types. Personally I agree with you; I would actually suggest that the compiler error out if there are anywhere near this many interpretations of an expression. Apparently the corrected expression compiles in 0.19s, which is unacceptable to me. I would much rather pay the cost once of adding a few type annotations or intermediate expressions than pay that fifth of a second over and over and over again for every recompile of that file. Since the types a pretty global constraint system, the expression is a landmine as well: you could fiddle with some distant overload which causes it to attempt the permutations in a different order and suddenly start timing out again.

      • MangoToupe 2 hours ago

        I would rather just have a flag to require type annotations to simply not have to worry about this. I find code much harder to read without them anyway.

    • alain_gilbert 8 hours ago

      > Even after reading the article, it's not clear why that particular expression is so egregiously poor.

      I'm glad I'm not the only one wondering why this is not instant to type check.

    • saagarjha 8 hours ago

      Plus is heavily overloaded

  • boxed 7 hours ago

    That Chris Lattner is taking a very different approach to the type checker in Mojo is afaik due to this kind of thing.

    • catgary 6 hours ago

      Did he get an actual type theorist for that part of the project?

christophilus 4 hours ago

Every time I tinker with languages like Rust or Crystal or Swift, I am amazed by their compilation times. OCaml and Pascal got this right aeons ago. I’m looking forward to a public release of Jai simply because compilation speed is one of its primary motivations. Having a fast feedback loop is crucial to my adhd-like development workflow.

  • undeveloper an hour ago

    isn't rust's compilation speed slow primarily due to its reference / lifetime checker, rather than the "actual compilation" of the program?

jb1991 an hour ago

Kotlin, which has very similar syntax and language goals as swift, though a different runtime of course, does it also suffer from long compilation times for some expressions due to type checking?

fainpul 7 hours ago

I love the clean syntax of Swift, which allows you to omit a lot of syntactic noise. It's so much nicer to write and read than for example Rust with it's ugly wordiness. But the type checker performance really is inacceptable.

  • tailrecursion 7 hours ago

    I too appreciate detail-free programs, and I wonder at the value of including all the typedecls and pointer markings, and lifetime markings, and mutability markings interleaved with the logic. Some people I guess believe that the details are "part of the program" and they aid understanding. Do you buy that?

    I find that sometimes typedecls aid understanding, but they get in the way if they're the least bit complicated. I'm better off reading the program. I never had problems understanding Lisp programs for some reason.

    • zozbot234 2 hours ago

      Maybe you could use a "hide type declarations" view as part of a structurally aware (tree-sitter or LSP-based) code editor?

  • kobebrookskC3 4 hours ago

    it seems like the clean syntax makes the type checker's life harder and so performance is unacceptable. would you rather have clean syntax or acceptable compile times?

    • fainpul 2 hours ago

      I want both! I strongly believe the computer should relieve the user of all the obvious, tedious, boring tasks. That includes figuring out types, if they can be figured out by inspecting the code. Obviously this isn't an easy problem to solve efficiently, but I think it's worth optimizing this heavily, instead of just giving up and forcing the developer to write "dumb" annotations.

      Edit: I'm no expert in this topic, but Prolog interpreters (compilers?) have some fascinating machinery to solve complex constraints. Maybe a similar approach could help here?

      • zozbot234 2 hours ago

        Trait instantiation in most languages is already using a Prolog-like approach.

indemnity 9 hours ago

As someone who started doing SwiftUI recently, it absolutely boggles my mind that (1) this is even a thing and (2) Apple seem ok to treat it as an unsolvable problem. When you finally solve it is some stupid wrong type passed in somewhere. I agree with the other poster. This is so pathetic it makes me question the competence of the engineers working on Swift.

Smells like “we made a poor architectural / design choice and ain’t walking it back”.

  • dilap an hour ago

    Swift is an early example of Apple losing its way. Such a stark contrast to Objective-c -- which was a simple, fast compiling language that hit way above its weight for expressivity and runtime speed. A great language for its day. Swift is "a C++ hacker's first attempt at language design".

    • pjmlp 30 minutes ago

      I would be fine with a Objective-C 3.0, but the big question would be how to fix the underlying C flaws, which was one of Swift's original goals.

      I do agree that the language design has gone overboard in the last couple of years, expecially in the various approaches to parallelism.

      However they are not alone, just look at any programming language sponsored by companies, you need features to justify team sizes and naturally old features don't go away.

      • dilap 3 minutes ago

        Taste & trade-offs aside, you've gotta make it compile reasonably fast! I do get that Objective-C is not the pinacle of language development, but you shouldn't give your main language the rough edges of a research project.

        (And while the past shouldn't necessarily be a shackle on the future, it is striking that such a radically different set of trade-offs was picked for Swift vs Obj-C.)

        I think both Go and C# are pretty nice languages, to give you an idea of where I'm coming from. And Rust is very interesting -- as a user you see software that gets written in it exceed the previous state-of-the-art (e.g., ripgrep).

        I don't see that w/ Swift. It seems like the opposite. E.g., the terrible Settings rewrite that rolled out a couple releases ago...

        Confession, though, while I did a lot of ojbc back in the day, I've never done more than kick the tires on Swift, so I'm not critiquing from a position of deep knowledge -- more like talking shit from the sidelines. But I do think I'm right. ;-)

  • fingerlocks 7 hours ago

    Follow a couple of rules and this won’t happen.

    Type annotate everything. Don’t mix integers and doubles, be explicit. Most important of all, do not ever make a long chain of nested result building function calls with those stupid trailing untyped dangling {} closures, especially when the closure expects an implicit return of an opaque ‘some’ type.

    What I’m saying is, don’t use SwiftUI. It’s a pee filled kiddie pool to attract JavaScript react devs with awful performance and semantics.

    • an0malous 2 hours ago

      That sucks because I really like the semantics of SwiftUI over UIKit. Declarative, reactive code just makes sense for UI.

    • rob74 2 hours ago

      So then, another language which has lots of features, but if you are foolish enough to combine feature A, feature B and feature C, bad things will happen? C++ sends its regards...

    • deliriumchn 5 hours ago

      Well apple themselves are pushing swiftUI and I suspect it will be the main (and only) approach as time goes on, even though its just a wrapper around "older" approach...

  • saagarjha 8 hours ago

    That is exactly what happened and the competence of the engineers working on the language is not really relevant.

  • mdhb 5 hours ago

    There is a reason why 1/3 apps in the Apple App Store are now written in Flutter instead.

    • moooo99 2 hours ago

      Because its easier to write one piece to code to target two platforms, regardless of how good or bad the native language for each platform is?

    • an0malous 2 hours ago

      Is Flutter more popular than React Native now?

foobazgt 9 hours ago

Slava mentions both bidirectional inferencing and overloading as two of the big culprits.

I've been doing some language work recently, and I'm quite sympathetic to bidirectional inferencing. I think, though, that modern PLs need better solutions for adhoc overloading. It's notorious for its complexity, blowing up algorithmically, and confusing users with surprising results (why oh why did the compiler select this function over the one I intended). That said, I haven't discovered a good alternative (for my purposes) yet.

  • zozbot234 4 hours ago

    > I think, though, that modern PLs need better solutions for adhoc overloading.

    So, something like "How to make ad-hoc polymorphism less ad hoc"?

    • mrkeen 2 hours ago

      Orphanless typeclasses FTW

SurceBeats 13 hours ago

About time! Nothing kills productivity like waiting 30s for the compiler to tell you it can't infer a type on line 27437 (who needs to refactor?). The diagnostic improvements alone will be worth it really but...

  • bestouff 9 hours ago

    Going from 10s down to 6s is not nearly near sufficient to me. This should be instant.