Treehouse Intermediate Course - Part 9 (Generics - Consolidation!)

So two things... I am ending up doing another entry today and I will be consolidating generics after all! I have a little bit of time so just a good half hour or so on summarising generics then doing a little extra practice, based on what I can find!

Start Time - 20:06

Reviewing Treehouse Course

Wow there was a lot to this course! By 'review' I mean to go through again, rather than critique. First off, I'm just going to skim through each of my previous six entries and do a summary based on those.

Writing Repetitive Code

When I started the generics course, I thought this would be the use of any - down casting and upcasting as appropriate. Not really at all!

Example of swapping values. Had to use inout keyword in the parameter/argument labels part as that is the only way of making them variables! The point with this whole bit is that it is limited with one type, or you make a function for each type - intSwap, stringSwap etc. Use of generics comes in here to use the <T> so that any type can be used!

The reason just using Any as the type won't work is due to type safety. Basically it means that someone could try using an int with a string to swap and the compiler would crash.

Writing Generic Functions

Yes, here is where the <T> is introduced. The T is another way of 'type'. It is convention to start with T, then U etc. Unless a placeholder name is used instead e.g. <Value> etc.

Multiple Parameters

Getting more technical now. The use of <T, U> as per the convention above. Another key point gleaned is that other types are actually generics! By that, I mean e.g. dictionary has <key, element> and array has the same type for the items in the array... once declared of course!

Paused at 20:17

Continued at 20:26

Protocol Based Type Constraints

This is where it gets REALLY hard! The protocol based bit means a specific protocol is put in the argument list. It's a constraint as it's something to be followed. E.g. the use of 'equatable' is a protocol that needs to then be followed. This then allows the use of == and != in custom types. Custom types do NOT have these already built in! So that makes sense - that Int, String etc. do in the code 'behind the hood'. There are other protocols - hashable and comparable.

Class Based Type Constraints

OK, so as the title suggests, this is e.g. a function having a custom type in the parameters. But not just that, having a generic type conforming to a class! Inheritance will work with this - with subclasses used.

Right, then we're back to protocol constraints - use of comparable.

Multiple Type Parameters

Use of a function in the parameters...that is confusing! In the example, it is 'operation' that is the function used. That is the second parameter in that particular case. So the function in the parameter list is general in the sense the generic types used! Yes this makes sense - it can be T and U. Then, we have another function made, which follows the same syntax of having one type go in and a different one come out, be passed through the original function. Confusing but the concept makes sense.

Generic Types in the Standard Library

This was a previous point in how certain types are generic - arrays, dictionaries and optionals specifically. They al have certain constraints but it is more about the types which have more code going on under the hood. So an optional is a public enum. Public so it is accessed by all, all the time. The cases for optional are some, none...you don't ever need to code that! But the code had to be created for this type in the first place.

Linked Lists

Wow I had almost forgot this! Different to an array. The items are connected and called nodes. Looking at it now, I see that Pasan's examples of classes were custom created linked lists.

Linked Lists Operations

This was all beyond difficult. The use of type alias came in here - another word for something to then be referred to for readability. But also a generic type included.

Within the challenges, the use of 'mutating' keyword for functions that will change the initial value.

Associated Types

So this is to do with a custom made protocol. Then a custom struct following that protocol. Use of associatedtype keyword - not entirely sure why this is used and the advantage.

Constraints with Where Clauses

So for combining a class with a protocol - certain syntax used.

Constraining Associated Types

So an advantage of using ATs is to avoid using inheritance, which can lead to further complications!
Another general point - it is best to avoid Any or AnyObject, as those types lose all of their type specificity!

Generics and Associated Types

Well this was especially hard to follow! Don't really get the point of it all. From what I can see, there are protocols with associated types in the code. Then classes created, conforming to these custom protocols but generic type syntax used for that.

Extending Generic Types

So I know about extensions in general - how they can add functionality to an already created type. Extending a custom struct is what Pasan does for his example. Then the extension is used with this struct to conform to a custom protocol. And another type - an built in type - Array. This uses the conforming to that same custom protocol. Again, this is all so tricky that I'm just making a note of it for now!

Finish Time - 21:19 (1 hour 4 minutes total)

More than planned! But all worth going over to be honest. Just so I know what I've covered at least. Tomorrow will be closures!



Comments

Popular posts from this blog

*Xcode Project Entry 2* F1 Quiz - part 1

Angela Yu Course Part 10 (up to lesson 112)

Angela Yu Xcode 12 Course - Part 7 (lectures 74 to 79)