Golang Generics. Do we ACTUALLY need them!?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] golang and genetics now let's say that we have two integers here x and y and we want to compare the values and return if x is greater than y well this is easy right just write a simple function and call it the day now let's say that additionally we have two floating point numbers and we want to perform the exact same calculation and see if one is greater than the other easy again we add another function just change up the parameter type a little bit done so but what if we want to continuously keep on adding different types flow 62 string a custom age struct and a custom year struct and the list goes on what are we supposed to do now do we just keep on adding functions for every single additional type as you can probably already guess this is not a scalable strategy plus free programmers are lazy pieces anyways and despise doing repetitive things like wouldn't it be awesome if there was some type of a black box to take the place of all of these different data types and then be able to implement the exact same logic but in a more general manner without the need for duplicated code for each and every single one of these new data types that we introduce so this is where generics come into the picture in most of the popular object-oriented languages such as java genetics are an integral part of how the language operates let's take our greater than function as an example we can in turn implement it with a generics and remove all of the duplicated functions that are almost the exact same even more so generics are literally everywhere in these languages like java built in data structures like arraylist functions and libraries and even frameworks however now we have even more questions if generics are so mighty powerful why won't they include it in the golang why of the bat since the birth of the language well at least according to google her goal was intended to be a language for wedding server programs that would be easy to maintain over time so the design concentrated on things like his scalability readability and concurrency and things like generics it just it didn't seem essential enough to the language initial goals at a time so it was left out for the buzzword again in go simplicity so that's why the golang hit did not originally come with genetics at least that's the reason that that's been thrown around whenever this topic gets brought up now you might be thinking okay great you just explain how fantastical genetics are and now you're also saying that we can't use them in go so now what luckily we do have quite a few workarounds in the golang first of all the two of the most common data structure like list and hashmap or slice and map in go have generics kind of natively built in but that's only two out of the hundreds of data structures that we potentially need to use every single day so this still wouldn't quite solve all of the problems that we have fortunately go has these godly interfaces that are supposed to solve a lot of our problems in the world and help our goaling ninjas walk on water so let's take a look at this other example here we have a main function here when in go and this main function calls is a print function which takes in a variable number of integers and just print out these integers so if we go ahead and run this program as you can see it is putting out one two three no problem however if we also want to print out i don't know maybe a few strings here unfortunately the compiler is going to complain and rightfully so because we are asking the parameters it to be of a type integer rather than strings one solution to this problem is a changing the type from integer to b of a type interface as you can see the error message from the compiler is now gone so if we go ahead and run this program again it is successfully printing out one two three twice here now one huge problem with these interfaces is that they do not support static type a checking for example you can actually just change these to be all her different types for the parameters that you are passing in and the kabbalah is not going to show you any sort of error message and depending on what your intentions were this may or may not be what you want and you may want to have at the compiler to do a little bit more hand holding you also have other alternatives like a type of suction for example and maybe even the reflector package in the goes standard library but they still don't have a static type checking yeah you also have some absurd third-party code generators that kind of make the static type of checking in other languages but it still needs to be regenerated and with distributed every single time you make any updates to your code as a result we now have a proposal to implement generics in go and here is how it works there are two main things that we need to take care of with the newest a proposed generics implementation in go the very first one is the type parameter itself the second thing is the constraint on the parameter now let's talk about the type parameter itself first so here we have the exact same example that we had previously demonstrated with we have the main function which is calling the printer function and the print function is taken in a series of its interface and type parameters what we need to do now is a cutting this interface type and after the function name we added these square head brackets and putting the interface typo right here and also assigning the interface a type with a type parameter symbol in this particular example we use a t also don't forget to add her back in the parameter type here with a t now go ahead and run this program again as you can see it is returning us at the exact same result now you might be thinking this looks so much like some type of a synthetic sugar that is not making any sort of difference at all right well not exactly now remember that previously we were able to pass in parameters of a different types integer double and string within the same function call and if we were to do the exact same thing and run this program again we see that it is actually giving us a type checking error which is exactly what we were hoping for but it wasn't available for us and this makes sense because the program is doing exactly what we asked it to do we have a interface a type here meaning that we don't really care what kind of parameters that we are passing in but we have this parameter type assigned into the interface type here and here is where we are telling the program hey whatever you're passing in we want to make sure it's all of the same type the second thing that we need to take care of is going to be the constraints on the type parameter now of course that we are having this wildcard interface type here meaning that it doesn't really matter it's not checking it's not filtering it's not putting a constraint onto what type is allowed to be passed in as a parameters so we can pass in string so for example as long as all three parameters are of the exact same type we can do floating point numbers here and run this program again all of which is going to be executed with no problems if we want something different we want these constraints onto the type parameter we can absolutely do so with again with our mighty interfaces it's kind of ironic we are replacing interfaces with interfaces anyways we will declare this interface called let's just call it type but this interface is going to declare all of the constraints uh maybe we want type integers we want a type string but we are not going to allow floating point numbers that we previously had here and also don't forget to replace the interface wildcard type with the constrained type here so let's go ahead and run this program again as you can see it is giving us this a type of checking error saying that flow 62 does not satisfy type which only includes integer and string now the new is the proposed golang headgenetics implementation is said to satisfy the following prerequisites minimizing number of new concepts the complexity should fall onto the writer of the generic code not the user of it and the writer and the user can work independently meaning that any update to the genetics implementation is not going to affect the user the existing code short bill times fast execution time and preserving have the clarity and simplicity of a gill now some of these are pretty subjective in terms of whether the current proposal is indeed satisfying them so i'll leave you to be the judge on whether we're on the right path so make sure to comment down below i love to hear your thoughts at the very least with this we will no longer need to work with these clunky interfaces a type assertion and or reflection but don't be so eager yes and there are still quite a few things that we're working on with the proposal two of which most of us would actually care about so the very first one is when we don't have any constraints should we keep on using these interfaces or should we replace it with any which only has a three characters instead we're talking about some mega important stuff life and data situation here the second thing is in the newest proposal we have changed to use these square brackets instead of the regular brackets originally which i do prefer since we are already using the regular brackets for functions i'm actually quite excited about her girl language generics and look forward to what the final result would eventually look like at the very least the golang would have likely had become less of a niche language as the lag of genetics has been the last piece of vegetation for many to finally have migrated their code base in java in python in node.js into the golang and this will also likely mean there will be more golang jobs and more and more golang developers in the future as for my personal experience as an ongoing learner for the language i've definitely found that the lag of a generics has been quite cumbersome as well but that's another story for another day so make sure to subscribe to this channel for future videos also make sure to get your one hundred percent free golang chi sheet at golangdojo.com cheat sheet or click on the link in the comment section down below i'll see you ninjas in the next video [Music]
Info
Channel: Golang Dojo
Views: 6,996
Rating: undefined out of 5
Keywords: golang, golang 2021, learn golang, go lang, golang in 2021, go language, golang tutorial, go tutorial, go programming language, golang tutorial for beginners, golang crash course, golang for beginners, Golang Generics. Do we ACTUALLY need them!?, generics, golang generics, generics go, generics in golang, gophercon, generics in golang explained, generics implementation in golang, golang generics proposal, GopherCon 2020: Typing [Generic] Go, generics in go programming language
Id: 7AzUqJ1OtPw
Channel Id: undefined
Length: 12min 50sec (770 seconds)
Published: Wed Jul 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.