Koin 3.2 - Improving the Kotlin Developer Experience by Arnaud Giuliani

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign [Music] thank you for being here hope you enjoyed the conclikov this is the last lot we are Friday I imagine you're a bit tired but uh let's keep a bit of energy together um just I have one question to know you a bit better just raise your hands if you are using coin in the audience quite a few of you uh cool cool to see all these people using coin is here just a quick presentation of myself I'm ano Giuliani I've created a coin in 2017 and the project is uh still going well and today we will talk about how we improve the experience of development with coin it's a dependency injection framework that means that it helps you structure your application and the idea is that coin provide you an easy way to be super efficient with all of that and we talk about improving the developer experience with coin and of course the talk is about the coin 3.2 version but time is flying and then there's already out there coin 3.3 3.4 uh we talk about a few things about there then uh up you will enjoy let's let's step back and uh I try to to explain you coin in two minutes more or less and uh what about different suggestion the best injection is about having a few classes and then you want to wire them and then having access to them in the instance and let's have a simple case here for example Class A Class B and the idea of coin is that it's it's used the kotlin language to provide a DSL then we open module here that is a section it provides you a space to help you configure your application and then we have another keyword singer here and then here we are describing a Singleton into our application the thing here is that behind this single expression is directly cutting it's your code is something that is compiled during in your code and then here you you may see that we are instantiating directly the class A and then if we are making the same for class B then we try to declare things and then yeah we need something to wire the instance needed by Class B here and this sees the game of dependency injection understanding where to find the things for you and the third keyword you need to know at least to begin to to use coin is get if it's go yeah and then in this function then Class B with gets we say to con to coin yes give me an instance of Class B and go on your side uh look at something that is that is looking at this type in your pocket and then give me the dependency and then we are gone this is how we create all the instance with Constructors we can then also use complex kotlin function you can write whatever you want inside those configuration and then it works the thing is also this DSL can allow you to have a few parameters a few options and then here we can say to coin can you create this instance at your start and then when you we run a program then you cutting program with main function we just say Main and then we just use the start coin function with modules and then we say that we are starting off modules the previously module function that we have we have used as we we can set as a value and then it's creating instances okay pretty simple dependence injection has to be simple in the case where the uh the the class that you want to instantiate is not um cannot be created by con you need to use field injection and to use fin injection in our case for example this class might have you can't can't be created directly by coin then you need something you need something to inject your your class B instance from coin and then this is what we have is a coin component interface that help you unlock the kotlin extensions from coin and then all give you the power to use inject function anywhere it goes and then this is how we have this inject function to inject a field inside my app and then you see we have the if you instantiate my app then you will have access to class B from a coin container you may have and found this kind of inject function in Android Android application and the good thing of cartoon is that we can make extension directly out of the box in any kind of system and you feel like it it's part of the of the the main the main SDK that you are playing with like here we are using appcompat activity it's an Android activity component and we are using injet and we are not using the coin component interface and then how we can do that is that coin provide out of the box all the extensions for you to have the coin features available in Android and that's it but sometimes the DSL can become to be very reliables and then it tends to you could have some real expression like that and you could be yeah something like that well this is a good time then to to have lots we had lots of feedback from people that says hey coin is great but really having dozens of gates Powerline is really annoying then the good question how we can avoid to have those get everywhere because finally the the game the the thing inside coin is that we are not making any introspection we are not making uh any reflection on your code then we are just executing your code inside the deal sorry the DSL then okay we have this we have of configuration how from this single definition then we have two single tones two kind of component but we have gets we have to say to coin hey give me give me the dependency please and in this what you have is that you are writing your function but we are giving you the tools we are giving you the API gate is the coin API to let you write the dependency to go forward what we have currently and if you want to look at the source code of code this is something that is looking like that then the first ID of the DSL was to say we give you keywords and then you can manage to reuse those keywords and functions to write your injection but now we are looking to the other point and then we say okay give me your functions and I will put all the injection inside and then we are more close to the inversion of control of the dependency injection thing and then it feels more natural for this kind of dependency injection framework but you can use both like you can write functions and if you want we can inject all your components if you want how it goes then you see I've written single function with a function and hand the new keyword that we have released in coin 3.2 is single off that means that we are creating a single and then we are taking a pointer to the class Constructor directly and then it's working it's it's creating the instance for you the idea is that it's easy to write to read easy like the DSL we keep consistency in the semantic we have the same keyword for you and also in terms of changes it won't break anymore like if you change something in one of the Constructor you have defined here the configuration it won't break like if you were using the classical DSL from coin you would have a change in the Constructor then the your compiler would say hey there's something that smooth here you need to edit your code and then this is how finally we have something that is more fun to use and then accessible in terms of also capacity for KMP great the good news is that it works with dependency but then it can work with Advanced more advanced features like injected parameters the idea of injected parameters is that if we have a class and we want to pass a data at the time where we are creating the the instance we keep the keyword here single off and the class directly don't forget the semicolon uh warning about the current IntelliJ staff behavior is that sometimes the import don't work like if you try to type directly double semicolon and my class A uh intelligent you know don't find your class then you have to remove the double semicolon and then import the file and then put the the double smick on something I really need to check with the IntelliJ team but you may have something in terms of import to to check but then we have this class we need a data which is an ID we Define it in coin and then what we do is that we pass this parameter two coin to say this is the idea I would like to create the instance with and then you see that here the writings are simple then I don't have to care about writing about my injecting parameters all of this is great it will cover let's say 80 percent of your usage but in some case you will have some difficulty and we have Prime to use that because kotlin can offer you default values name parameters and then you will require to go back to the classical DSL because when we take your function we take all the parameters and we try to make to to put get on it on every parameters as this is the same we don't make any introspection any analysis then we want we won't be able to understand are you using default value are you using anything else in terms of uh value for this Constructor parameter the thing is if you have complex expression try keep keep using the classical DSL if you have a definition that is quite long and using only get you can use directly this new Constructor DSL format this experience also help us to go forward for the the other options that we have for the DSL of coin and what kind of options would you have on these dsls is we need a qualifier qualifier is a label you put on the definition to try to differentiate two kind of definition of the same type if you want to put extra lighting if you want to create things that start then we have we may have lots of options appearing in terms of the Excellence and construction and the thing is in the current in the classic DSL then we have parameters then you see that we are opening parenthesis and then we say name it's the way to to indicate we are using qualifier we can say we are creating this instance a start then it's another parameter and also we we if we want to bind a compatible type directly for this definition and says for example this definition don't bind only my class but my class and my component types then I need to to add it but you see that finally here in terms of experience we are mixing operators parameters then can be a bit weird in terms of handling those functions ending all those apis then yeah this is not super great this is Google but not great because then for us also it block us from providing new extensions a new way to to work with your DSL but then we have a new Constructor DSL we can take this chance to to open the new options DSL and then you have these functions we are creating instance for my class A and yeah we have the place here to directly open something a block code to let you write anything you want for this definition and then we could create we could use the function named qualifier created at start and then bind any kind of components you want um yeah this is great and this is more readable but compared to the other way of writing your definition it's interesting and you can use this way to write options with the the uh the old format of definition okay then we have a class and the compatibility is done with an operator named with options and you use with options and you will have you will unlock all the new DSL options for you directly and then you will have the same kind of approach of describing options for all your definitions it's a good way for us it's super important to keep it readable to keep it simple to have things that you don't have to to really be scared about how you how you write things we talk about the DSL improvements um and now we will talk about how we handle module dependency in coin then in coin you under module this is space where you you are describing oily components here for example we are describing two modules one for UI module and a data module and usually what we do is that we say we are starting coin with those modules directly you say modules and UI module and data module okay fine and you can use your clear list of modules and you can use operator with the plus operator then okay that's fine you are using a list of modules but we are getting into some limitations and for you it's a bit strong because there is no strong link between modules like you are using UI module and data modules there is no link and you are making the link dynamically by loading everything and in terms of scaling you could break things into without understanding in terms of dependency from what you are using uh in terms of other modules then it's a good thing for us to give you the possibility to clearly Express some include even it feels a bit like yeah it should be already there it's a it's a good it's a natural options the coin Community didn't really require such options and really with big big big company that are using coin at scale it's really become really important to have a mechanism to ensure that we can link everything for you the idea of includes is that finally you will be able to reuse many modules from anywhere and then for us on our side we are taking all your module graph all your includes and then we are flattening everything and then we are optimizing things and then it's super important because then you reduce your configuration complexity and it follows you to better reuse all your stuff everywhere and you don't have to care about how you are re-including how you are reusing search or Search definition of the configuration and then it becomes Supernatural to scale in terms of modules to have one feature model that are using common data common UI and then you can scale at many many many levels and force for you in terms of impact there is no impact even more we will optimize the way how you are we are loading everything for you then feel free to use directly uh this includes this includes functions all of this is directly available as a multi-platform API that means that this is not restricted to gvm or or API you can use it on the sharp code your coaching share code or your native share code clearly to unlock all of the superpower just use the latest coin version you have coin coin core is the project for the agnostic coin dependent injection framework that means that you can use it for multi-platform and in the basic kotlin application after that you have several flavors of coin where we are integrating more feature and advanced feature for coin in Android and other kind of ecosystem like like cater you can do you can use coin with caterer directly but then we are currently in three point four point zero this is the new version for this semester and then we will follow up for for the the patch and the delivery updates for Android for coaching and everything around one new thing we try to launch and we have launched uh last uh last year is uh is proposing complementary new options to write the coin if you don't want to use the DSL you could use annotations and for a long time coin has been a framework really oriented by the DSL but the idea if we begin to orient ourselves for annotations is we don't want to reproduce any existing solution like the the start of coin was to say we can provide tools fan tools to let you write things super quickly in a cartoon DSL and then you don't have any override in terms of compilation and any other things but then if we bring annotation we really want to bring something new we really wrong we really want to embrace this experience of annotating to your code and then going fast but we don't want to impact your compilation time and we don't want to give you other impact after that and yeah I will say that we have limitation in the in the coin DSL because we can't analyze what we are writing and this is not the right place and then this is where we are introducing kotlin compiler plugin and the coding compiler plugin is the space where we can really begin to analyze your code to make even if we want a rewrite coin on the Fly but let's keep Simple Things um the idea will be to for us it's a good way to introduce new tools for the annotations and we are then stepping up with Google KSP that provide us some good tools to avoid to to go deeper into the complexity of um of kotlin compiler development I don't know if you've seen complete complier development talks it's really really heavy and then it's it demands lots of resources in terms of time to to invest then for us the idea is that we will be able to configure everything with annotation we go we can go quickly we want to keep the coin semantic the word the keywords we want to find them again as an annotation and then the idea is that we will generate just a few lines of DSL the few lines that you don't have to write or we would avoid you to write then we just annotation we will write the single definition for you in odsn set up such projects you can go and set up that insertcoin.io and then you can use the KSP Gradle combine the KSP plugin and use the coin annotation um um Gradle dependencies let's go into details and begin with annotation and then let's go back to our simple class to simple example here and then if we add a notation and we keep semantic then we if we have a single on it to have our Singleton and that's it we know that it's running we know and coin will start and then know that those dependencies are existing and then we are running and it no introspection on your code nothing at runtime to impact you we can detect type binding and we you can also specify it like for example if we have another type and you would like for example if you have multiple type went too fast if you add an interface here you do have nothing to to to change the definition will bind directly in my type also then Class B and then my type and you see that in terms of parameters we don't have to specify anything we know that your Constructor will need some something from coin and then we'll try to inject things if you have too much types then you can specify if you really want to optimize your binding and then expose your configuration for one type you can go for binds binds parameter that allow you to make the things but you don't need it's really if you really want to optimize your your stuff and then in terms of dependency you have nothing to to declare here we understand that your Constructor have this type this type this type and then we understand that we are generating the DSL for you but then we can go for we can go deeper we can understand that this is a new label and then for coin we know it will generate the get all new function for you access and then um you will you will have something in your label here you can write lazy if you want a lazy dependent a lazy class here the lazy type from kotlin then it will be detected also and last kind of type we can detect in the in the Constructor as a list if you want to request all the same the all the definition of the given type here the list of all the Clays a for example then coin will understand and then generate the get all functions behind in the DSL and then you see there is nothing for you to write we keep things really simple we detect what you have in terms of Constructor and then you can you can really enjoy to have nothing more to do you can play with properties and injected parameters that mean that this is special parameters here and then we need to indicate that this is not a classic uh dependency here you want to get Okay then if it's in injected parameter then you tag your your property add injected bomb and if you want this to be found in the coin properties registry then you can say add property on on your field and you see that finally all of this is really straightforward to use it's really just for the dynamic properties that you have to type things else we can detect everything for you okay let's recap a bit um let's take just a quick example that he's more familiar that uh to what you are using a bit every day like we have a repository of data okay with an interface then we target single then if you have a presenter let's say then we are using the interface directly then we can say add factory factory is a kind of instance in coin to say it's creating a new instance each time and single it's a Singleton then we keep your instance in memory and then for example if we have a view model it's an Android view model here then we have dedicated tag for the Android the Android ecosystem then then the main one is at coin view model to declare you of your model inside coin as a view model and then in a few definitions you cover all your dependency injection it generates your coin GSL configuration behind and then we have all the automatic injection and binding uh keep in mind that there is the new label detected and the lazy and all the list of type then we handle for you directly you don't have to make a special um annotation or something and for uh properties then either it's an injected parameter either it's a properties from the system and then use the proper annotation but in terms of experience it's super important for us to be sure that you have the minimal to write and then you can do the maximum and it doesn't change your code you still use the buy inject or by view model fill injection in your Android activity on or any kind of place where you are using fill injection it's still compatible nothing to change for you it it just changed the way you are declaring things um and then you need modules that see here we have declared the definitions and here we need a few modules but we can't use we can't use directly the the DSL modules why because then The annotation processing know about classes functions but not directly values directly declared like that and we are in boundaries between runtime behavior and then analytics Behavior then we can't simple responses we can't then we need module Class A Classic module class you may find another solution is yeah I have a class my module and then you target at module okay super super simple then the idea for coin instead of having a class pass scanning or other kind of scanning is to say coins can this entire package and then you will have all the definition bound to this module and then you can have several modules just bind the package name and then scan for this and then all your definition will be tied in your module Dynamic directly you can of course use the includes function that we have seen just before and then you can include other module class very classic but finally here it's important has as I would as I was saying we are optimizing all the inputs for you and then inside a module class you can Define also a function and it depends how you want to use those annotation this way to declare things is more close to DS to the DSL and then the feedback of of developers is yeah but then better to use DSL up to you like how you feel with that and then you can declare from function you can declare directly a component that means that you declare functions the parameter will be your dependency and then inside the body and your of your function you are using the dependencies that can't provide you and lastly if you want you can even use top level definition that means that you don't have to own modules or to have a module to write a function to declare a definition and this is one of the latest updates from coin notation what done two is that directly from a function in the file just tag it with at single and then you create a definition for you pretty uh pretty pretty straightforward then we need to start coin you see you see before that we are using the start coin uh keyword but then now we have DSL and classes how it goes then we use we still use start coin function then the modules and then the idea is okay we need to run a module from the class that you have defined and the magic here if you call it the magic is that the only point of generation of code generation is done here is that we are generating an extension of your Mod on your module class that are representing the that is the right that is the coin configuration yes sir we are writing the DSL for you then you write less you use annotation the flow can be seen as a bit better for you but then you can use directly the generated code inside your start coin function and then up to you to mix annotations or module if you want like you can begin to work with the notation if you want to test with annotations like you see that these modules function the my DSL module is a DSL module and then the my module class you see that we are instancing the the class module then we are launching both the the DSL and then we are reaching the generated DSL function and then up to you to find the way you want to work like um in terms of impact it's really super super fast the idea is not to reproduce something that is taking long long long time and the idea of coin at the beginning was to say okay let's get rid of everything to to be super minimalistic in terms of compilation and then we just need to compile completely with key SP and just the fact that we are targeting coin GSL generation then we have a really really low impact super interesting you can debug it like we are generating this DSL you can just break point inside if there's something wrong you can directly go inside and you will understand what has been generated and then it's not annotation to replace coin DSL it's a new tool a tool to let you really have the way to run you want to to use coin like if you if you like annotations then you can use annotations if you really like DSL you can continue to use DSL but and you can try to use both up to you in terms of Team culture to see how you want to deal with that like in some team sorry um you may want to have only one way to to to to write then uh it's a it's available tools sorry sorry coin is a is a tool that has always been been made to make everything simple then my main word if you have to to keep something in mind from this talk is keep everything simple please and something to let you meditate or so on this quote a girl less bugs let's talk about uh what's next with coin um I don't know if many people seen but we have many tutorials to let you um use sorry I need a little further to use uh to to to begin to use coin in a menu configuration and then you have cutting continuation many kind of Android application from view model jpeg compose annotations but also multi-platform and Cato then you can use you just check out you follow it's uh it's a tutorial in a few minutes you can run an application that is using coin in terms of roadmap where are we going in 2023 um the new version 3.4 is out there and then and these are already ready to go for you end of track that means that in terms of community support we won't publish any more uh patch on the 3-3 because it's already six months it's already has been published since months away then we can't hold all the the versions than them in terms of Community Support then the new version is 2.4 latest stable 3.3 and the News table is a 3.4 then in these three days now the read the written that we try to address is that we try to keep um release from one semester to avoid having too much releases and the problem is that we have Android version we have Cato version we have updating cutting we have a date and multi-platform Then and Now on compose then now we have multiple multiple ecosystem to Chase and we try to have a peaceful way to deliver for you new versions coin 3.4 come with the new experimental curtains engine and then we are taking new functions to help you make dependency injection with proteins and then one of the first feature we want to to to to to bring also as experimental is that we won't bring um any uh anymore you can unblock completely your start process in your Android application this is really interesting you can unlock your start process with coin by loading in background with proteins audio modules um you you can you will see on our blog I will give you the the URL later where you can find the details about the change log but here's the the new the new version and also two new important versions coin notation 1.2 that is bringing the top level functions and coin worker and also um the new thing is for us is that we are covering completely jet brains composed and the compose multi-platform side not only Android jetpack compose but also all the music platform stuff then it's super interesting that means that we extend all the apis from jetpack compose the Android jpeg composed to the multi-platform apis and then we're offering more features like remember inject remember modules or remember scope then something that are more tied to all the compose life cycle something that is a core value coin is a pure open source project and is a Community Driven then just for me to thank you all the to thanks all the people that are contributing to to coin and the price some of you are in this wall of faces from GitHub but it's a matter of people a matter of making things better together than a scene and this since a few years now since 2017 we have made a good road and then if you want to join the community to have resources for news like do you have the website inside con.io the Twitter also in soft coin IO we need to see but yes we are still on Twitter and also the kotlin slack the kotlin language slack uh instance we have a channel Dash coin where you can ask question if you if you see things you can ask questions about all the community and if you have a real issue you can also raise an issue on the GitHub project coin is an open source project but um with with the the time I created a company to help people to add company to help other company use coin at scale then we are here to help and also uh we are beginning to launch a new platform a new developer platform around architectural development around architecture Technologies then stay tuned go on codeiller.io to to have the news and to see about also the Twitter I believe it will be interesting also Link in and if you want stickers there is my friend Michael that is on the on the side here jump on into to catch some stickers and uh thank you very much and uh please rate the talk thank you [Applause] we have a bit of time for a question if you want thanks for the talk uh so if I understand correctly the dependency resolution happens at the compile time right at the runtime at runtime okay then my I have no question now okay thank you hello uh might be a follow-up to to his question but um if I would use annotations would there be any performance impact at runtime no no no because then we are just generating a bit of coding code and then you compile it for uh and then there is no impact in terms of uh in terms of runtime okay thank you thank you for the talk uh I see you showed to the ways to create with single and respected but what if I have like SDK and I have a websock inside ID and this websocket instance should be pure SDK client instance so I can have like two sdks and I need two websockets um if I if I understand your question if if you have separated SDK and to to to see if uh separated SDK can see the same configuration uh uh well so my SDK have uh like uh two internal components and each of them use the websocket but uh each instance of SDK should have its own instance of work socket then you can this is what we call context isolation that means you can embed your own coin context and then it's completely isolated and then you can you have your own instance for your SDK and then uh if you have a you you have your instance on your side and it's completely separated okay so it's context resolution exists thank you thank you hi thanks for the talk my question was similar to the one that was just asked so if you had an interface and with two implementations and uh both of those are marked as single uh is there a way to uh somehow it's runtime determine which one to use based on some environment variable or something like that yeah yeah I forgot to to mention that we have at named that name is a qualifier and then you give a name and it take a string and then you you type at single and then you have another animation at named and then on each implementation you have you give the right name and when you are when you need to inject the right implementation you use at name on the on the function on the Constructor parameter okay thanks okay I can take other questions just after that if you want in private thank you very much [Applause]
Info
Channel: Kotlin by JetBrains
Views: 7,231
Rating: undefined out of 5
Keywords: Kotlin, Kotlin Programming Language, Kotlin Multiplatform Mobile, Kotlin Android, Kotlin for server-side, Kotlin/JS, Programming language, Programming, Android development, Kotlin Multiplatform
Id: Th9FeISf87A
Channel Id: undefined
Length: 42min 11sec (2531 seconds)
Published: Thu May 04 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.