News for hackers. Stuff that matters.
Re:On Apple's Love Affair with Swift by pavlov on Saturday August 11, 2018 @08:53AM (#17739485)
Chris Lattner writes:

”One of the best and most annoying things about Objective-C is that it has C in it. This has been hugely important for Objective-C in practice, because if you run into a performance problem with objc_msgSend, you can always rewrite that algorithm in C. That’s really, really, really important for Objective-C being successful in both in the days of NeXT on 16 MHz processors and also today for the low-level code that people are writing.”

This is indeed a great thing about Objective-C: it’s an improved C with a tight set of runtime features that help build interactive applications.

There’s a need for such a language, and I’m sad that so much of the rhetoric from Apple and the Swift community is working on the assumption that Obj-C should be completely replaced by Swift.

Swift is not a compact language like Objective-C. It’s more like C++ in how it tries to incorporate every programming paradigm that can possibly fit. The heavy focus on complex upfront typing is not necessarily the best choice for iterative app development.

Swift is useful in many places, but personally I found Objective-C’s “Smalltalk-flavored C” to hit a sweet spot for my GUI needs where integrating with C and C++ libraries is usually required all over the place.

Re:On Apple's Love Affair with Swift by pjmlp on Saturday August 11, 2018 @09:09AM (#17739528)
Yeah, but you forgot to copy the other relevant part.

> The fundamental problem was Objective-C was built on top of C. C inherently has pointers. It has uninitialized variables. It has array overflows. It has all these problems that even if you have full control of your compiler and tool stack, you just can’t fix. To fix dangling pointers, you would have to fix lifetime issues, and C doesn’t have a framework to reason about that, and retrofitting that into a compatible way into the system just wouldn’t really work.

If you took away C from Objective-C, you couldn’t use C arrays on the stack, for example. And if you couldn’t do that, there’s entire classes of applications where the performance just wouldn’t be acceptable. We went around, around, around. We said the only way that this can make sense in terms of the cost of the disruption to the community is if we make it a safe programming language: not “safe” as in “you can have no bugs,” but “safe” in terms of memory safety while also providing high performance and moving the programming model forward.

Re:On Apple's Love Affair with Swift by pavlov on Saturday August 11, 2018 @09:20AM (#17739562)
That’s a good reason to create a new language. But all the C code in the world isn’t suddenly going to go away, and so the need for an improved C still remains.
Re:On Apple's Love Affair with Swift by pjmlp on Saturday August 11, 2018 @10:07AM (#17739724)
Language design graveyard is full of improved C attempts, because the problem is not technical, rather it can only be properly solved through change of generations.

Microsoft, Apple, Google have the luxury to impose moving away from C on their platforms.

On other platforms you have developers that won't touch any kind of Safe C even at gun point.

Re:On Apple's Love Affair with Swift by josephg on Saturday August 11, 2018 @09:41AM (#17739630)
That’s why LLVM is such a great idea - any llvm language can interoperate with any other, within the same binary, with no overhead. Apple isn’t throwing away their C code any time soon either. LLVM provides a smooth upgrade path via mixed language binaries.

I suspect that the move to swift would never have happened without LLVM. And I wouldn’t be surprised if llvm’s cross language compatibility was specifically attractive to Apple because it let them have their cake and eat it too - write a new language but keep legacy C code whenever that’s the best option.

Re:On Apple's Love Affair with Swift by masklinn on Saturday August 11, 2018 @11:38AM (#17740086)
> That’s why LLVM is such a great idea - any llvm language can interoperate with any other, within the same binary, with no overhead.

I don't believe LLVM provides a stable cross language ABI, and AFAIK languages just use regular FFI (via the C ABI) to communicate with one another, regardless of them being implemented on LLVM or not.

Re:On Apple's Love Affair with Swift by ihnorton on Saturday August 11, 2018 @01:32PM (#17740636)
> I don't believe LLVM provides a stable cross language ABI, and AFAIK languages just use regular FFI (via the C ABI) to communicate with one another

Provided the compilers generate compatible versions of LLVM IR, then cross-language calls can be done at the IR level. See github.com/Keno/Cxx.jl

IIUC Swift already uses clang to load module maps in order to expose and call Objective-C, and they plan to do something similar with C++ (no link handy but I think there is an issue or enhancement proposal about it).

Re:On Apple's Love Affair with Swift by slavapestov on Saturday August 11, 2018 @08:46PM (#17742327)
Yeah, Swift’s C interop goes further than most typical FFIs because it actually calls into Clang internals to read headers, lay out structs and form calls. C and Objective-C types, functions and methods are mapped to Swift definitions, by directly looking at the Clang AST.
Re:On Apple's Love Affair with Swift by matheusmoreira on Sunday August 12, 2018 @01:28PM (#17745353)
> C ABI

Is there really such a thing? I thought the ABI was dependent on the operating system and compiler.

I agree that C is unique in it's ability to generate programs that can be interfaced with from any language. This also seems to apply to older languages like Fortran and Pascal, for some reason. Higher level languages are an ABI nightmare; seriously doubt anyone will ever dynamically link against a C++ library at runtime and call a method on a C++ object from any language that isn't C++.

Re:On Apple's Love Affair with Swift by DannyBee on Saturday August 11, 2018 @03:17PM (#17741096)
1. As someone says, LLVM does not have a cross-language ABI, so no, it's not as easy as you think. It makes some things nicer, some things harder.

2. Prior to LLVM, there were much more complete attempts at this (that were even more successful, actually!)

Re:On Apple's Love Affair with Swift by earenndil on Saturday August 11, 2018 @09:15PM (#17742429)
As the siblings have mentioned, llvm doesn't really do this. What does, though, are the jvm and .net, although those aren't really oriented towards low-level c programming.
Re:On Apple's Love Affair with Swift by pjmlp on Saturday August 11, 2018 @10:12AM (#17739740)
It is a great idea, but not an innovative one, already PL/8 for IBM RISC research used a similar approach.
Re:On Apple's Love Affair with Swift by blub on Sunday August 12, 2018 @03:54AM (#17743423)
My guess is that a lot of the C hold-outs will never switch to another language. C represents the siren's call for them: the best performance, requires skilled handling, no seatbelts, stable definition.

A lot of C's endurance is related to programmer psychology, so it would be futile to design a better C.

C++, Rust, Go, Java will continue to drain the pool of C developers until it's only used for maintenance of existing projects.

This is why it's extremely irksome to see people recommend mixing C and e.g. Python for speed, or other such Frankenstein's monsters - it's just prolonging the agony of whoever will maintain that.

Re:On Apple's Love Affair with Swift by speedplane on Sunday August 12, 2018 @04:06AM (#17743457)
C still represents the closest approximation of what is happening on a chip, quasi-atomic operations acting on finite resources like registers, memory, and I/O. Even if you don't know assembly, you can reasonably understand what various chips will do with your C code, especially low level C code that occasionally dips into assembly (e.g., handling interrupts or chip-level atomic operations).

Rust, Go, and Java, with their memory management and cross-platform portability, and general non-determinism may eat away at the edge of C, but can never replace it.

I could see them fully replacing C if features like garbage collection and byte-code interpretation were handled at the chip-level, so languages would not have to necessarily deal with them. Could also see it happening if a chip's out-of-order execution was exposed at the language level. That would not necessarily make C worse, but Java may be more adept at handling it.

Re:On Apple's Love Affair with Swift by blub on Sunday August 12, 2018 @05:30AM (#17743658)
You've made several points...

