Stephen A. Davis's Avatar

Stephen A. Davis

@webdavis.bsky.social

Hi, Iโ€™m Stephen ๐Ÿ‘‹. I like to talk about software architecture and test-driven development ๐Ÿงช Check out my work on https://github.com/webdavis/webdavis

24 Followers  |  31 Following  |  60 Posts  |  Joined: 25.10.2024  |  2.0553

Latest posts by webdavis.bsky.social on Bluesky

How are you liking it so far? (Thatโ€™s the resource I am considering for getting started with SwiftUI)

16.02.2025 00:11 โ€” ๐Ÿ‘ 1    ๐Ÿ” 0    ๐Ÿ’ฌ 1    ๐Ÿ“Œ 0

Dope! Any hot takes from the lesson?

09.02.2025 18:43 โ€” ๐Ÿ‘ 0    ๐Ÿ” 0    ๐Ÿ’ฌ 0    ๐Ÿ“Œ 0

I stand with the trans community. Pass it along.

09.02.2025 12:45 โ€” ๐Ÿ‘ 28738    ๐Ÿ” 7123    ๐Ÿ’ฌ 526    ๐Ÿ“Œ 524

This took me forever to wrap my head around and it all boiled down to my lack of understanding of the Swift language.

The more you know!

28.01.2025 01:50 โ€” ๐Ÿ‘ 1    ๐Ÿ” 0    ๐Ÿ’ฌ 0    ๐Ÿ“Œ 0
Post image

Now here's an enum *without* associated value types.

Since there are no associated value types the compiler says we can compare instances of this Error type and slaps on the Equatable protocol at compile time.

28.01.2025 01:50 โ€” ๐Ÿ‘ 0    ๐Ÿ” 0    ๐Ÿ’ฌ 1    ๐Ÿ“Œ 0
Post image

As it turns out, in Swift, enums with no associated values get free Equatable synthesis!

Here is an enum *with* associated value types:

28.01.2025 01:50 โ€” ๐Ÿ‘ 0    ๐Ÿ” 0    ๐Ÿ’ฌ 1    ๐Ÿ“Œ 0

So what the heck was going on?

Somehow, the compiler is saying that RemoteFeedLoader.Error conforms to Equatable.

This led me to believe that the compiler was auto-synthesizing RemoteFeedLoader.Error's conformance to Equatable.

But what conditions need to be met in order for that to happen?

28.01.2025 01:50 โ€” ๐Ÿ‘ 0    ๐Ÿ” 0    ๐Ÿ’ฌ 1    ๐Ÿ“Œ 0
Post image

We could explicitly conform RemoteFeedLoader.Error to Equatable like so, but apparently we don't need to? ๐Ÿค”

28.01.2025 01:50 โ€” ๐Ÿ‘ 0    ๐Ÿ” 0    ๐Ÿ’ฌ 1    ๐Ÿ“Œ 0
Post image

Here's the FeedLoader protocol's implementation class, RemoteFeedLoader.

No where in this code did we explicitly conform the Error type to Equatable:

28.01.2025 01:50 โ€” ๐Ÿ‘ 0    ๐Ÿ” 0    ๐Ÿ’ฌ 1    ๐Ÿ“Œ 0

On the surface it makes sense: LoadFeedResult is Equatable if the Error in the failure case is Equatable.

This means that the protocol's associatedtype Error need to conform to Equatable, which means that whatever class implements the protocol need to have an Equatable Error type.

28.01.2025 01:50 โ€” ๐Ÿ‘ 0    ๐Ÿ” 0    ๐Ÿ’ฌ 1    ๐Ÿ“Œ 0

If we uncomment the conditional conformance (the commented out line above), then the compiler error goes away ๐Ÿซฐ, and we can compare LoadFeedResult types using XCTAssertEqual.

28.01.2025 01:50 โ€” ๐Ÿ‘ 0    ๐Ÿ” 0    ๐Ÿ’ฌ 1    ๐Ÿ“Œ 0
Post image Post image

The puzzle ๐Ÿงฉ:

Whenever I commented out the following line in the interface boundary, I would then get the accompanying compile-time error in the test code:

28.01.2025 01:50 โ€” ๐Ÿ‘ 0    ๐Ÿ” 0    ๐Ÿ’ฌ 1    ๐Ÿ“Œ 0

Day 6 of #100DaysOfCode

Log: github.com/webdavis/100...

Understanding the Swift compiler!

Last week I was struggling to understand how the Swift compiler was auto-synthesizing conformance to Equatable for an enum result type.

#code #swift #learning #compiler

28.01.2025 01:50 โ€” ๐Ÿ‘ 3    ๐Ÿ” 0    ๐Ÿ’ฌ 1    ๐Ÿ“Œ 0

No worries haha, still fun ๐Ÿ‘

25.01.2025 02:39 โ€” ๐Ÿ‘ 1    ๐Ÿ” 0    ๐Ÿ’ฌ 0    ๐Ÿ“Œ 0

This is really interesting and reminds me how developers go from user stories to use cases. Seems every domain has its techniques for dialing in. Great stuff!

25.01.2025 02:38 โ€” ๐Ÿ‘ 1    ๐Ÿ” 0    ๐Ÿ’ฌ 1    ๐Ÿ“Œ 0
Post image

๐Ÿ˜† I wonโ€™t be passing any Ophthalmology tests, but I enjoyed the UI! Great work!

