8 ms·
Lightweight API Design in Swift
- gigatexal 7y agoOff topic, but I've been following Sundell for some time now from his podcast and think he's doing some really cool things in the Swift space.
- Austin_Conlon 7y agoDiscovered him from his podcast as well, it's wonderful. Here's an example interview with Chris Lattner: https://www.swiftbysundell.com/podcast/50/ https://www.swiftbysundell.com/podcast/50/.
- gigatexal 7y agoThanks for this! I didn't know they did one with Lattner, should be awesome.
- sgt 7y agoProbably one of the most elegant languages around.
- melling 7y agoYes, it’s my favorite language. Now it needs better cross platform support to be more widely accepted.
- gigatexal 7y agoI hear server-side swift is gaining traction
- clawoo 7y agoAnd a proper IDE and a faster compiler. Xcode is dreadful with its strange fetish for recompilation even if there were no changes and the compiler is slower with every iteration. The solution put forward for this always seems to be "buy newer hardware".
- mojuba 7y agoAbsolutely. One of the things I hate most is this thinking "run-time is more important than compile-time". Well the compiler and the IDE are runtimes too, we developers are users too, we sometimes work on our laptops unplugged and we do want our software to be fast and efficient!
- skohan 7y ago> "run-time is more important than compile-time" This is a pitfall of Rust as well. I suppose there will always be a tradeoff between stronger static analysis and compilation time, and I'm firmly in the camp that I want my compiler to catch more issues so I don't have unexpected runtime failures, but iteration time matters too, and I'm hopeful improvements can be made in this area.
- mojuba 7y agoSometimes you introduce certain restrictions in the language just for the sake of simplifying the compiler. This is allowed, valid and will be understood by the developers. For example, for decades most static languages required a symbol to be defined before its first usage. Removing this restiction seemed like a nice little cute convenience but the outcome of it is this horrific slowdown of the build process. Personally I would agree to the restriction if I knew the compiler could be 5x faster which is pretty realistic to achieve, it seems.
- skohan 7y agoThose kinds of restrictions can have their own productivity pitfalls. For instance, it might be the case that requiring declaration prior to usage explicitly will speed up the compiler, but it might also mean that you can't move blocks of code around without paying careful attention to the order of declaration. I think having a permissive compiler, but also being able to profile the compilation process is a reasonable compromise.
- 7y ago
- tigershark 7y agoF# and Kotlin seem better to me, and don’t have that hideous syntax for the closures. But it’s for sure another world compared to the awful objective c.
- saagarjha 7y ago> the awful objective c :(
- mojuba 7y agoI wouldn't say Kotlin is significantly better than Swift in terms of syntax, they are largely similar to the point you sometimes think: someone copied this from the other, I wonder who was the first. To me personally though the showstopper for Kotlin is that it runs on Java VM. That was the design goal of course, i.e. to become a major "upgrade" of Java esp. on Android. Java VM (with its GC) is a creation of the pre-mobile era when all we had was desktop computers, ever growing processing power and an assumption that you have infinite supply of electric power. It's a different philosophy and design. I think Google's choice of using it on a mobile platform was unfortunate, though at the time (i.e. before the iPhone) many popular mobile platforms did run on Java VM so it seemed like a natural choice.
- algerd 7y agowhat about kotlin native https://kotlinlang.org/docs/reference/native-overview.html https://kotlinlang.org/docs/reference/native-overview.html ?
- singularity2001 7y agoKotlinc takes 10 seconds to compile Hello World, 30 seconds with wasm target. Seems almost conceptually unfixable.
- pjmlp 7y agoswiftc isn't also not that blazing fast. For me Delphi, .NET Native, D are some of the targets to beat in compilation time. They all share a common trait though, not built on top of LLVM.
- jbverschoor 7y agoI'ts def. one of the best designed languages, and has a ton of progress. They're also very agresive with deprecating things that should be changed.
- seanparsons 7y agoWords I never thought I'd see attributed to Swift, it's a blend of a Scala like design with Objective-C parts.
- saagarjha 7y agoIt takes many of the good parts of both and wraps it in some very nice syntactic sugar. What's not to like?
- seanparsons 7y agoCredit where credit is due, they did unicode "right", if you need to deal with those shenanigans it's really nice. However it also adopts a bunch of the bad parts like having variance but not really having a way to handle it. Last I remember the Equatable instance for tuples was just magic, it was impossible to rely on it with the types. Even maybe 6 months ago when I last tried this if you write some code that appends maybe 6/7 arrays together immutably the compiler would go into an exponential spin. The project I'm working on ending up dumping it entirely and moving over to being a web project written in TypeScript because it was so bad. Part of that being a laundry list of problems with the surrounding tooling like SourceKit which may or may not still apply.
- ngcc_hk 7y agoIt is very easy to learn and use. Well compared with java and objective c. Learn it and push the app with v 1.0 is a bit hard but it works. In fact the app still there. Great language.
- pharaohgeek 7y agoI couldn't agree more. I'm anxious to see it build up a rich developer ecosystem like Java has. The availability of so many 3rd party libs for Java is a huge advantage. If Swift gets to that point I can see it taking over all of my server-side development.
- skohan 7y agoWhich types of libraries in particular do you feel are missing?
- jlubawy 7y agoReminds me of this API design post for Go: https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis https://dave.cheney.net/2014/10/17/functional-options-for-fr...
- saagarjha 7y agoMaking DSLs in Swift is surprisingly easy, as this shows, but a brief word of warning: if you overdo it, your API, while pretty at the use-site, can end up being hard to actually use–you'll create what's essentially a read-only language. SwiftUI has taken this style of design to about as far as it's possible to go, and while it's pleasant to read it has a number of pains with regards to API discoverability. Plus the compiler absolutely hates everything about it :(
- skohan 7y agoI have the feeling that SwiftUI was slightly rushed. Swift's language development has been slow and methodical, with a lot of care put into making sure new language features are not only substantially valuable and well conceived from a design standpoint, but also that they don't risk moving the language in a direction which would limit potentially better possibilities in the future. This can be frustrating, as it means it takes a long time for obvious and useful features to make their way into the language (looking at you variadic generics), but the tradeoff is that Swift is a very elegant and well-designed language which doesn't have some of the awkward edges you see in other languages which have to exist for legacy reasons, like you see in C++. In that respect, it seems like function builders skipped that whole process. It's not that they're a bad feature, and similar facilities exist in other languages, but the fact that they were added to the language without a lengthy vetting process seems very out of character. In a way I don't even know if it's really needed. I have a project which uses declarative code to achieve similar goals to SwiftUI, and you can get really close without function builders. For instance, you can write code like this: var view: [View] {[ Foo(...), Bar(...), Baz(...), ]} It's not quite as clean as the function builder approach, and it doesn't have all the same benefits (i.e. producing a typed result vs an array of protocol instances) but it's very easy to understand and doesn't require any additional language features. My theory is that since Google has been pushing Flutter aggressively, Apple needed to answer with a "modern" FRP-style workflow for iOS to retain its developer mindshare.
- gwking 7y agoIt may also have been rushed due to the ABI stability deadline. I'm speculating, but it is easy to imagine that the SwiftUI team saw the window for getting new language-level features closing. More generally, Apple's yearly splash model seems to dictate that teams either release or wait 12 months, if waiting is even an option at all. It makes me wonder how many Apple APIs would have been revised under a more flexible release model.
- skohan 7y agoThis post does a good job of highlighting one of my favorite things about Swift: it's almost always possible to express things exactly how you want. And Swift is getting even better at this with features like property wrappers. Swift has a few hurdles to overcome, like better cross-platform support, solutions to the performance cliffs related to ARC, and more parsimonious copy-handling of value types, but I think it has the potential to be one of the most productive and powerful languages for building software in the next few decades.
- Razengan 7y agoI love Swift as well and prefer to use it for everything wherever I can, but you may find yourself running into walls if you try to get too smart with it. Though I suppose that’s inevitable in any language. An example of something I’ve been struggling against: https://forums.swift.org/t/swiftui-extension-for-os-specific-view-modifiers-that-seems-too-arcane-to-implement/30897 https://forums.swift.org/t/swiftui-extension-for-os-specific...
- nickmqb 7y ago> Since each transform needs to perform vastly different image operations, using an enum in this case would’ve forced us to write one massive switch statement to handle each and every one of those operations — which would most likely become somewhat of a nightmare to maintain. I don't mean to pick on the author, but I've seen this line of reasoning a few times before. It's the same argument that has been used in the past to justify inheritance hierarchies in OOP languages. I used to believe it too. However, I don't think this is actually true. In fact, I'd argue the opposite: switch statements, if used well, are _extremely maintainable_. Even though a switch statement might handle many cases, it does not become more complex [1] by doing so. If we're concerned about the length of the individual cases, we can easily replace each one with a function call. Fundamentally, in the example from the article, we'd like to map a piece of data (the operation with its parameters) to some behavior (actually performing the operation). A switch statement is one of the simplest ways to do that. [1] https://www.infoq.com/presentations/Simple-Made-Easy/ https://www.infoq.com/presentations/Simple-Made-Easy/
- m12k 7y agoAgreed on all points. One of the main metrics I use to assess maintainability of code is 'how many places do I need to edit to make a change?' (within the same file or worse, in other files too), 'how easy is it to find those places?' and 'how easy is it to make a change in one of those places but overlook another needed one?' On pretty much all of those counts, a single switch statement will tend to beat an inheritance hierarchy.
- flipgimble 7y agoWhat you describe is called the Expression Problem [1] in programming language design and there is no simple formulaic answer on which method is better. I think you have to consider many aspects of your code's current design and possible future evolution when deciding which approach to use. For example: do you expect to have more types of transforms, or more operations/method per type of transform? It also means you can't nitpick a limited tutorial for focusing on one approach vs. the other. Fortunately swift (as well as Rust or Kotlin) has excellent modern toolbox that includes protocol conformance and algebraic data types so you can use either one. Keep in mind that swift protocols avoid many of the pitfalls of Java\C++ school of OOP design you might have seen before that can only express "is-a" relationships. [1] https://en.wikipedia.org/wiki/Expression_problem https://en.wikipedia.org/wiki/Expression_problem