1. C is no closer than C++, or Rust (including unsafe) and they're all surprisingly pretty far from the hardware these days... this is visible the most in multi-threading, where high-level C statements are re-ordered by the compiler and even simple C statements like an integer assignment can be split into multiple instructions. Inlining and register allocation is also quite opaque and perhaps best left to the compiler, unless one uses non-standard code to force specific behaviour.

2. Well put: Go and Java were/are eating at the edge of C. Java took serious bites in the past, nowadays they're not really competing much. Today, what might have been a small Linux C program could be a Go program, especially if the performance constraints aren't too tight. Technically speaking, Rust can replace C almost everywhere, it's just that it's a significantly more complex language.

Re:On Apple's Love Affair with Swift by speedplane on Wednesday August 15, 2018 @12:20AM (#17763749)
Regarding #1, disagree. In C, a function call is just a request to send the instruction pointer to another point in the memory space. In C, code is treated as data, which is how it's treated in hardware.

C++ introduces virtual functions, and when you make a function call, you're no longer going to a single place in memory, you go to where the class's virtual function table tells you to go, effectively jumping to a memory pointer. This is pretty helpful, but it's an incredibly different way to think about code. A function is no longer a set of instructions in memory, it's a logical representation of functionality.

And yes, I know in C, you can set a variable to a function location, and then jump there, but it's painful to do and you have to do it explicitly, so it doesn't really create an abstraction.

Re:On Apple's Love Affair with Swift by pjmlp on Sunday August 12, 2018 @12:32PM (#17745090)
Regarding 2.

I am fully convinced that if Java and .NET had a proper AOT toolchain from day 1, meaning beyond dynamic linking (NGEN) or commercial offerings from 3rd party JDKs, C's influence on FOSS world would have been much less than it achieved to be.

And we would already have achieved an UNIX like stack closer to what Inferno/Limbo was all about.

Re:On Apple's Love Affair with Swift by nine_k on Saturday August 11, 2018 @05:55PM (#17741773)
> you couldn’t use C arrays on the stack, for example. And if you couldn’t do that, there’s entire classes of applications where the performance just wouldn’t be acceptable

Would you mind naming a few such classes?

Why would arena allocation, or evrn good generational GC fail to deliver the performance when quick allocation / deallocation of (large) arrays is required?

Re:On Apple's Love Affair with Swift by slavapestov on Saturday August 11, 2018 @08:43PM (#17742318)
Generally one feature GCd languages lack is strong support for value types. Cache thrashing due to pointer chasing is an issue, and even if you can allocate large numbers of small objects quickly, the indirection will kill you. In Swift, many small allocations are avoided entirely by storing the value as part of some other object. For example, imagine you want to store an array of points; it’s usually much better if you can have an array of structs of floats where everything is represented as one flat allocation.
Re:On Apple's Love Affair with Swift by pjmlp on Sunday August 12, 2018 @12:36PM (#17745111)
Oberon, Oberon-2, Active Oberon, Component Pascal, Eiffel, Common Lisp, Modula-2+, Modula-3, CLU, C#, D all have strong support for value types.

Java just made a couple of wrong decisions regarding AOT and value types that are now taking years to correct course, while avoid breaking backwards compatibility.

If something like Modula-3 had been granted Java's success story, we wouldn't be talking about how GC languages don't have good support for value types.

Re:On Apple's Love Affair with Swift by slavapestov on Monday August 13, 2018 @12:48AM (#17748063)
I think we have different definitions of value types - in all Common Lisp implementations I’m aware of values have a uniform representation as a (tagged) pointer.

Also I think C# structs do not have mutable fields, but I could be wrong.

Edit: it looks like C# structs are mutable, my mistake.

Re:On Apple's Love Affair with Swift by pjmlp on Monday August 13, 2018 @02:16AM (#17748320)
Common Lisp is just one example among 11 programming languages that I have listed.

Maybe take some time to check the other ones?

As for value types in Common Lisp, even if one bit is lost for tagging, it doesn't make arrays, structs and primitive values, less of value types.

