Live data from Hacker News

Swift Regrets

belkadan.com

201–207 of 207 posts

Re: Swift Regrets

#201
post #162
post #156

Earlier quoted context omitted.

The question mark syntax just moves this burden to some other layer of abstraction.

I disagree. Knowing if something could be nil is a very different thing than something unexpectedly becoming nil. If you have values that are guaranteed by the compiler to never be nil, that removes the burden completely.

> that removes the burden completely.

In practice this gives you improperly constructed objects with bad state, returned from code some random person wrote in a hurry. Yes, it is bad code but so is most code in existence you have to interface with.

Re: Swift Regrets

#202
post #45

Earlier quoted context omitted.

I code several times faster in ObjC than Swift, and this is after writing two released Swift apps. Just a simple thing like writing string to num to string that works in different bases is a nightmare in Swift.

Hope this eases your nightmares: String( Int("ff", radix: 16), radix: 10)

Actually that simplistic approach crashes if the value of the string goes out of range or the string contains invalid characters for the base. For commercial quality code dealing with user input you need to do your own checking char by char and assemble the result using multipliedReportingOverflow and addingReportingOverflow. The char by char processing is a pain given you can't really take the value of a Character the way you can in other languages and end up having to feed them through a map. In the end my correct bulletproof implementation is only about a page of code, but it was awkward to write.

Re: Swift Regrets

#203

Earlier quoted context omitted.

The article is only for demonstrating how standard library works (it was the first Google result for me). The point is we shouldn't need to convert a string to an array of characters to manipulate it.

> The point is we shouldn't need to convert a string to an array Yes we should. Unicode strings have ambiguous lengths. I would encourage you to try slicing Unicode strings sometime to understand why this is true. Consider the black Santa emoji. What is the length of that emoji? Backspacing an emoji deletes it, so it could be one. But then how would you substring slice out the skin color component of the emoji? Conve…

This is a typical example of overengineering.

When one speaks about "string slicing" they don't mean stripping the skin colour of an emoji. If there is a black Santa emoji in a string, I want to keep that Santa black. I am not interested whether it's a composite emoji or not.

The simple syntax should cover 99% of use cases and there should be a special, more advanced syntax for the rest, one that you'd use if you are writing an Emoji parser.

Re: Swift Regrets

#204
post #202

Earlier quoted context omitted.

Hope this eases your nightmares: String( Int("ff", radix: 16), radix: 10)

Actually that simplistic approach crashes if the value of the string goes out of range or the string contains invalid characters for the base. For commercial quality code dealing with user input you need to do your own checking char by char and assemble the result using multipliedReportingOverflow and addingReportingOverflow. The char by char processing is a pain given you can't really take the value of a Character t…

You yourself said "Just a simple thing like...", so I thought you were looking for a simple solution. Your complaints are no different if you were using strtol/atoi, so what's unique about swift here? Are you expecting "value of character" to be in some range that's not within 0..I have also written commercial code that does this, and I'm just going to paste it here because it's actually not that much code. Sums two strings of arbitrary length, well outside of the overflow limit, for the given base. Returns the sum as string in the base given. No mapping or overflow checks needed. Invalid characters for the base are treated as 0, but you can easily modify it to throw an error or whatever.

   func sumStrings(_ left: String, _ right: String, _ radix: Int) -> String {
        var left = Array(left)
        var right = Array(right)
        var carry = 0
        var result: String = ""

        while left.isEmpty == false || right.isEmpty == false {
            let charLeft = left.popLast() ?? "0"
            let charRight = right.popLast() ?? "0"
            let intLeft = Int("\(charLeft)", radix: radix) ?? 0
            let intRight = Int("\(charRight)", radix: radix) ?? 0

            carry += (intLeft + intRight)
            result = String(carry % radix, radix: radix, uppercase: false) + result
            carry /= radix
        }
        if carry % radix != 0 {
            result = String(carry % radix, radix: radix, uppercase: false) + result
        }
        return result
    }

Re: Swift Regrets

#205

Earlier quoted context omitted.

> The point is we shouldn't need to convert a string to an array Yes we should. Unicode strings have ambiguous lengths. I would encourage you to try slicing Unicode strings sometime to understand why this is true. Consider the black Santa emoji. What is the length of that emoji? Backspacing an emoji deletes it, so it could be one. But then how would you substring slice out the skin color component of the emoji? Conve…

This is a typical example of overengineering. When one speaks about "string slicing" they don't mean stripping the skin colour of an emoji. If there is a black Santa emoji in a string, I want to keep that Santa black. I am not interested whether it's a composite emoji or not. The simple syntax should cover 99% of use cases and there should be a special, more advanced syntax for the rest, one that you'd use if you are…

No, it's not overengineering. It's working with unicode. You have to let go of the idea that a string is a sequence of characters separated by where your cursor pauses. It's not. Black Santa was a simple metaphor for more real-world scenarios such the chinese language, in which multiple characters form to create a new character.

Imagine if you had a Color key on your keyboard, and a Santa key on your keyboard. Each key individually has meaning. When the two characters are positioned adjacent to each other, you get SkinColorSanta as one single character.

What is the new length of your string? Two for each original Color and Santa characters? Or 1 because there is only one icon the represents the two together? Remember Swift interops with C, so what is the size when you need to drop to char*? Hope you don't plan to use 1!

How would you solve this problem? Swift is open source. Make a PR if you have a better solution.

Re: Swift Regrets

#206

Earlier quoted context omitted.

This is a typical example of overengineering. When one speaks about "string slicing" they don't mean stripping the skin colour of an emoji. If there is a black Santa emoji in a string, I want to keep that Santa black. I am not interested whether it's a composite emoji or not. The simple syntax should cover 99% of use cases and there should be a special, more advanced syntax for the rest, one that you'd use if you are…

No, it's not overengineering. It's working with unicode. You have to let go of the idea that a string is a sequence of characters separated by where your cursor pauses. It's not. Black Santa was a simple metaphor for more real-world scenarios such the chinese language, in which multiple characters form to create a new character. Imagine if you had a Color key on your keyboard, and a Santa key on your keyboard. Each k…

I'm not sure if we are on the same page. I am simply talking about syntactic sugar, not semantics.

To restate, I still don't understand why you think

    str.dropFirst(1).dropLast(2)
handles unicode better than

    str[1:-2]
What does dropFirst(1) drop if the first _element_ is a Black Santa? Why can't whatever it does be implemented using the bracket syntax?

Re: Swift Regrets

#207
post #117

Earlier quoted context omitted.

...and of course it was simple to interoperate with Objective-C in C++, in a way that it isn't for Swift, sadly

Which is super annoying when you have an engine written in C++, perhaps that runs on multiple platforms, and want to run it with a GUI under macOS.

You can use Scapix Language Bridge to automatically generate bindings for various languages directly from C++ headers:

https://github.com/scapix-com/scapix

Disclaimer: I am the author of Scapix Language Bridge.

Post reply on HN