21.01.2025 16:30 โ€” ๐Ÿ‘ 1    ๐Ÿ” 0    ๐Ÿ’ฌ 1    ๐Ÿ“Œ 0

1. What testing framework I should use.

2. If itโ€™s is a class-per-test framework (e.g. the JUnit approach) or not.

3. If the language supports interfaces/protocols. If not, then what does it use to achieve composition?

19.01.2025 23:04 โ€” ๐Ÿ‘ 2    ๐Ÿ” 0    ๐Ÿ’ฌ 0    ๐Ÿ“Œ 0

Taking a break todayโ€”my sleep debt is piling up ๐Ÿ˜ตโ€๐Ÿ’ซ ๐Ÿ“ˆ

However, I've found these posts really improve my understanding of things.

That being said, I want to get better at TDD. Currently, I donโ€™t โ€œthink itโ€ as well as I would like to, but these posts could help me change that.

Maybe that's the move.

18.01.2025 04:27 โ€” ๐Ÿ‘ 0    ๐Ÿ” 0    ๐Ÿ’ฌ 0    ๐Ÿ“Œ 0

This is some next level nerdom. Bravo ๐Ÿ‘

17.01.2025 06:04 โ€” ๐Ÿ‘ 1    ๐Ÿ” 0    ๐Ÿ’ฌ 0    ๐Ÿ“Œ 0

Argghh!

I said all this and realized that I didn't make it very clear that I am referring specifically to the "try?" syntax.

You can see it in the first code snippet as follows:

let root = try? JSONDecoder().decode(Root.self, from: data)

(I'M TIRED lol)

17.01.2025 05:58 โ€” ๐Ÿ‘ 0    ๐Ÿ” 0    ๐Ÿ’ฌ 0    ๐Ÿ“Œ 0

Lolz ๐Ÿ˜‚

17.01.2025 05:54 โ€” ๐Ÿ‘ 1    ๐Ÿ” 0    ๐Ÿ’ฌ 0    ๐Ÿ“Œ 0

Seems kinda convoluted! Also, now we have to force unwrap root.

Maybe that's why the Swift team implemented this.

(Side note: I usually hate reading documentation, but the Swift team actually does a really good job at it.)

(7/7)

17.01.2025 05:53 โ€” ๐Ÿ‘ 0    ๐Ÿ” 0    ๐Ÿ’ฌ 1    ๐Ÿ“Œ 0
Post image

(6/7)

Now let's see what it would be like to implement the same behavior without this syntax. We could convert this to a do-catch block, like so:

17.01.2025 05:53 โ€” ๐Ÿ‘ 0    ๐Ÿ” 0    ๐Ÿ’ฌ 1    ๐Ÿ“Œ 0

Here is what the Apple docs say about it:

"You use try? to handle an error by converting it to an optional value. If an error is thrown while evaluating the try? expression, the value of the expression is nil."

(5/7)

17.01.2025 05:53 โ€” ๐Ÿ‘ 0    ๐Ÿ” 0    ๐Ÿ’ฌ 1    ๐Ÿ“Œ 0

This is because Swift allows us to convert errors to optional values.

In this case we would rather throw our own domain-specific error. We don't care about the DecodingError thrown by decode().

So instead, if decode throws an error we instruct Swift to convert it to an optional value `nil`.

(4/7)

17.01.2025 05:53 โ€” ๐Ÿ‘ 0    ๐Ÿ” 0    ๐Ÿ’ฌ 1    ๐Ÿ“Œ 0
Post image


(3/7)

Take a look at the following static function. Why isn't there a do-catch block to catch the potential error that JSONDecoder().decode throws?

17.01.2025 05:53 โ€” ๐Ÿ‘ 0    ๐Ÿ” 0    ๐Ÿ’ฌ 1    ๐Ÿ“Œ 0

Converting an error to an optional value is a great way to handle situations where we want to throw a domain-specific error in favor of a third-party error that we don't control (e.g. DecodingError thrown by JSONDecoder().decode).

(2/7)

17.01.2025 05:53 โ€” ๐Ÿ‘ 0    ๐Ÿ” 0    ๐Ÿ’ฌ 1    ๐Ÿ“Œ 0

Day 5 of #100DaysOfCode

Learned about Swift.Error, including error handling in Swift, as well as generics.

Today I'll focus a specific case:

โŒโ“ Handling Errors by Converting them to Optional Values

Let's get to it!

(1/7)

17.01.2025 05:53 โ€” ๐Ÿ‘ 4    ๐Ÿ” 0    ๐Ÿ’ฌ 1    ๐Ÿ“Œ 0

Do you have any favorites from the freecodecamp podcast that you recommend?

17.01.2025 05:14 โ€” ๐Ÿ‘ 1    ๐Ÿ” 0    ๐Ÿ’ฌ 0    ๐Ÿ“Œ 0

Hey Zero,

Thanks for connecting! I followed you back ๐Ÿฅณ

I totally get itโ€”these topics are tricky for me too! Thatโ€™s why I post about them; rubber ducking helps me process.

If somethingโ€™s unclear, it might mean I havenโ€™t explained it well. Feel free to ask or point it out! Letโ€™s learn together ๐Ÿ™‚

16.01.2025 19:39 โ€” ๐Ÿ‘ 0    ๐Ÿ” 0    ๐Ÿ’ฌ 0    ๐Ÿ“Œ 0

@webdavis is following 20 prominent accounts