Re:On Apple's Love Affair with Swift by h4b4n3r0 on Saturday August 11, 2018 @10:15PM (#17742593)
C# had value types from day one.
Re:On Apple's Love Affair with Swift by slavapestov on Sunday August 12, 2018 @12:09AM (#17742841)
Yeah, its one of the few that does. Even then, C# value types are immutable, and there are no value type collections (not a collection of value types, but a collection that is itself a value type). The lack of these two features means it’s not as generally useful as you might like.
Re:On Apple's Love Affair with Swift by h4b4n3r0 on Sunday August 12, 2018 @01:54AM (#17743125)
An array of value types (including structs) in .NET (not just C#) is laid out contiguously in memory and stored in-place like an array of C/C++ structs would be. The problem of chasing pointers does not exist if you use structs (and don't use non-value types such as strings within those structs). And .NET value types are mutable, of course.
Re:On Apple's Love Affair with Swift by slavapestov on Monday August 13, 2018 @12:50AM (#17748069)
Right, but the array itself has reference semantics.
Re:On Apple's Love Affair with Swift by pjmlp on Monday August 13, 2018 @02:17AM (#17748326)
Not when you allocate it on the stack via stackalloc, as of C# 7 also allowed in safe code.
Re:On Apple's Love Affair with Swift by __abc on Saturday August 11, 2018 @11:04PM (#17742683)
And ....
Re:On Apple's Love Affair with Swift by 0x0 on Saturday August 11, 2018 @09:25AM (#17739579)
ARC in objective-c has almost fully solved this issue in practice.
Re:On Apple's Love Affair with Swift by valuearb on Saturday August 11, 2018 @06:15PM (#17741854)
Nope. I still found many ways to crash my objective C apps while using ARC. Because Swift is memory safe I no longer have to give up days or weeks of my life tracking down obscure bugs caused by overwriting memory I should not have.
Re:On Apple's Love Affair with Swift by gurkendoktor on Sunday August 12, 2018 @05:33AM (#17743665)
Could you please give some concrete examples of how memory corruption has affected you in modern Objective-C? NSArray has range checks, delegates can't dangle anymore because they're weak references now. I think that leaves interaction with C APIs, but don't Swift users have to deal with UnsafeMutablePointer to use them - which isn't memory safe either?
Re:On Apple's Love Affair with Swift by jakobegger on Sunday August 12, 2018 @09:53AM (#17744347)
The easiest way to cause memory issues is to access mutable collections from multiple threads. I really wish NSMutableArray and friends were thread safe...

Unfortunately, Swift doesn't fix that AFAIK.

Re:On Apple's Love Affair with Swift by lerno on Wednesday August 15, 2018 @05:42PM (#17770040)
No no no. That is a one-way trip to hell. There is no ”good for all threading scenarios” thread safe mutable array design.

I’ve debugged too much java code where the programmers thought that ”thread-safe” meant ”no need to think about threading issues”.

Re:On Apple's Love Affair with Swift by blub on Sunday August 12, 2018 @03:58AM (#17743434)
Thanks for having the patience to put all these Obj-C myths to rest.

The Obj-C community (or what's left of it) is trying to make a martyr of an error-prone, cumbersome programming language.

Re:On Apple's Love Affair with Swift by pjmlp on Saturday August 11, 2018 @10:01AM (#17739690)
Try to use ARC with C APIs.
Re:On Apple's Love Affair with Swift by 0x0 on Saturday August 11, 2018 @10:03AM (#17739706)
That's like saying try to use Java JNI with C APIs. Think of direct C API access in Objective-C a bonus, not a design pattern :)
Re:On Apple's Love Affair with Swift by pjmlp on Saturday August 11, 2018 @10:11AM (#17739735)
Completly different, ARC only works with APIs that follow Cocoa retain/release patterns, which aren't the only APIs that one needs to use, e.g. CoreGraphics.

And unlike JNI they aren't protected by a VM wall.

Re:On Apple's Love Affair with Swift by _ph_ on Saturday August 11, 2018 @09:24AM (#17739575)
I can understand the need to create a new programming langauge, but as the previous poster wrote, they rather recreated all the complexity of C++, instead of a more lean language. Also, Swift seems to suffer from overly static typing in its object oriented approach - which defeats the idea of object oriented programming.
Re:On Apple's Love Affair with Swift by masklinn on Saturday August 11, 2018 @11:40AM (#17740094)
> they rather recreated all the complexity of C++

I find that this assertion holds about as much water for Swift as it does for Rust, Swift is not even remotely close to C++'s in terms of complexity, and that's with building in mechanisms for performant memory safety, a concept which C++ literally just shits on to this day and for the foreseeable future.

Re:On Apple's Love Affair with Swift by valuearb on Saturday August 11, 2018 @06:22PM (#17741883)
Swift is a really approachable language, it’s core features are simple and the code is very readable. You can write any app without having to use generics, extensions, protocols, more esoteric enum features, etc, etc.

But once you learn to use the more advanced features, your code just gets easier to write and maintain. I enjoy that I can pickup new language features in Swift one at a time, figuring out where each adds its best value, and occasionally refactoring to take advantage as part of my ongoing efforts to minimize code debt.

Re:On Apple's Love Affair with Swift by zepto on Saturday August 11, 2018 @12:29PM (#17740311)
It’s hard to take seriously the idea that swift and c++ have equivalent complexity if you have delivered products using both languages.

It’s not even close. Swift is (still) minimalist by comparison to C++.

Re:On Apple's Love Affair with Swift by sanxiyn on Saturday August 11, 2018 @11:51AM (#17740136)
Swift is complex, but it is not C++-complex. C++ is in the completely different league.
Re:On Apple's Love Affair with Swift by pertymcpert on Saturday August 11, 2018 @07:54PM (#17742184)
That's extremely hyperbolic. I've worked on multiple C++ FEs and it's orders of magnitude more complex than Swift.
Re:On Apple's Love Affair with Swift by SlowRobotAhead on Saturday August 11, 2018 @06:28PM (#17741897)
As an embedded engineer I can’t agree that C “has problems” because it treats you like a big boy with your big boy pants on. :)
Re:On Apple's Love Affair with Swift by fisherjeff on Saturday August 11, 2018 @07:46PM (#17742149)
I work almost exclusively in embedded C and even I can’t agree with this. Just for starters, those two statements are not mutually exclusive.
Re:On Apple's Love Affair with Swift by ovao on Saturday August 11, 2018 @06:46PM (#17741953)
Which is a problem for developers who simply want to write code without having to reason about whether it’s safe to dereference something, and without having to worry about a simple oversight resulting in the programmatic equivalent of summoning Cthulhu.
Re:On Apple's Love Affair with Swift by SlowRobotAhead on Sunday August 12, 2018 @02:48AM (#17743266)
>a problem for developers who simply want to write code without having to reason

Still not seeing the problem.

Pointers aren’t magic and aren’t hard.

Re:On Apple's Love Affair with Swift by ovao on Wednesday August 15, 2018 @12:22AM (#17763753)
Pointers, conceptually speaking, are not hard.

Pointers, in practice, are hard. Even C-family compilers cannot safely reason about them in many (most) cases.

Re:On Apple's Love Affair with Swift by pertymcpert on Monday August 13, 2018 @09:21AM (#17749816)
Oh boy, trying to debug some miscompile because of a subtlety in aliasing rules would make me disagree.
Re:On Apple's Love Affair with Swift by pertymcpert on Saturday August 11, 2018 @07:56PM (#17742190)
As a C compiler engineer I can assure you it has problems.
Re:On Apple's Love Affair with Swift by SlowRobotAhead on Sunday August 12, 2018 @05:31PM (#17746515)
Did I say C didn’t have problems? Or did a say it’s not a problem that C might allow you to do unsafe things?
Re:On Apple's Love Affair with Swift by pertymcpert on Monday August 13, 2018 @09:23AM (#17749823)
I think so?

“As an embedded engineer I can’t agree that C “has problems” because it treats you like a big boy with your big boy pants on. :)”

Re:On Apple's Love Affair with Swift by SlowRobotAhead on Monday August 13, 2018 @07:24PM (#17754360)
Do you know what “because” in that sentence means?

Edit: Nah, that’s OK. Just downvote and scamper on. Make HN more like Reddit, that’s what this place needs!

Re:On Apple's Love Affair with Swift by dang on Tuesday August 14, 2018 @03:49AM (#17756191)
Could you please stop posting unsubstantive comments to Hacker News?
Re:On Apple's Love Affair with Swift by pertymcpert on Tuesday August 14, 2018 @06:39AM (#17756725)
Well I took it to mean that because you think it treats you as a "big boy" you don't think it has problems.
Re:On Apple's Love Affair with Swift by pertymcpert on Tuesday August 14, 2018 @06:41AM (#17756730)
Oh and I have no idea how to downvote on HN. Not that I care, but I don't see any downvote buttons.
Re:On Apple's Love Affair with Swift by blub on Sunday August 12, 2018 @04:05AM (#17743455)
There is no such thing as "big boys" in software engineering, there's only humans which have certain cognitive weaknesses.

C is really good at exploiting those, while at the same time giving programmers a feeling of power.

Re:On Apple's Love Affair with Swift by speedplane on Sunday August 12, 2018 @04:08AM (#17743464)
So providing a feeling of power over the language and it's abilities is not a legitimate advantage of the language?

Isn't that why everyone loves python, solving a complex problem in a handful of easily readable lines of code?

Re:On Apple's Love Affair with Swift by blub on Sunday August 12, 2018 @05:34AM (#17743666)
The feeling of power is nice for the programmer, but it doesn't influence the technical capabilities of the language, which are deplorable safety-wise.
Re:On Apple's Love Affair with Swift by SlowRobotAhead on Sunday August 12, 2018 @12:35PM (#17745107)
Did you see the smiley face in my post that’s implied I’m kinda joking.

Also... I’m a little concerned that your post has “we’re all equal” social justice vibe to it... which is silly. C is more powerful than because “safety features” aren’t there.... deplorable... LOL.

You try going a day without using something programmed in C.

Re:On Apple's Love Affair with Swift by blub on Tuesday August 14, 2018 @02:25AM (#17755944)
Oh boy, how I wish I could have at least one day without something programmed in C. I'm so sick of all the vulnerabilities...

No social justice vibe was meant. I was trying to say that we're all bad at dealing with the kind of errors which appear in a typical C or (perhaps to a lesser extent) C++ program.

I know that some people are able to do this more successfully than others because of skill, etc, but it's not just about certain individuals. As a whole, the C community has a really poor track record at security and safety and that has a lot to do with the language itself.

Re:On Apple's Love Affair with Swift by chillacy on Monday August 13, 2018 @02:24AM (#17748352)
Tone is a tough thing to get right, and I'd say your initial post came off pretty condescending to a lot of people, regardless of whether you meant it or not. It's not even a C thing either, I've seen the same thing where someone declares functional programming as the only "serious programming" and category theory is easy, so everyone else is wasting time with imperative languages. So, it's a bit off-putting.
Re:On Apple's Love Affair with Swift by pjmlp on Sunday August 12, 2018 @12:40PM (#17745139)
Every single day back when I was developing software for ZX Spectrum, Amiga and MS-DOS, before UNIX's influence took over the industry and forced us into dealing with C.

Thankfully there was already C++ for those of us that wouldn't move into such a limited language versus what something like Turbo Pascal 6.0 for MS-DOS was already capable of, feature wise.

