Archive for February 6, 2024

Tuesday, February 6, 2024

Swift Tricks

André Jorgensen:

Generic typealias can be used to simplify param types etc

typealias Parser<A> = (String) -> [(A, String)]

[…]

func parse<A>(stringToParse: String, parser: Parser)

[…]

Finding Elements of Specific Type in Swift

extension Array {
    func whereType<T>() -> [T] {
        compactMap { $0 as? T } // The function "compactMap(" in Swift is incredibly useful. It maps each element of an array to another optional type and returns the value if it exists (is not null).
    }
}

I’ve been using a version of this where the type is passed as a parameter. With this version it’s determined using type inference from the call site.

He currently has 202 other tricks listed.

Wade Tregaskis:

for case let rep as NSBitmapImageRep in image.representations {
    … // `rep` is an NSBitmapImageRep.  Non-bitmap reps are skipped.
}

I sometimes forget that this is possible (and even more often exactly what the damn syntax is – kudos to vacawama in today’s case of this for reminding me with their StackOverflow answer). There are numerous other ways to write the above, but I think it is the most elegant.

I’ve been writing Swift for almost 10 years and still have to think to remember this syntax, as well as if case let. I know that case is for pattern matching, but it still looks weird to see it there, and this is the same reason it’s written using as without the question mark that normally accompanies downcasts that might fail. You might expect to be able to write:

for rep as? NSBitmapImageRep in image.representations {

but instead Swift gives us case let and a more general pattern matching feature. If you write this or get it correct except for the ?, the compiler’s error messages are unhelpful:

for rep as? NSBitmapImageRep in image.representations {
// Expected 'in' after for-each pattern

for case let rep as? NSBitmapImageRep in image.representations {
// Pattern variable binding cannot appear in an expression

Previously:

Pkl Programming Language

Apple (via Hacker News):

We are delighted to announce the open source first release of Pkl (pronounced Pickle), a programming language for producing configuration.

[…]

We created Pkl because we think that configuration is best expressed as a blend between a static language and a general-purpose programming language. We want to take the best of both worlds; to provide a language that is declarative and simple to read and write, but enhanced with capabilities borrowed from general-purpose languages. When writing Pkl, you are able to use the language features you’d expect, like classes, functions, conditionals, and loops. You can build abstraction layers, and share code by creating packages and publishing them. Most importantly, you can use Pkl to meet many different types of configuration needs. It can be used to produce static configuration files in any format, or be embedded as a library into another application runtime.

[…]

When binding to a language, Pkl schema can be generated as classes/structs in the target language. For example, the Application.pkl example from above can be generated into Swift, Go, Java, and Kotlin.

Daniel Jalkut:

Who had “Apple will release a new language, implemented in Kotlin, with IDE integration for everything but Xcode” on their bingo card?

_mischi:

Vision Pro is cool and all, but have you ever spent time searching for that format error in your YAML file?

Joe Heck:

The choice of package/module configuration IN swift was a decision I wish had been revisited back prior to Swift3 - so many ongoing and upgrading complications from that alone.

Teatotaller Cafe v. Instagram

Annie Ropeik (2020):

The owner of the Teatotaller café in Somersworth is taking on Facebook at the New Hampshire Supreme Court.

[…]

Owner Emmett Soldati markets them all on Instagram, which is owned by Facebook. He says it was a blow to his business when, in 2018, Teatotaller’s Instagram account – with more than 2,000 followers – was shut down without warning.

“We had spent money advertising on their platform to do many things, including building a following, and we lost that following,” Soldati said in an interview at Teatotaller Sunday.

Facebook’s terms and conditions for Instagram limit users’ legal recourse, but say they can pursue a case in small claims court. Soldati did that, in Dover, arguing the platform was negligent in deleting his account and asking for it to be restored.

Margie Cullen (via Hacker News):

When Emmett Soldati first noticed the Instagram account for his small cafe Teatotaller was deleted, he had no idea the battle to get accountability from Facebook would take six years.

But Soldati, who represented himself in court, has finally won his small claim against the social media giant now known as Meta.

Duncan Shaw:

The parent company of Instagram was ordered to pay a judgment of $100 plus court costs and interest to the owner of the Teatotaller Café after his account was deleted.

It’s still not clear why the account was deleted in the first place.

Dan Luu:

Are there fundamental reasons that a company the size of FB can’t provide much better support than they do?

The most common explanation I’ve heard is that support is impossible due to cost, but I don’t find this plausible based the profit FB-sized companies make per user. If you just naively look at how many support people they could pay, it’s quite a lot, not including things like diverting money from the ~$50B that’s allegedly been spent on the metaverse.

[…]

I know of kafkaesque horror stories of bank and brokerage account loss, so it’s not like brokerages are perfect, but it’s rare enough that I don’t personally know anyone who’s had their personal account or funds temporarily lost, let alone permanently, whereas with FB, a large fraction of my non-tech friends have lost accounts.

treeman79:

My wife and most of her friends have all lost their Facebook accounts at least once. They all gave up getting them back. Many tears as most of them use it as their only photo backup for kid pictures.

At this point it's just routine for them to have their account taken over and lost periodically.

cGilmore:

As someone who’s both permanently lost a [iTunes] previous account, losing thousands of $$$ in purchases, and who’s recently yet again temporarily lost access to purchased content—both of which were due to an issue with Apple’s services—I will never, EVER buy music, movies, or TV shows from Apple ever again.

Chris Wanstrath, GitHub co-founder (via Hacker News):

Banned from GitHub without any explanation. Guess I’m moving all my code to BitBucket.

Previously:

Ruling in Vizio Lawsuit May Strengthen the GPL

Luis Villa (via Hacker News):

In October of 2021 the Software Freedom Conservancy (SFC) decided to launch what is believed to be the first significant open source lawsuit based in contract rather than in copyright. Critically, the SFC’s case argued that anyone who benefits from the General Public License (GPL), not just the authors of the software, should be able to bring a lawsuit to enforce the terms of the GPL.

This case was brought in Orange County, California against Vizio, a large TV manufacturer. Like most TVs these days, Vizio TVs include Linux and a lot of other open source software that is under the GPL. The GPL says that buyers of those TVs should be able to get copies of that source code, so SFC walked into a store in Orange County, bought a TV, and requested copies of the source code. Vizio did not comply with the request, and so SFC brought suit.

[…]

The short version is that, by asking for specific performance (a contract remedy) rather than financial penalties (a copyright remedy), and by claiming violations of rights granted by the contract (the license) rather than rights granted by copyright, the federal court found that this was a contract case and not a copyright case.)

Previously: