 Thank you for a to be here for this talk. I know it's the last slot. So I really appreciate it I thought the room would be like nearly empty. So it's great I'm Nicola Frankel for a long time. I've been a Java developer working as consultants Then I did some orchid ticketing stuff and I mean and at some point I decided that Customer where to bother some and it would be much better to go to conferences drink beer and talk to To two people which is what I'm doing right now I'm now working as a developer advocate for hazel cost and I've been long Time cocking fan who knows about hazel cost by the way Yeah, that's we assume that so if you're interested if you are in the Java ecosystem basically Hazel cost is like a distributed that are structures to make it very simple and then we have another product called jet which builds upon it and Which does trim processing using hazel cost and DG so in memory, which it needs to be very very fast okay now Probably you might have heard already about domain specific languages like a couple of years ago They were very very hot. I even think there is a like an orally book or a manning book about them and basically a domain specific language Narrows the scope and the features of a general purpose language in order to guide you to make Developments on this narrow scope much easier and for example We can think as XML as a general purpose language even oh, yeah I know it's not really a language and then you can create those Narrow scopes languages such as SVG on top of XML or If you are using Java You can have those DSLs which are used for assertions. I don't know why but Assertions seems to be very popular for DSL So a couple of years ago. There was ham crests first asserts now. I think the most popular is assert J and In Kotlin At some point there was this Way to create and raids you eyes and they add a DSL code on co So you didn't need to write your UI in XML. You just like really coded it So you didn't need to in flight. It was pretty good. I think that they stopped the developments I don't know why because I'm not an Android developer And then I created something called cutting that I will show you in the end and In Java you are pretty limited to create such DSL and in general What you are doing to create the DSL is just like you sequence your method in order Okay The most popular method is actually method chaining So basically while you are returning the current object at the end of each method and then you can like change the method together You can also have nested method calls and lambda, but in the end the best you can achieve is something like that so this is assert G codes and Yes, it reads like correct English And you cannot do much better. So that's that's what that's most you can do I have an example in another project Here this one So here is like a DSL which uses the Florence API and You need formatting, but then you are when you are formatting it reads pretty well It looks like the YAML if you are if you know about Kubernetes, it looks pretty much like YAML But you you can you cannot do much better with Java so I want to create a DSL and I wanted to look like declarative and still to be codes and Let's use Kotlin to do that so let's start so I will start with this very simple sample which is Varding codes and Varding is a web framework who knows about Varding by the way Couple of people. So I assume half the room is our Android developers, right? in general Like the Java server side people know about Varding because it's the way that you can't create Like web apps without coding a single line of HTML So if you already are front-end developer or an Android developer, you don't care that much Now Even if you don't know Varding you understand, I think pretty well what this does So you set the theme you create a vertical layout You set the content of the UI to the vertical layout then you are the margin then you are components and yeah Then you've got your stuff This is pretty cool to write. However I Think there is this big issue that basically you can just change the the order and Nothing will happen. I mean you will still have the same the only thing that is really relevant Are those two components? You can even set the margin at the end or set the content at the end So if you have like team of developers They will write each in their own way and unless you've got very very strict Guidelines that for example you should always set the content at the end or set the margin at the end or whatever Then it might be hard for to get into some new code So how can Kotlin can help us? So first what we will do is we'll create a new UI called lean UI and We will create this Kotlin UI So I want to create the same but using a DSL but first I will be very very step-by-step so the first thing that we can do is here we have this set theme and Let's use some property access syntax. So that's one very nice way to do that So if you have a setter then you can just use this property access syntax it gets the things a bit better Now if we would be in Java here in order to start we would do something like that So we would create like this like static methods and we are we are in Java So it's supposed to be object-oriented, but basically we also have always have these UIs classes that are not object-oriented at all and Then they create the layouts so if I Copy paste or cut paste this into your Kotlin Thanks to IntelliG. I already have this working for me So I can copy Java codes copy and and paste it into some Kotlin and then I've got already Kotlin. I can remove this object because with Kotlin I can have like top-level function and It gets much much better already. So basically now I can say hey vertical layout This and I get my vertical layout and then I can affect this to a vertical layout and I can like add some layouts dot add components or label blah blah blah Label Okay, but still I will have the the the the same problem as before but again as I mentioned we will do it like It's text field. We will do it bit by bit Text field I will call it. Hello So I have the same problem as before I have this order problem, okay now What would be great is actually I have this vertical layout. I Can use those extension function in Kotlin So instead of having like this function that use this parameter I can say instead that vertical layout is called on UI and then of course I can remove that and Here I can have that so it's a bit better already now on vertical layout basical in every layout I can have like margin and padding So if I was in Java, I would create like an additional method and here margin Boolean and here more gin and Here I would create the padding one and here say vertical layouts Set It's called spacing. Sorry spacing Is spacing and I can use this Here Okay, and now comes the problem. I can have now I want to to set the default value So here I would say vertical it returns something so return vertical layout and I will say margin Let's say the default will be true and Here I would say that It's return a vertical layout and The default would be true Something like that Now issue with that is I need to choose what the default For spacing all margin I can have I cannot have both because actually those are Boolean parameters, so they send the same they say share the same Signature there are conflicts and it doesn't work anymore. So as the previous speaker Told we can wrap them inside an inline class, then it creates a new type But it's a lot for not much So what can we do well the first thing is we can provide Defaults and now we can remove everything and we have a single signature here vertical layouts We can say this It will work it to be a bit smaller. Everybody still sees even at the back fine I'll create here too and Here I can pass nothing. So everything will be true Now I still have an issue because if my spacing Is true and my margin is false. Sorry. It's the opposite that I wanted to do I still need to pass it as true even though it's the default which I don't like because it's too much So no worries because we've named parameters. I can have exactly that so right now the only thing that we still have as an issue is this like components ordering stuff Actually, it's a real issue We didn't solve the the initial problem. So we created everything that worked, but we still had this this problem Now comes something very very great We can add a lambda here we can add a lambda that will Be called a receiver lambda It takes nothing it returns nothing. It's called. Let's say in it and With this lambda we can apply it Sorry, we can apply it. So basically apply means that we will execute this lambda on this object and now We can get a bit better By doing this still not perfect, but it's a bit better and We can also here We can actually like scope what we don't want to see saying a apply and We can apply here the stuff that we did before and now it's this put this everywhere and I can change it and I don't even need this Variable so as as a user you don't care that much, but as the code maintainer you it's interesting and given that actually It's pretty easy to understand then we can use like a simple body like an expression body And it doesn't tell me how I can do it. So I will do it Automatically and remove the return type because now even If you understand here you return a vertical layout the compiler understands it and you don't need it anymore There are two applies and now as a like library designer you need to to think what's more important What do you want to override? For example, do you want first to apply? the custom Stuff and then whatever happens you will set the margin to the one that was given or and the spacing to the one That was given or do you want to let the user like full power and then he can override saying a You can set the content to something else. So as a as a library designer. That's something you need to do Now here I don't need this variable anymore and I can create my vertical layout like this and The problem with that is you always need To pass a lambda you cannot just say the article layouts Like this But that's fine. Just as we default parameters that are Boolean We can pass the default parameter that is like the empty lambda and then it works again I don't know if you what's your level in Kotlin, but this is made possible by the fact that normally This would be what? should be expected But if a lambda is the last parameter in your method parameters, then you can extract it outside It's just syntactic sugar But actually it gets pretty good the fact that you can remove it and and I Don't know if so the question is whether This syntactic sugar has a name and the answer is I don't know so perhaps somebody in the audience knows the name I Call it like lambda syntactic sugar. I know that this as a name. It's called lambda with parameter That's that I know but this one. That's a good question. I will check Sorry again Yeah, it's just syntactic sugar. It's just like if lambda is the last parameter. You just extract it and everything is fine okay, and I mean now, you know everything because the next step is to actually Do the same with like the label So I can create a label and it's exactly the same stuff that I use so here I'm using the label And I have like a lambda with parameter that has by default nothing then creates a new label with This and now I can replace this like add component label with label Hello, and guess what I can do the same thing with text field. Oops, not here. No text field field Nope, so I will copy past its Because I don't remember the shortcut and So I will say text fields now should be caption and here it's this at text fields and It's a text fields Yeah, once you understood the trick, it's it's a lot of copy paste. I agree Here and guess what now you can replace it with text fields with world And now you can do some stuff here and here you can do some stuff also here and the good thing is Now they become nested and now you understand what happens and actually if you are not interested in something Given your ID you can hide it. So basically it's a master detail stuff You can enter at any points and and see everything and then go into the detail But it doesn't stop there like imagine that You have a lot of people who will create html levels so here as I As I did before My sample UI here was saying hey hello in strong and I need to set it as like it was Content type so I need to set this dot contents mode. Sorry HTML you don't want it What does it tell me? Okay, so perhaps it was not the right one contents mode html now. It's the right one now I can remove this now. It doesn't want to import. Please do it. No, okay, don't do it So imagine that every time I need to create that that wouldn't be that great So what I can do is I can create a dedicated one That will use The label function that I created above and that does it automatically So if you have a lot of people that use this then just like make them their life easier and just create an html function and once you you understand that life is easy and Because you can do it for the whole framework and if I run that Here you can see that I've created a sampler with every possible option of every possible widget and that's Pretty stupid Everybody can do it It's just a lot of time But basically is just like copy paste every time everything is here and For example for Hazel costs, and I didn't know what to do in the plane. So Actually our our configuration is in YAML or or XML or JSON well Let's create a TSL because it's easier. It's less error prone So here I didn't do everything but you can see that if you know Hazel cost It looks really nice and it's compile time. So basically you can check everything and Works pretty well, and if you are using spring You might know that they are also using this stuff when they create their spring DSL and actually This is what it looks like and again, even if you don't know spring you can understand what this does and Here for example, that's what I like a lot that you create a beam of type person repository without passing the class again thanks to the like inline Stuff which makes it possible to refile the classes though It's not reification is just like since it's like copy pasting But still it knows about the type and then it knows that it must create a beam of type person repository Now the question is a We had groovy before we could create nice DSLs. Why don't you use groovy? Well, if you are you groovy user, I guess that it's perfectly fine to use groovy. However, I like my types I like them. I know a lot of groovy people they prefer to say oh, but you can do Everything and when you need the types you just put them. Well, I would reverse that stuff He say hey, you know what I prefer to type everything and if I really I cannot do that Then I can also cast to any if I cannot Then perhaps there are scholar developers here because color is very powerful. You can do everything Yes, you can do a DSL with basic You can write basic Using skull well that the problem is like a scholar is very very powerful And then in like if you gather like 15 scholar developers in a room, then you will get 20 solutions for the same problem. So not my stuff either takeaways So the first thing how to create a DSL is basically you must start from Java and Java Whatever the API you don't care because your Kotlin code is hundred percent Interoperable with Java in both ways. So you can call Kotlin from Java and Java from Kotlin Of course, if you create a DSL in Kotlin in Java, it won't look so great But you can build upon your existing Java API even if it's your not your API Then you can use the property syntax. So basically if you have like setters just use the equal sign It's always it looks a bit cleaner The third is extension function a lot of people say a what's the best stuff in Kotlin? Oh, it's nullability handling In my opinion It's extension functions. You can achieve a lot with extension functions My code becomes much more object-oriented when I use extension functions And when I'm having when I have to code back in Java, it's really really hard for me because I miss that feature Name parameters and default values. They go hand in hand. You should really really like consider them So instead of overriding every time or sorry of overloading you should consider using them It makes your your task much easier. So instead of like chaining your your your methods, just use them London's we've received where they are the next big thing Like for DSL there are there are a must-have like everything is based on them Some stuff that I didn't talk about operator overloyal. So in Kotlin You can overload operators, but it's not like you can overload everything like in C or Scala So you cannot have like hash hash bang bang function You must have you can only have a dedicated set. So in some cases depending on your DSL You might want to add Persons together for example, then it's good to remember that you can overload the plus operator Refile generics I mentioned it's when I showed you the spring beans DSL It's really great. I mean the fact that you can like make believe that you have refile generics even though the bytecode has no refile generics is really great and Something that I also didn't use in my demo is the infix So if your function only has one parameter You can annotate it with infix and you can call him without parenthesis So depending also on your domain Depending if you are using a math DSL or whatever then you can create a nice DSL and remove the parenthesis and it looks bad and So thanks a lot for your attention. You can read my blog follow me on Twitter and more importantly you can have The look at what I just show you you so you can like get clone it and and play with it And also I have this card inside So if you really want to to see how it looks like when everything is finished Even if it's in the old version of Valin then I courage you to have a look and you will see It's only what I showed you here So basically if you want to create a full-fledged DSL with the 10 Bullet points that I showed you previously you can already do it by yourself. I Really must warn you that it's very easy to write them Again, it's a lot of copy pasting and it's it looks super nice Beware because once you start creating a DSL then it's code. So you must maintain it so if you're if you if you create a Nice DSL for I don't know on top of hazard cost That means that you need to maintain it or to give it back to us so we maintain it But anybody I mean at some point somebody is going to have to maintain it So it looks nice, but it's like every time you write a line of codes. You must think about yeah What's next and next is maintenance. So I guess we have some minutes left ten minutes Is there are there any questions? Yes, what about the performance in the ID? especially IntelliG IntelliJ because I think when I was using libraries with some extension methods it I had To wait a lot for auto-completion to appear like maybe one second or something. I Is it okay? I didn't have such experience and to be honest I didn't benchmark it So I don't know but as as you could see like on my demo. It's it's quite easy Of course when you create extension methods and lambdas and whatever in all cases you will have those long function classes names and of course that's not super great I think that in the latest versions of Kotlin you have incremental compilation So I would suggest you to use that to speed up if you especially if you have big project But everybody uses my cross-services anyways, so that's fine other questions Do you use Kotlin in Hazelcast in production code? Nope Because I don't want to maintain that stuff because I know that if I write it I will have to maintain it and here I have the main like director of engineering and he will chase my ass You see I don't want to put my finger in that so I can I can give it to engineering and then they maintain it But again, it's really Every line of code that you don't write you don't need to maintain So you really need to think About the trade-offs and in our industry that's funny because we are supposed to be like logical people But it's either white or black and most of my talks is it depends It depends on your context. So if you think that you make you you will ease The way that your user are using your product then of course you should provide a DSL Now I know that not many of our user are using Kotlin So I will address only a small fraction of our community and I will need to maintain it The trade-off is not in in my favor here Now if now everybody here decides that tomorrow they will come in ten other people to use Hazel cost with Kotlin Then let's start talking. I must say that I've seen a lot of Kotlin DSL libraries been Like stopped like maintaining has stopped because they didn't want to maintain it. That's not my question In high-performance environments. Do you think this should still be done? Especially is do you think there is a like an overhead in calling any functions? Could they be in line? Yes, you can I mean also in line is not magical. Yes, you don't you don't need to call Afterwards, but then you've got a lot of copy pastes of these like Codes so if it's a lot of codes then no, it's not a good idea I think that when you when you have such questions you should basically do the benchmark in your own context We've option a and option B and checks which matrix Metal to you other questions Okay, let's call it a day. Thank you very much. I have Some cool has a cost stickers and there are some cool Still have a lot of calling stickers