Re:On Apple's Love Affair with Swift by SlowRobotAhead on Sunday August 12, 2018 @01:08PM (#17745271)
Ok... so if I’m getting the idea here... C is more limited, but also dangerous because it gives direct access to memory and pointers making it dangerous.

Man... to listen to hackernews tell it... no one should use C!

I wonder how many comments describing how bad C is are from people on Linux machines?

Re:On Apple's Love Affair with Swift by pjmlp on Monday August 13, 2018 @02:26AM (#17748364)
The problem is not direct access to memory and pointers as such, every systems programming language needs to provide such features.

The problem with C, versus other systems programming languages it that it its pointers, implicit conversions of array into pointers, integer operations, string and array bounds don't have any kind of validation.

On other systems languages disabling such validations is explicit.

C is like JavaScript in the browser, we have to put up with it because UNIX clones won the market of commodity OSes.

C on its own without UNIX, would have failed in the market.

On the desktop world it was already being replaced by C++, Smalltalk, VB, Delphi and 4GL before FOSS based on UNIX and C started to be widespread.

Unfortunately it is impossible to convince people on pure UNIX based OSes to use something else.

Fortunately, Apple, Microsoft, Google, Sony, Nintendo are able to set which programming languages one is allowed to use on their platforms and C is on the way out on their SDKs.

Re:On Apple's Love Affair with Swift by SlowRobotAhead on Tuesday August 14, 2018 @02:43AM (#17755997)
LOL yea, C is going away alright. Just as soon as every company you mentioned and everyone else in the world stops making embedded electronics!
Re:On Apple's Love Affair with Swift by coldcode on Saturday August 11, 2018 @09:12AM (#17739541)
I have used Objective-C since 1997 when I worked with WebObjects back then and I don't miss it for a second. When I have to touch our one codebase with Objective-C I have to groan. It was amazing for its day and I did enjoy it despite its shortcomings. But I can write way more highly functional code faster in Swift than I ever could in Objective-C due to real type safety and other modern features. I also spent years doing regular C and C++ and Java (not at the same time) and all 3 languages seem clunky to me now. Preferring Objective-C over Swift is wishing for the golden oldie days kind of thinking and I'm one with almost 4 decades of programming experience. Swift is still young and growing and sometimes thats a bit challenging to adapt, but its the one language I truly enjoy now.
Re:On Apple's Love Affair with Swift by apple4ever on Saturday August 11, 2018 @03:52PM (#17741232)
I’m the opposite. When I see Swift I just groan. It’s so much hard and slower to write more highly functional code (not to mention going back to ready it) than Objective-C. Part of that is lots of things about Swift are backwards (variable definition) or overly expressive (var/let/func).

I just cannot enjoy Swift at all. When I go back to Objective-C its just such a joy to write.

Re:On Apple's Love Affair with Swift by valuearb on Saturday August 11, 2018 @06:36PM (#17741922)
Developer productivity is far less determined by how fast you can write code, than by how many bugs you create, and how hard it is to fix them. The vast majority of time is spent debugging and maintaining code.

And I feel like I can write in Swift nearly as fast as Objective C. Objective C was always so baroque, when I define a method or variable and declare the exact same thing in a header file? Properties syntax?

I actually switched when Swift 1.0 came out (just after writing a twenty thousand line simulation engine in Objective C). It was so lightly baked I was starting to regret my decision for a couple months until version 1.2 released, only then could I ship my app commercially. Since then every release has only gotten better, and the rough edges are entirely gone. The only thing I miss from Objective C is compile time, and now just barely given how much faster the compiles have gotten.

Re:On Apple's Love Affair with Swift by gurkendoktor on Sunday August 12, 2018 @06:38AM (#17743820)
> The vast majority of time is spent debugging and maintaining code.

Not all programming is the same. I've seen a lot more code churn in frontend projects than in the backend, where the actual logic and persistence happens. That makes the speed of (re)writing code relatively more important (at least for the iOS apps I've worked on, YMMV).

But even when I had to clean up legacy code, I spent most of my debugging time on on incorrect threading (typically using UIKit or Core Data from the wrong thread), AutoLayout errors, Apple framework bugs, storyboards/NIBs out of sync with code, performance degradations, or anything else where Swift doesn't help much.

In my experience, Objective-C programmers aren't allergic to safety or brevity. We love ARC! It's just that Swift has taken away too much productivity, mostly through terrible tooling and by dividing the community, for very little gain.

> Objective C was always so baroque, when I define a method or variable and declare the exact same thing in a header file

When I helped out on a Java project, I noticed that my coworkers religiously created interfaces for every single class they wrote. They couldn't really explain it beyond it being a "best practice", but I suspect they enjoyed being able to look at a short file that only contained the interface plus JavaDoc, instead of opening the full implementation every time. They basically re-invented header files :P

I don't think people would mind headers as much if Xcode was a better IDE, and we didn't have to copy declarations around manually.

Re:On Apple's Love Affair with Swift by pjmlp on Sunday August 12, 2018 @12:43PM (#17745162)
> They couldn't really explain it beyond it being a "best practice", but I suspect they enjoyed being able to look at a short file that only contained the interface plus JavaDoc, instead of opening the full implementation every time. They basically re-invented header files :P

Then they should have known better.

That makes sense for unit testing, in cases where mock classes would be required.

All Java IDEs have object browsers no need for header files.

Re:On Apple's Love Affair with Swift by gurkendoktor on Monday August 13, 2018 @05:12AM (#17748869)
Mockability was the official reason until we started using Mockito 2. I hope everyone on the team was aware of the object browser. And yet they felt better with interfaces everywhere. It might have been a simple case of cargo-culting, though.
Re:On Apple's Love Affair with Swift by baby on Saturday August 11, 2018 @05:02PM (#17741525)
I'm surprised that anyone could find Objective-C a better language than Swift. I'm just waiting for iOS to move completely to Swift to start developing on that platform. It's a much better language.
Re:On Apple's Love Affair with Swift by vbezhenar on Saturday August 11, 2018 @05:35PM (#17741686)
Not everyone wants features. Go is an extremely simple language, it doesn't even have generics. People use it widely anyway.
Re:On Apple's Love Affair with Swift by Redoubts on Sunday August 12, 2018 @01:15AM (#17743042)
> Part of that is lots of things about Swift are backwards (variable definition) or overly expressive (var/let/func).

Is this a joke? This sounds like "the language is wrong because it didn't completely ape C"

Re:On Apple's Love Affair with Swift by 0x0 on Saturday August 11, 2018 @09:27AM (#17739588)
Objective-C in 1997 and in 2017 are almost two different languages. Between ARC and generics, auto-synthesized ivars, and blocks, there's not much missing from being a modern C#/Java-like language.
Re:On Apple's Love Affair with Swift by zoul on Saturday August 11, 2018 @09:58AM (#17739677)
I love Objective-C, but calling it (close to) a modern language is a long stretch. The “generics” are a joke, for example. The whole type system is miles from what can be expressed (and guaranteed!) by a modern language. We got an immense amount of fun out of it considering its age, but it’s time to move on.
Re:On Apple's Love Affair with Swift by tsycho on Saturday August 11, 2018 @12:01PM (#17740187)
Totally agree that generics in objective c are very bare. And that's what frustrates me the most. Apple/Lattner could have improved them, but chose not to, by focusing all their energy on Swift.

"Simple" things I want from generics:

1. Carry over generic information from header files to .m files, and not force us to drop all type information by having to use "id".

2. A compiler flag to enforce that the generic type must be specified at usage.

3. Where clauses in generics: Foo <T where T : NSCopying, MyProtocol>>

4. Generics on protocols, ideally with all the other features.

I know some of these are not trivial to implement, but they are mostly new syntax and hence don't have backward compatibility issues (except #2 which is why I asked for a compiler flag).

Re:On Apple's Love Affair with Swift by mariodiana on Saturday August 11, 2018 @06:11PM (#17741833)
I wan't to ask in all seriousness, or in all ignorance—I don't mind, so please take your pick—why generics are so important. Let's say, in the first case, you have an NSArray in Objective-C. The object in the array is of type id. Now, in the second case, you have an NSArray, but you've declared that it will only hold types of NSString. Why is the second any better?

I ask, because the only way I could see it being better is if you're taking that array—that data structure—and passing it all over the program and using it everywhere. (Note to the literal-minded: this is hyperbole.) If that's what you're doing, that's not Object-Oriented Programming.

If you're instead doing real OOP, you're designing an object that will perform a clearly circumscribed bit of work for you by responding to a set of messages. The NSArray may live inside that object—in other words, the object is composed in part by using an NSArray—but that array is hidden from the rest of the program.

Generics are no big whoop-di-do in that case, because there is no client-programmer that has to worry about them; or, to put it another way, you—the original programmer—only have to worry about that NSArray with respect to the programming of that one object. In that case, the data type the array holds is not a big deal to keep track of.

So, again—seriously—what is so great about generics? The whole point of Objective-C, originally, was that it was late-binding—which, if you ask Alan Kay, was the point of OOP in the first place. If you're worrying about types all over the place, you're doing something wrong, no?

Re:On Apple's Love Affair with Swift by valuearb on Saturday August 11, 2018 @06:43PM (#17741943)
Your array of NSString example has little to do with generics. Instead think of a class that contains an NSArray, and has methods to do specific things to the array items, for example a sort() method. Using generics you can define the array values to match the type given when the class is created. So the same class could contain NSStrings, UImages, UIControls, etc, etc, and sort accordingly.
Re:On Apple's Love Affair with Swift by slavapestov on Saturday August 11, 2018 @09:09PM (#17742399)
Alan Kay’s ideas are very interesting and Smalltalk introduced many groundbreaking ideas that still inspire programmers today. But there’s no fundamental law of nature that says dynamic message passing OOP is the “right” way to approach OOP, or even that OOP is the best paradigm for many coding problems developers face today.
Re:On Apple's Love Affair with Swift by mariodiana on Sunday August 12, 2018 @02:26PM (#17745650)
Okay, the context of the reply to which I was initially replying to was about how the implementation of generics in Objective-C was "very bare." I'm beginning with this point, because though I agree with your second point about OOP perhaps not being necessarily "the best paradigm for coding problems developers face today," I don't quite see where that point fits in. We're talking about Objective-C, which means we're talking about OOP. If you want to explore some other kind of programming paradigm and insist that generics are needed there, I have no quarrel with you. My point was that I didn't see why generics were needed in OOP.

But that brings us to your first point: namely, what is the "right" way to approach OOP.

When I bring up Alan Kay, I'm bringing up a guy who was there at the start, and who set his stamp on OOP. Granted, "OOP" has been done in different ways. You'll note, however, my use of scare quotes. Allow me to explain.

When OOP had the misfortune to become a managerial buzz, somewhere back in the 1990's, and thus became a "must-have," what happened was a lot of procedural programmers simply took their procedural programming, wrapped in in C++, compiled it with a C++ compiler, nodded to the boss and assured him that they were doing OOP—and went along on their happy way.

Likewise, when college professors—set in their ways, as is their want—were told that they had to start teaching OOP, they largely did the same thing.

The result is that we have lots and lots of people out in the world "doing OOP"—who actually aren't. They're writing procedural programs, with the trappings of classes and sub-classing and what not. But all over in their programs, they are digging into the internals of their classes—and, sometimes, you see them digging in many layers deep.

And they're "doing OOP."

As I understand him, Kay is saying that you send a message to an object, on the understanding that the object will handle the message, the sender not having to worry about the ultimate who or how. If there's another kind of OOP other than that—or other than the travesty I described above—what is it?

But, that's another question, entirely. My original question was: why are generics such a big deal in OOP? I don't believe that's been answered.

Re:On Apple's Love Affair with Swift by pjmlp on Sunday August 12, 2018 @12:44PM (#17745170)
And even Smalltalk had many ideas from FP, like blocks (closures) and the collection messages similar to what modern developers know as LINQ.
Re:On Apple's Love Affair with Swift by zoul on Sunday August 12, 2018 @02:05AM (#17743149)
For me, the big deal about generics is that they allow the type system to cover a much bigger part of my design. Without generics, any parametrized API will erase your types and then the compiler is blind. With generics the type information is preserved and when I specify a parameter type, somewhere several API layers away a type is suddenly fixed, too. That gives me a warm feeling (and code completion, too). And since expressive static type systems seem to be the right way to go, allowing the type system better insight into your code is a big win.
Re:On Apple's Love Affair with Swift by saagarjha on Saturday August 11, 2018 @01:26PM (#17740618)
I don’t understand how you think this should be implemented. Many classes informally conform to protocols or in some cases the conformance can only be checked at runtime with a respondsToSelector: check. So any generics at compile time stricter than what we currently have will just not work or be useful.
Re:On Apple's Love Affair with Swift by valuearb on Saturday August 11, 2018 @06:46PM (#17741951)
Not useful as I’d rather find that mistake when a customer reports the crash, instead of having the compiler tell me?
Re:On Apple's Love Affair with Swift by saagarjha on Saturday August 11, 2018 @09:10PM (#17742411)
The point I'm making is that the compiler can't tell you this, since conventional Objective-C code does generics checking at runtime. There's just not enough information available for this to be reliably diagnosed.
Re:On Apple's Love Affair with Swift by slavapestov on Saturday August 11, 2018 @09:06PM (#17742392)
A fundamental problem with Objective-C generics that cannot be addressed in an additive manner is that generic parameters just desugar to ‘id’; you cannot use Objective-C generics with C types and you still pay the performance and memory use cost of trafficking exclusively in class instances. This of course was the right trade off, since it allowed generics to be layered on top of existing types like NSArray without either changing their implementation or breaking existing client code. However, when designing a new language with generics built in from the start you can make much better design decisions.
Re:On Apple's Love Affair with Swift by kitsunesoba on Saturday August 11, 2018 @05:01PM (#17741514)
That’s a real stretch. As someone who previously wrote Obj-C and currently writes Swift full time, whenever I revisit and work on old Obj-C code I constantly have “oh right, Obj-C doesn’t have that” moments and either have to pull in some third-party library that does what I need or settle for a less than satisfactory (and usually dramatically longer) solution.

I do have times when writing Swift where I miss some bit of Obj-C, but those are rare and are becoming fewer as the language develops.

Re:On Apple's Love Affair with Swift by pjmlp on Saturday August 11, 2018 @10:16AM (#17739754)
Other than safety, that is.
Re:On Apple's Love Affair with Swift by apple4ever on Saturday August 11, 2018 @03:53PM (#17741239)
Meh, I think that is overrated.
Re:On Apple's Love Affair with Swift by kitsunesoba on Saturday August 11, 2018 @05:10PM (#17741554)
It boils down to the self discipline of individual developers. If you’re meticulous in making sure you’ve dotted your I’s and crossed your T’s and haven’t let too many of those insidious cast statements slip in, you can write reasonably safe Obj-C, but during my career as an iOS dev I have yet to encounter an Obj-C codebase that was fully written by such developers. They’ve all been riddled with gratuitous casting, erroneous type assumptions, and other bad practices (that aren’t possible in Swift) to the point that it’s a wonder that they ever functioned as intended.

Personally though, I’ve found that even when I’m keeping a close guard on things when writing Obj-C and am sure to avoid all the obvious pitfalls, bugs slip through in the most unexpected places, so I’m quite happy to have the Swift compiler double checking everything for me.

Re:On Apple's Love Affair with Swift by valuearb on Saturday August 11, 2018 @06:49PM (#17741962)
I’ve never understood this opinion, and always wonder if it’s a professional developer espousing it. Because most of your time as a commercial developer is spent debugging, and Swift makes it far easier to write correct code, and to find bugs when you write incorrect code.
Re:On Apple's Love Affair with Swift by blub on Sunday August 12, 2018 @04:10AM (#17743468)
On a positive note, at least you're just writing buggy programs and not killing your patients somewhere or causing industrial accidents.

Unfortunately, as software continues disrupting various industries, it's less and leas certain that even buggy programs written by someone who thinks that safety is overrated will remain harmless.

Re:On Apple's Love Affair with Swift by pjmlp on Sunday August 12, 2018 @12:45PM (#17745172)
Only because software is a special snowflake where companies aren't held responsible for shitty software.
Re:On Apple's Love Affair with Swift by jmpt on Saturday August 11, 2018 @09:52AM (#17739655)
I'm glad for you, but everything you said is completely subjective.
Re:On Apple's Love Affair with Swift by IowaBoyInMN on Saturday August 11, 2018 @10:16AM (#17739753)
I believe the parent was subjective as well. I am curious as to why you appear to label just this comment. HN is for opinions as well as facts and I deeply value all of them. Particularly the opinions that politely challenge me.
Re:On Apple's Love Affair with Swift by flipgimble on Saturday August 11, 2018 @09:26AM (#17739584)
When people criticize C as a language they are usually referring to the fact that it easily and frequently allows for many classes of bugs that riddle C code. For decades, as an industry we've been apologizing and fixing critical exploits and bugs (ex HeartBleed), that can be traced to the fact that C allows for mistakes. If a bug is allowed, someone will perpetrate it sooner or later. Sacrificing correctness and safety for the speed and complicity of C is not a valid tradeoff for the industry in my opinion.

A major feature of Swift is that it forces you to deal with error conditions and missing values. In practice you do the right thing once and early in your code, and you can write the majority of your code with guarantees that prevent major classes of bugs. Swift is not perfect, still changing, and many complain about being forced to write safe code, but it is absolutely the right move of Apple to make for their platform.

Re:On Apple's Love Affair with Swift by kartan on Saturday August 11, 2018 @10:22PM (#17742607)
> If a bug is allowed, someone will perpetrate it sooner or later. Sacrificing correctness and safety for the speed and complicity of C is not a valid tradeoff for the industry in my opinion.

It depends on what you are building. For an app it is not a big deal to be slower. But you will not write a kernel in Swift as you will lose performance all over the place.

Python/Javascript/Lua are used as scripting languages to create video games. But the 3D engines are written in C or C++.

Each tool has its use.

Re:On Apple's Love Affair with Swift by pjmlp on Sunday August 12, 2018 @12:47PM (#17745179)
> But you will not write a kernel in Swift as you will lose performance all over the place.

That used to be the argument against C and C++ in the 80 and 90's regarding 16 bit home computers.

Any serious developer would know that Assembly was the only proper way to write software that required real performance.

Yet a couple of decades later.

> But the 3D engines are written in C or C++.

Re:On Apple's Love Affair with Swift by jmpt on Saturday August 11, 2018 @09:50AM (#17739647)
If Swift lets you override dealing with errors (think force unwrapping) then such security is pointless because "someone will perpetrate it sooner or later." In practice, what we get is an overly pedantic language that gets in the way of the developer.
Re:On Apple's Love Affair with Swift by pjmlp on Saturday August 11, 2018 @10:18AM (#17739761)
Not at all.

There is a world between every single string, array and numeric operation being a possible source of memory corruption and being forced to explicitly write unsafe code.

I have been using mostly safe languages since the 80's.

They only get in the way of cowboy programming, which is a very good thing for anyone that praises quality in software development.

C would no longer be around if companies got properly sued for each CVE exploit.

Re:On Apple's Love Affair with Swift by hedora on Saturday August 11, 2018 @01:55PM (#17740737)
People have written and continue to write secure C code, and insecure code in “safe” languages.

I have written systems in memory safe languages. The languages that provide memory safety also incur GC pauses, which means they’re unsuitable for applications where latency matters at all. (I’ve also noticed that programs written by professionals in memory safe languages tend to have many more serious high-level bugs than those written in C/C++, but I don’t have data to back that up.)

Re:On Apple's Love Affair with Swift by icebraining on Saturday August 11, 2018 @02:22PM (#17740880)
> The languages that provide memory safety also incur GC pauses

This is incorrect, you can have memory safety without a gc. An example is Ada, and more recently Rust.

Re:On Apple's Love Affair with Swift by comex on Saturday August 11, 2018 @06:56PM (#17741986)
And Swift, which uses automatic reference counting instead of GC. This is not a “zero-overhead” design like what Rust aims for; sometimes it has quite a lot of overhead. But it is free from the phenomenon of GC pauses.
Re:On Apple's Love Affair with Swift by pjmlp on Sunday August 12, 2018 @12:50PM (#17745189)
> But it is free from the phenomenon of GC pauses.

Not really. RC algorithms can lead to stop the world in cascade deletion of highly nested data structures, and even cause stack overflow due to destructors being called.

Re:On Apple's Love Affair with Swift by icebraining on Saturday August 11, 2018 @08:06PM (#17742226)
Well, in that regard, so is Python; the GC is only used to resolve cycles, and can be disabled.
Re:On Apple's Love Affair with Swift by com2kid on Saturday August 11, 2018 @04:17PM (#17741306)
IIRC memory safe Ada doesn't allow for deallocation.

Kinda sorta cheating. ;)

Re:On Apple's Love Affair with Swift by pjmlp on Sunday August 12, 2018 @12:51PM (#17745195)
It does via memory pools, RAII (aka controlled types) and dynamic stack allocation.
Re:On Apple's Love Affair with Swift by com2kid on Tuesday August 14, 2018 @01:50AM (#17755854)
It isn't truly safe like Rust, as Ada can still have dangling pointers laying around, the free function is called "Unchecked_Deallocation", not sure if newer Ada has any mechanisms for better ownership semantics.
Re:On Apple's Love Affair with Swift by baby on Saturday August 11, 2018 @05:03PM (#17741528)
> People have written and continue to write secure C code, and insecure code in “safe” languages.

No. It's the inverse. People who are very good with C continue to have vulnerabilities in their code. Why? Because C.

Re:On Apple's Love Affair with Swift by pjmlp on Saturday August 11, 2018 @02:21PM (#17740874)
Yeah, but I do have data to back up the fallacy of the perfect C programmer that writes secure C code.

https://www.cvedetails.com/vulnerability-list/opmemc-1/memor...

Apparently reviewing code before merging into mainline isn't of much help, but lets keep this myth of secure C code alive.

Σ (Logic errors in safe language) < Σ(Logic errors in C + Memory Corruption in C)

Re:On Apple's Love Affair with Swift by flipgimble on Saturday August 11, 2018 @11:33AM (#17740053)
The critical difference is that in swift your have to override safe behavior by using special syntax. This means you can search for it to fix it, you can have compiler/linters enforce certain rules, you can catch it easily in code reviews. No professional team I've worked on would allow forced unwrapped code except in special well documented cases.

The default code you write in swift is safe. You spend less time worrying about its correctness, which means you can focus on solving higher level problem and less time debugging.

When people call swift pedantic, for example, and I take time to ask for the specifics, I mostly get "not familiar to me", or "I'm used to shooting myself in the foot and unwilling to consider alternatives". So my patience has run out.

Re:On Apple's Love Affair with Swift by fauigerzigerk on Saturday August 11, 2018 @12:15PM (#17740251)
If only there was a way to catch array index out of bounds.
Re:On Apple's Love Affair with Swift by zoul on Saturday August 11, 2018 @02:14PM (#17740835)

  extension Collection {
    subscript (safe index: Index) -> Element? {
      return indices.contains(index) ? self[index] : nil
    }
  }
And now you have the choice:

  array[i] // traps for invalid indices
  array[safe: i] // returns nil for invalid indices
Re:On Apple's Love Affair with Swift by fauigerzigerk on Sunday August 12, 2018 @06:40AM (#17743827)
I always have a choice if I'm the one writing the code.

But what I want is a way to catch all errors that others may have introduced in code I have no control over. That includes arithmetic overflow as well.

Re:On Apple's Love Affair with Swift by zoul on Sunday August 12, 2018 @08:41AM (#17744103)
What would you do? If the ordinary array subscript produced an optional value, people would simply fall into the habit of force-unwrapping it, since the hassle of handling the spreading optionals would be too big. And throwing an unchecked exception would lead to empty catch blocks everywhere, just as in Java. And then the execution would continue in God only knows what state. I simply don’t think that the type system can solve the question of trusting your dependencies for you.
Re:On Apple's Love Affair with Swift by fauigerzigerk on Sunday August 12, 2018 @09:20AM (#17744233)
What I want to do is to wrap a piece of code running in its own thread in a catch block so I can respond gracefully to any errors without killing the entire process.

What gracefully means depends entirely on the application. Often it means to log the error or display a meaningful error message if it's an interactive app, then maybe do other kinds of cleanup and shut the thread down.

It can also mean to retry later. Array out of bounds errors are quite often just a result of not properly handling an earlier temporary error.

Whether or not errors leave the rest of the application in an inconsistent state depends entirely on the design of the application.

I'm not saying that letting an app crash on buggy code is never the right thing to do. I just want the option to handle it differently where appropriate and possible (it's not always possible anyway).

Re:On Apple's Love Affair with Swift by zoul on Sunday August 12, 2018 @09:26AM (#17744256)
You might be interested in this:

https://gist.github.com/lattner/31ed37682ef1576b16bca1432ea9...

Speaking of reliable systems, introducing an actor model is a good opportunity and excuse to introduce a mechanism for handling and partially recovering from runtime failures (like failed force-unwrap operations, out-of-bounds array accesses, etc). We explore several options that are possible to implement and make a recommendation that we think will be a good for UI and server applications.

This looks like a more principled approach that we will eventually get in Swift.

Re:On Apple's Love Affair with Swift by fauigerzigerk on Sunday August 12, 2018 @09:46AM (#17744321)
Sounds pretty vague, but at least he acknowledges the issue.
Re:On Apple's Love Affair with Swift by zoul on Sunday August 12, 2018 @10:08AM (#17744417)
Re:On Apple's Love Affair with Swift by fauigerzigerk on Sunday August 12, 2018 @10:30AM (#17744496)
So essentially they plan to terminate the actor instead of the whole process. I think that would be a very good thing.
Re:On Apple's Love Affair with Swift by valuearb on Sunday August 12, 2018 @02:06AM (#17743150)
Lol, I put that code in my last project, a 150,000 line codebase built by lazy contractors, as just one of the tools to clean up the errors from their objective c like approach to software quality.
Re:On Apple's Love Affair with Swift by blub on Sunday August 12, 2018 @03:43AM (#17743402)
Interestingly put, "the Objective-C approach to software quality". There's something in the psychology of a large number of C, Objective-C and C++ programmers that makes them overappreciate execution speed and underappreciate memory safety in particular, but also validation and correctness in general.

One of my goals now is to outline safe ways of using C++ for various projects and I'm not too optimistic. In fact I'm unsure what we'll end up with.

Re:On Apple's Love Affair with Swift by pjmlp on Sunday August 12, 2018 @12:56PM (#17745212)
Quite true, hence why I say that the problem is human and not technical.

Those of us that come from Wirth languages always put quality and delivering what the program is expected to do before going crazy with optimizations.

I always got the feel that many in the C communities write code as if they were micro-optimizing every single line, just because.

And those that care about type safe driven programming in C++ usually have background in safer languages.

Re:On Apple's Love Affair with Swift by fauigerzigerk on Sunday August 12, 2018 @06:52AM (#17743849)
I don't think Swift has a way to catch all runtime errors that others may have introduced in a reliable way.

Perhaps because it's not a huge problem in iOS apps, but for long running multi-threaded applications this is a problem.

Re:On Apple's Love Affair with Swift by scarface74 on Saturday August 11, 2018 @12:57PM (#17740450)
I don’t know Swift, so I don’t know what you’re implying, but I only know of three ways to deal with an array out of bounds runtime issue....

1. Don’t check for it at runtime and the runtime writes to memory that it shouldn’t like C.

2. Throw an exception and prevent memory corruption.

3. Don’t ever work with indexes and just use iterators.

Is there a fourth way that it can be handled in other languages?

Re:On Apple's Love Affair with Swift by saagarjha on Saturday August 11, 2018 @01:31PM (#17740632)
You could have the subscripting method check at runtime and return an Optional.
Re:On Apple's Love Affair with Swift by saagarjha on Saturday August 11, 2018 @01:30PM (#17740625)
This isn’t what Swift means when it says it’s safe. “Safe” doesn’t mean your program doesn’t have bugs or can’t crasg; rather, it will never have to deal with things like memory corruption and related undefined behavior. In fact, Swift considers crashing rather than allowing such errors to propagate to be an explicit design goal.
Re:On Apple's Love Affair with Swift by slavapestov on Saturday August 11, 2018 @08:51PM (#17742340)
Right; this is why integer operations trap on overflow by default, instead of silently wrapping, for example. Technically you’ll “crash” more but the crash occurs at the exact location of the problem and not much later in the form of corrupted data.
Re:On Apple's Love Affair with Swift by valuearb on Sunday August 12, 2018 @02:09AM (#17743157)
It’s the difference between a crash pointing exactly at the cause, and spending days or weeks isolating crashes that only occur minutes after the error that caused them, or only on certain devices, or in certain memory configurations, or only on every other Sunday, etc.
Re:On Apple's Love Affair with Swift by zoul on Saturday August 11, 2018 @09:52AM (#17739656)
There is a world of difference between letting bugs easily happen a letting the programmer override error handling.
Re:On Apple's Love Affair with Swift by Razengan on Saturday August 11, 2018 @10:59AM (#17739898)
Up till Swift 2 that was mostly the case – getting "in the way" – but as it is now I'd say Swift forces you to think about being a better developer, while still letting you be a bad one if you really insist on it.

After working exclusively in it for a while now I can't help but cringe at the practices which I see encouraged in most other languages.

Re:On Apple's Love Affair with Swift by gok on Saturday August 11, 2018 @08:53PM (#17742346)
I actually find Swift's C interop pretty extraordinary. Miles better than cgo, JNI, cffi/Cython/ctypes, etc. Parsing C headers, complete with comments and stuff, is super cool, and just about as slick as you can get without bringing in serious C cruft (like having its preprocessor). My biggest disappointment with Swift is that C++ doesn’t get the same treatment.
Re:On Apple's Love Affair with Swift by saagarjha on Saturday August 11, 2018 @09:18PM (#17742439)
C++ really is hard for any language to interop with. You basically have to add all of C++ to the language before it can work with C++.
Re:On Apple's Love Affair with Swift by gok on Saturday August 11, 2018 @09:20PM (#17742453)
Yeah I agree it wouldn’t be easy but damn would it be great. And like... given that much of the Swift Core team implemented C++ in clang, they’d be the ones to do it.
Re:On Apple's Love Affair with Swift by b3b0p on Saturday August 11, 2018 @05:15PM (#17741582)
I have some similar thoughts. I like Objective-C and C and always thought of Objective-C as an improved upon iteration of C. The C/C++ integration is one of my favorite parts.

Also, if Swift is so much better and is supposed to be safer to use why are apps much slower and buggier than I ever remember? My iOS devices went from instant in iPhone 1.0 to pathetic and almost not useable at times on an iPhone 6 Plus, which would still be an awesome phone for my uses (using it as an iPod essentially and the occasional call) except it's slow and buggy. Even answering the phone is no longer instant. I could have sworn I remember when Jobs introduced the iPhone he was bragging how instant everything was. What would he say now and why are apps much less stable if Swift is supposedly that much of a benefit and greater than Objective-C

Re:On Apple's Love Affair with Swift by saagarjha on Saturday August 11, 2018 @09:24PM (#17742468)
This is entirely unrelated to Swift. But to address our concern: your iPhone 6 Plus is the slowest iPhone still getting updates today. Sure, it should perform better, and the iOS 12 update later this year will help fix this issue, but Swift has nothing to do with making your phone slow.
Re:On Apple's Love Affair with Swift by MR4D on Sunday August 12, 2018 @10:55PM (#17747744)
I upgraded to the iOS 12 beta a week or so ago on my 6S+. I can say without any doubt that it is so much faster that I feel like I have a new phone in my hand. I honestly never thought an upgrade could make this much difference.
Re:On Apple's Love Affair with Swift by rimliu on Saturday August 11, 2018 @07:47PM (#17742151)
Not sure what apps you are talking about, but not many core apps have been rewritten in Swift.
Re:On Apple's Love Affair with Swift by Volt on Saturday August 11, 2018 @08:58PM (#17742361)
That's just plain software bloat, which continues to plague everything ever.
Re:On Apple's Love Affair with Swift by cheez on Saturday August 11, 2018 @12:48PM (#17740415)
That's because a lot of the people involved in C++ are now involved in Swift.
Re:On Apple's Love Affair with Swift by patrickg_zill on Saturday August 11, 2018 @09:54AM (#17739662)
Funnily enough, NeXT never ran on 16MHz cpus! The original machine was a 25 MHz 68030,followed by 68040 running at 25 and 33 MHz.
Re:On Apple's Love Affair with Swift by masklinn on Saturday August 11, 2018 @11:44AM (#17740112)
NeXT did not, but Objective-C was not designed by or for NeXT, it was licensed for it years later.
Re:On Apple's Love Affair with Swift by tinus_hn on Sunday August 12, 2018 @08:04AM (#17743999)
If you need C in your Swift program you can just put it in a separate library.

I think the idea is to make common things easy and uncommon things possible.

Re:On Apple's Love Affair with Swift by lmm on Saturday August 11, 2018 @09:01AM (#17739511)
Those 16MHz processors were two or more orders of magnitude slower than anything anyone's running Objective-C on these days. Is there really a need for C any more?
Re:On Apple's Love Affair with Swift by NeedMoreTea on Saturday August 11, 2018 @10:56AM (#17739888)
When compiling or video encoding I'll take a current processor. For everything else, not so much.

For speed of UI response, loading times, cursor and input latency we're at best no further forward. Quite often we're in an objectively worse situation. With a tiny few exceptions, no one cares about code or data sizes any more and that is reflected in what's delivered.

That's quite apart from the rise of web apps and the slowness that necessarily introduces.

Re:On Apple's Love Affair with Swift by willvarfar on Saturday August 11, 2018 @09:12AM (#17739542)
I recall discussing the spec of an upcoming phone platform in about 2007 or so. It claimed to be 4x faster than the previous generation and there was much rejoicing. And at the same time the screen went from QVGA to a widescreen that had 5x as many pixels... And the camera from 2MP to 8 or was it 12MP. In practice, from a usability perspective, the new platform was slightly slower than the previous platform :(
Re:On Apple's Love Affair with Swift by ziotom78 on Saturday August 11, 2018 @09:11AM (#17739537)
This is true, but consider that today we are using computers fo to run tasks much more complex than 30 years ago. Rendering a complex HTML page with CSS and Javascript is an herculean and time-consuming task, which is why Firefox and Chrome are written in C++; and don't forget deep learning!

Plus, low-level languages like C are sometimes the only way to go if you are writing for embedded systems with slow CPUs.

Re:On Apple's Love Affair with Swift by acdha on Saturday August 11, 2018 @11:06AM (#17739920)
You don’t even need higher level tasks like that: take something as seemingly simple as processing text. That used to be 8 byte operations, display was looking up a bitmap and copying it into a buffer, but once programmers accepted that humans use more than ASCII and want nice displays that started to involve context sensitive decoding and display, processing vectors, subpixel rendering, etc. All great improvements but most of them require more CPU/RAM than a 30 year old computer had, too.
Re:On Apple's Love Affair with Swift by pjmlp on Saturday August 11, 2018 @03:34PM (#17741157)
Only if one doesn't look into the right place.

Mikroelektronika has Basic and Pascal compilers, in addition to C, for all those slow CPUs that are market relevant.

https://www.mikroe.com/compilers

And they aren't the only ones still alive.

Re:On Apple's Love Affair with Swift by bluejekyll on Saturday August 11, 2018 @09:25AM (#17739580)
> Firefox and Chrome are written in C++;

Firefox is on a course to remove C++ from safety critical areas and replace those with Rust.

Edit: to be clear this is based on the blog posts I’ve read from Mozilla about the introduction of Rust in different areas of Firefox. I know nothing of firm plans.

Re:On Apple's Love Affair with Swift by otabdeveloper2 on Saturday August 11, 2018 @09:29AM (#17739597)
'On a course' implies that there's some sort of definite plan and resources delegated to the task. Which isn't true of Firefox and Rust, as far as I know.
Re:On Apple's Love Affair with Swift by bluejekyll on Saturday August 11, 2018 @09:50AM (#17739648)
While it may not be an explicit goal, everything I’ve read implies that, for example the heading in this wiki on oxidization: https://wiki.mozilla.org/Oxidation

> Moving forward, the goal of Oxidation is to make it easier and more pleasant to use Rust in Firefox, and correspondingly to increase the amount of Rust code in Firefox.

And then there is the Rust page on Mozilla’s site:

> Mozilla also utilizes Rust in many of its core initiatives including Servo and key parts of Firefox.

https://research.mozilla.org/rust/

Where “key parts” is a link to the project quantum announcement: https://medium.com/mozilla-tech/a-quantum-leap-for-the-web-a...

> Initially, Quantum will share a couple of components with Servo, but as the projects evolve we will experiment with adopting even more.

Re:On Apple's Love Affair with Swift by gsnedders on Saturday August 11, 2018 @01:22PM (#17740597)
There is, however, no expectation or any real plan that C++ will be gone from Firefox. There's a huge amount of code, and there's a cost to rewriting it.
Re:On Apple's Love Affair with Swift by bluejekyll on Saturday August 11, 2018 @03:30PM (#17741146)
I only claimed “safety critical areas”... the other millions of lines and OS interop for UI, etc., will most likely not change for a long time.