← All episodes Episode #49

Smart Types (Like Regular Types, Only Smarter) with Jorge Vasquez

🗓 December 10, 2021⏱ 1h 04m
Smart Types (Like Regular Types, Only Smarter) with Jorge Vasquez

About this episode

Jorge Vasquez has been working on a way to have more precise data modeling while not sacrificing performance or ergonomics. Smart Types in ZIO Prelude are the answer, and they are amazing!

Transcript

Generated automatically by Spotify, so expect the odd misheard word. Select a timestamp to play from there. Download (.srt)

Read the transcript

0:01 Welcome to happy path programming. I'm Brew cycle. I'm James Ward. All right. Well, today we have with us Jorge Vasquez from Bolivia, and I saw your talk at functional Scala, was that last week two weeks ago? When was that? That's right blur. I'm has all strange. Yeah, that's a week. Yeah. Yeah. Last week and just loved it and so we'll talk about your talk in a little bit. But yeah, great to have you on and we, let's start with how did you you're in? Bolivia, how did you get into the colour in Bolivia? Oh well, yeah, that's that's an interesting story. It was some some years ago, I think. 2015, I was working in a company here in Bolivia using just Java.

0:52 So we use the Java object there and we used plain Java. We didn't use any Frameworks at all. The company had its own internal Frameworks and communication, protocols and things like that. It was a Telco. Okay. And yeah, that's everything from scratch. Yeah, we also did some C++ to interact with some very low-level libraries to interact with the 4G networks, and that kind of stuff. And well, I remember one day, my, my boss who is also my my friend and my teacher from University. He told me about cuddling so he why he started looking at calling and told me all this Looks this coddling language looks really nice. It seems like I'm getting worried about was that? That was 2015.

1:44 Yeah, okay. So, early years pretty early. It was the are many years. Yeah. So I got interested. I, and I started looking about codling and I don't remember about at what moment, but I got to ask Allah. So I started saying something problem, the Scala. Yeah, I think it was well, Actually didn't do anything with codling. It was like I was starting reading articles, right? And there, I think there was one of the typical articles like comparing cuddling with Scala, right? And that's how I got to know about scallop because I've never heard about it before, and I started looking at skull and I saw there was some interesting things from functional prominent, write like this typical functions map filter fault. And do you have a man around?

2:39 Did you study math and squalor? Yeah, yeah, yeah. Well, not the math math, I started engineering, but in that, during my career, I had a functional, programming course, it was just one semester and that was with Haskell, and I loved Haskell, but the time and yeah, I remembered about those things. I've learned right, like filter map fault, fold left full, right. And I saw everything. Of that was in this color, right? So that that was the most exciting moment for me. And I started, I decided to learn more about Scylla and I left coddling aside. So so I started looking at the courses from other ski in Coursera right there was already the functional programming specialization and that will after that I started learning about akka, okay? And yeah.

3:38 Yeah. Those so that's that's basically how everything started. Yeah, cool. And were you doing this at that Telco or had you moved on to to a different different job or? Yeah? Well, they're the Telco just wanted to use Java so after that I should know Java has the map and filter and all that stuff now to yeah, yeah. Well, that's a very good point, right? Also, I liked about Java 8 that it included. At the Lambda Expressions Maps filters, but Nations. Yeah, but it was limited, right? So, so I wanted to do well at that time, I knew something about functional priming like the basics in at University, but I always wondered how how will it be doing a complete application using just functional programming.

4:31 Yeah, and I realized as Carla had more potential to do that than Java Java was like With the stream save API, you could do some some nice stuff, right? Like instead of doing those for Loops equal to do everything one line with streams, right? So so that was awesome. But it was very limited and well also I wanted to use more scallop because of Acca initially, because in that Telco company, we had well, the application pattern be are pretty pretty well fit for each other. Yeah, it was pretty well. Threat because with plain Java we we had to use these very low level concurrency Primitives, right? Like logs and synchronized blocks.

5:19 And it was awful because there was there were also that looks right. It was impossible to eliminate that looks and sometimes the, for example, the the short messages system stopped, right, and it was a deadlock and After that the backing to see what happened to two. So that deadlock happened, it was really difficult and even worse, it was refactoring, the code. So, the deadlock didn't happen anymore. So, when I learned about akka, I thought, well, this is a lot better, right? You should use is Calla with akka and yeah, but you didn't that didn't start with like Java akka. You just went straight to its Colour. I wanted to start with Scala occur because I thought it was nicer than in Java but well I hadn't I didn't have the opportunity to use a kind that company and I left. I started working in another company here that at the works with us clients and with that client, I started using a spark. So also I learned about Spark before. Yeah.

6:39 But well initially, they were using Pious Park and they also used kife and hql for the jobs on AWS and they decided to migrate to everything to a spark. But they were, they were deciding if they should use the python version or the scallop version and I told them skala, we should use his Hannah please. So, You want to do? That wasn't necessarily the best choice for them. Yeah, yeah. And well, we did some research, right? I remember one person research to the python version 1, the Java version and myself, this color version. And at the end, it was pretty obvious that scallop was there. The best option in that case.

7:30 So, we migrated everything to Aspire working in Scala and well, that's whooping, I got more More opportunities to work more and more with Scala which Park basically. And well after that, I saw that there were these nice libraries to make spark more typesafe, right? Like frameless wheel. I've only I've only used spark with the dataframe API, which I was not very happy with the dynamic. Yeah, yeah. Yeah. That was, that was a pro. For us also because well, everything compiles, right? But when the shop runs, it gives you random errors. So we had to be very careful and I saw there was this frameless library and I'll have to check that.

8:24 Yeah, it's pretty interesting. It's it's backed by shapeless. Okay so so yeah it uses this ideas of generic types and things like that. Yeah. And after that I realized there was also So quill. So I saw one of these great presentations by Alexander European. Yeah, and yeah, it was pretty motivating because always Alexander presenting a very motivating way, right generals? Yeah, yeah. So I got pretty inaudible well with a spark but I've used it with postgres and and it just, I love it. It's fantastic. And we had Alexander on month or so ago, and, yeah, so that's cool. So you like the experience of quill Spark though. Yeah, it was it was really nice to.

9:13 Well besides that, I started exploring myself about cats cats effect is colors it at that time. I didn't know about zero yet. So, well, I was trying to understand cats all that functor hierarchy, right? And it was really hard to understand, it was really complicated. So I Remember at that time, I decided Well I think I'll go back to Haskell. I learn better about Haskell and I think that will help me to understand better cats, right? And yeah, so I started reading some books about Haskell and with that I was able to understand better cats but still, I couldn't see I wanted to really see the light right. Like the moment they Delight moment when you see oh I can really apply functional priming in everything, right? Yeah.

10:16 And I was it was just looking for more talks in YouTube and things like that. And that's when I started. I reached to one of the of John, the goes talks in YouTube and it wasn't about to see, oh, it was about pure script, I think, yeah. Okay. Yeah. I think I remember that one. Yeah, yeah. Yeah, and that was awesome, right? And I kept looking for more talks about from John because he explains very, very well. And that's when I got to know about the so I remember his talk functional programming today to the me know, it was to the max I think well, I think that wasn't a zo presentation. I don't remember. But there was one of these presentations where he life transformed and imperative application to us. Actual application with zero.

11:09 And that's when I said, I have finally, I can see the light, I can see now that, yeah, I'm kind of using these ideas. I don't have to understand the monad. Hierarchy. The yeah. Yeah, exactly. That. That was my, my illumination moment. So, so, yeah. And things dang. I think that was two years ago. Yeah, when I got to know about z09 address the mobile scholar, see What is the name of the company that you're at? And there is scholar consulting company is alright, yeah, yeah, exactly. I work with Escala see. Well, it's colic. Yeah, well it has, I think both pronunciations are. Okay, so some people say, skala see other ones, a Skylark.

11:57 So there was never a consensus, I think around that, but I started working there last year on March, so that's going. Well, I was on my previous job and I was working with aspire to Scala, right? But I thought, no, I really wanted to work with the scallop with Zeal with functional programming. So, I started looking for remote companies and I applied to some positions. And in that process, I remember, I also saw a talk presented by one of this Cadillac developers. It was in YouTube. I think it was at functional Scala. 2019. Yeah, yeah. Yeah. Yeah, I think that the it was there. And that's going, I got to know about Cadillac today.

12:45 Mentioned, there we are. Hiring and we accept. We're fully remote company and You So Co and you can work in 0, right? So, that's when I decided to to apply and yeah, thankfully, I was able to join them and seeing them since then I'm working with them. That's cool. All from Bolivia. Cool. Well, um, yes I mentioned you your talk from from functional Scala last week and so let's dive into to some about your talk. So you introduce something called smart types, but I think we should start with, not smart types. But what is this problem? That smart types solves? What's the underlying problem? Yeah, definitely.

13:31 Oh yeah, yeah, the pain Point. Yeah, yeah. Well, the problem was the The following and precise data modeling. So we have like these typical problem in applications, right? Like we, well, there are several problems, right? The typical one is, for example, you have a function that accepts an integer as a parameter, and you have to check every time. Oh, is this integer positive or negative? We see if it's positive, do whatever it needs to be done. And if it's negative drawing exception for Both or return and either I left value of a nighter for example, and you have to keep doing that every time right in your application. You have to make sure all the time that you are receiving value to data.

14:24 So, well, they're good techniques in functional programming to solve that kind of problem. So one of those techniques is using a smart Constructors. So they A of smart Constructors is that you make the default. For example, you have a name class, which encapsulates just a string, right? And, well, the normal Constructor will accept just any string. It can be empty. It doesn't make any checking's. So the idea of smart Constructors is that in Scala. You can make the default Constructor private and you can create your own Constructor in the companion, object of this name class. Are constraints. Apply it on the with your constraints applied, right?

15:11 So you receive any string, have a yeah. It won't create invalid objects. So if you think of a type as a set of values exam, just make sure that whatever type you create does actually fit in then set of values. Yeah, yeah, exactly. So the problem that we're trying to solve is that we've got this, we've got these These base types that we store stuff in memory with strings and integers, but we for data modeling, we want, we want to have constraints that are Beyond just those storage types. If this also sounds like I always forget the name of this kind of programming the one where it's like, oh, I don't just specify. This is an array of characters, but it is also size 10, you know, you have the extra constraints that the language imposes, where What's that called? That's what they have.

16:10 A language for it or something. We've talked about this, I don't know. Okay, anyway, it doesn't matter. Okay. So that's that's the general problem that we're trying to solve, right? And so, one approach that you can take is to have a smart Constructor which is going to I guess in the very basic form non-functional, you would throw an exception. If you try to construct this thing and you violated that would be at the point of trying. To create the object and they personally rather than waiting till you're trying to pass it and exactly it's yeah it's got to be this. That's true. And I guess maybe the more traditional approach is to not validate until you try to pass it into something that uses it and to have more precise data modeling as Jorge frame. This the precise data modeling, your function does not accept something that is is invalid.

17:04 So your type your type parameter to your And expresses the valid type that you can give it right? And if it's got to be in the range from 0 to 10 then it's going to say okay because it's that type I know that it will never be outside of that range. Yeah, so then checking it inside every single function that's okay. So that's a you would is. So you would have a type that says that that says I don't really bad name would be integer 0 to 10 or something like that, but you would have the type with that. Yes, exactly exactly exactly. So So if you receive, if you have a function that receives its this integer 1 to 10, type, for example, you are sure that the value is between 1 and 10, right? So, so that's the idea of this smart Constructors and it's a very simple technique that can be used to being in Scotland, in other languages not to Justice color because it's just that technique, right?

18:00 And the return type of your smart, Constructor isn't your value. It's an either. And are or your value and then you have to then. So you have to have a separate function and not a Constructor because the constructors doesn't have a return type and in Scala Constructor is just a fun. Okay? Yeah, etc, etc. So so you can have these smart Constructor function that will return this either type, for example, so it will be either an error or the type like that you want like name for example, right? So, so that's the the basic. Idea that we can use in Scala for data modeling and it works very well but it has some limitations. So for example, if you try to well there is example, I've shown in the presentation at functional Scala was more complicated one.

18:57 Let's say we have a person class and this person has three Fields name, email and address and well, the most naive implementation Some of that will be modeling everything as a strings, right. And well, that gives us emails the string addresses, the string. Exactly. And that's the, like, the rain possible in this strange, right? And that gives us several problems. Well, one that we don't have validated data and the other problem is that we can mix up things, right? We can mess up so we can we try to instantiate a person? We buy by accident, we can pass an email to the name field for example and because all of them are strings. There won't be the compiler won't help you. So you know that you are making a mistake there, right? So strategies as great, you gave me a string and and the type of string is anything that is, it cannot be null in Scala. I can I have a string I think.

20:02 Technically you probably could pass null. Yeah. Your post personal? Yeah. But well in factual priming we restrict string. Yeah, it's just a string. No. Other type restrictions on it. Yeah, exactly. It's just a string, right? And well, with this technique of smart, Constructors, we can create our custom types, right to like, like name email and address that inside of them encapsulated strings. But this Custom types have this smart Constructors, that will do some validations, and so on. And now that we have this custom types, it's impossible. For example, to make that kind of mistakes, like, you passing an email to a name, right? The compiler will yell at you and tell you that that's not the thing to do, right? Right.

20:51 Yeah. I'll be like, oh, you tried to put an email and then name field? That's not valid and it's also can with the smart Constructor validate. Do Validation like named Shannon empty. Yeah. There should be an at sign in the whatever regex thing you want to do to validate that. It's an email. Yeah. See an example you simply never create a person that isn't valid. Exactly. So that should be fundamental make. Yeah, yeah, yeah, presentable States know what's that? What's that? What's? That understandable, States, impossible? Yeah, make invalid making valid States unrepresentable, right? Exactly. And I was remembering about the talk by Richard Feldman. I've seen you invited him some weeks I guess.

21:36 Yeah, we talked to Henry weeks ago. Yeah. And he had one of his talks was was precisely about these subjects were right. It was in Elm but it was the same idea. Make impossible States unrepresentable, right? So so that's the idea and it's a very basic one right about. It's so obvious that that I think we don't really Amber it, right? So we have to be conscious about using it and it solves a lot of problems just by using this technique, but sometimes the ergonomics of doing your data modeling in this way, is hard? Yeah, yeah. Get into some things that you've done to make it not hard, but buttocks. I think that that's really, I think probably most people could get on board with the idea, but it's sometimes it's just is hard to do that.

22:26 Well, I think it also, I mean in my own head I've I keep asking, oh, what is it type? And then I come back to this mathematical description of it's a set of values. And at first, I didn't like that. It was too happy. But now I'm going. Oh, now we have the basic idea and how do we get to those set of values? So one way is the Smart Constructor. And after you explain it, I'm going. Well, why is that sounds like the right solution. Why is that a problem? Okay, so one of the challenges with this approach is that if you have a Mark Constructor that returns in either for your name, for your email, for your address, and for your person, the code that you have to put together to, to do all of those things and assemble it all together, and maybe a crew, all of your possible validation errors.

23:17 Like, it just gets nasty, this just a functional composition issue it. Sadly it is. It's just that the ergonomics of it can be really painful because I would like to know if the name field is wrong and there's only one name in the name field and is the email address wrong and I don't want to have to go back again and again, to get that information. I would like all that information at once. So is that the problem? Yeah, exactly. So before to create a person, for example, if you had just a strings to create a person you just needed to pass the strings and that was it, right? But now we can't just pass a name or I'm Email or an address directly because the smart Constructor always returns and either of those types, right? So now we have a new problem that's composing. The eiders right and Welding Escala we can use for comprehensions to to simplify that that composition, right? Because without for comprehension, so it will be a lot harder, right?

24:22 But it's still something that it's unpleasant at least, right? So the poor comprehension is going to Fell on first are well maybe you want to aggregate all the air? Well, yeah, exactly. Maybe you want my Brigade all the errors or maybe you are creating data with constant values, right? Like, you're trying to create a person, I don't know if James worked with James world at Dotty mail.com and with a valid address, you know, the traffic smile time you have ample time. Yeah. You know static values. And so you don't have to go through all the messiness. You're like, I know this is valid, like don't make me have to go through all the either for comprehension, whatever it is stuff.

25:09 It's like this is a valid person. And I know it is, and you can trust it because at compile time, I have these values and excited in the case of the smart Constructor doesn't matter. It's the spark instructor, doesn't care if it's runtime or compile time and said that. So it's painful when you know that it's something is valid the comply. At compile time. There's no, the smart Constructor is only run it. Run time, right? That's right. Exactly. So that's why the smart house from you don't get that, you know. So what you want to do is be able to create static values and have them checked at compile time except get there, get there. But before we do, before we do, there's a couple other challenges and around that. This idea. One is, how is, how is your type actually stored? Do you use a case class and Scala? And if Then you have wrapping around that, that, that primitive value and so you've got more memory overhead. Okay.

26:07 So alternatively, you could use a, a type Alias to a string. But then there's all these ergonomic issues with that approach and Jorge. You can tell that you this is, I think part of your presentation. So you could tell us about some of the ergonomic issues with with type aliases. And then you could also tell us about opaque types. Okay? Yeah, yeah. Because well you've mentioned other problem, right? Like Well, this first problem like, it's like extra boilerplate right when we know data at compile time, but the compiler can't check it, right? So that's one problem with smart Constructors and the other problem is runtime overhead, right? Because we are using case classes that wrap these strings, right? So, we have additional boxing and unboxing over hit at runtime. So, that's the other problem, right? We could use type Alice's instead of case classes, but also you could say this type of Equals the string type, address equals string type email equals string but with that we will have the same problem as before in which you could mix up your parameters when creating a person, right? Because at the end you can give it a string and a yeah, there's a cool.

27:18 An email is a string and the name is the string. So there's a lot of people have termination. Yeah, you've lost your type information, right? And type Alice's are just other names for the same type. They are not not separate types, right? So, so that's the, those are the problems with smart Constructor. So, in the presentation thing, I explored other techniques that we could use instead of smart Constructors to to solve these problems. So, the problems are the compiled time. Boilerplate, unnecessary polar plate, to the. Other problem is the runtime overhead, right? And also, we have other boilerplate problem at compile time, that is, if you know, that you're trying to create a data, This invalid at compile time, the compiler also will want to detect that, and it will construct hiders. Ideally, we would like that to give us a compilation error, right?

28:10 So, smart Constructors. Don't that for you. Yeah, right. Where and where I experienced? This, this challenge a lot is when writing test, so when I write a test like, ideally my, my constraints that I have on a, on a Type R would cause compilers, if I I've violated them and then likewise, if I don't want to use a Smart Constructor in my test because I know what data, I want to pass this thing and I know if it's valid or not. And so when you're writing your tests around these these more precise data modeling types, it can be a huge pain for a lot of reasons, except I think sadly too well. So exploring other techniques. For example, there is these these new type Library that works just in Scala to.

29:02 By the way, it doesn't work in Scala 3 and I don't know if they are going to create a version for Escala three because it's don't because maybe it's don't even necessary for Stella free. So the idea of this new type library is that you can still use this nice smart Constructors technique, but you can annotate your class with these new type annotation. And the effect of that is that the runtime overhead won't be there anymore. Or. So at runtime these values will behave. Just as normally strings they want to be boxed inside these classes like name email and address at compile time. Yes. But at run time they will be simply strings. So is it using macros to do that? Yeah, I think it uses macros under the hood to achieve that, right?

29:51 So that's kind of a weird syntax like there's a couple different libraries that I've used around the sand and one of the things that's challenging. In this space is that I've seen a bunch of different names to describe the same concept, so there's like new types and then there's like tagged types or tagged unions. Yeah, there are several names. Yeah. And so, it gets a little confusing to know like that. All these things are really trying to solve that problem of precise data modeling and in scholar. There's a number of different libraries that you can use for it. So new types is one even the phrase precise data modeling doesn't actually Lee, tell me what the problem is. Yeah, it just says, yeah, it's kind of generic, so yeah, you get better different term. Well, smart smart type smart types, maybe. Yeah, actually, that is more.

30:42 That is Mor least opens my brains up. Bring up a little more than precise data modeling. We could be talking about domain driven design or something. Yeah, so anyway, okay, so the the new types approach we have better run time. Four months. Like we don't have the overhead of the box and all that but we don't get the, the compile time of static values. May checking like that's one of the downsides of that approach, right? Yeah, we have the same problem as before. So if we try to create data, that we know it's valid or invalid at compile time, the compiler wanted to take that. So, we'll have the same overhead with either and for comprehensions and things like that. So, And ideally, you want to have some way to express your type constraints.

31:36 And yeah, potentially Express multiple type constraints, like and you kind of a mathematical way. Yeah. You want to be able to say like must be longer than this on a string or must be you know and in these character must contain these characters and you want to be able to have multiple and I don't think new types has a good way to. Yeah. That's and it doesn't. Yeah and in fact that that's a Problem, I didn't mention in the presentation, and it's a very good one. Because when you start using this smart Constructors technique, for example, with, or without new type, the problem is that you have a lot of duplication, right? Because for example, in this very simple example of the person class, you have name and address that basically have the same constraint that is, they shouldn't be empty, right? And you have to write that logic for each type, right? For each smart Constructor.

32:34 You have to rewrite that same logic. So, that's also a little painful, right? And with new types, it's the same thing, right? Because you knew types. You also write your smart Constructors. The only difference is that it's yeah, yeah. The only difference is that you have these new type annotation to eliminate the, the boilerplate, at runtime, right? But yeah, so that's another New types are very important, like the type of aliases, but not that they don't let us pass a email to a name field. Is that right? Is that one of the main differences between Type aliases and new types? Yeah. Yeah, exactly. Yeah. I'm too. Well, actually, at the very Basics, the this idea of new types comes from from Haskell, right?

33:19 So in high school, for example, if you try to create a deck equivalent to a case class, will be creating a data type, you have this data Word and you can create like this name type with a string inside of it. But that means that if, you know, boxing, right? But if instead of using the data keyword to use the new type keyword, that tells Haskell to treat that type as just a string at runtime. And that's the basic idea of needed a name that is, yeah, during for. Yeah, exactly. So that's the basic idea, right? And well, after that, you can add these smart construct. Percent and things like that. Right? So well new types of solves one problem, but not the runtime problem, but the compile time is it's also it's still a little ugly yet yet.

34:13 So if you if you try to convince your your teammates, right? It's a little hard to because they don't understand. Also, why I have to keep this repetition across all these types, right? And I need to He's either everywhere and this for comprehension so. So it's not very convenient. So expert other library in Scala that's refined and refined is a very interesting Library. It also just works in Scala to, for now. And it seems parting Haskell as well as a, as I know, as far as I know. And the idea of it is that you can Define Type constraints, but at the type level.

35:00 So it uses like a DSL. There's right. There's like a type DSL. Yeah, type level DSL. Exactly. So in your, in your brackets, you actually use types to convey the constraints that you want to apply to this thing and so it's a very, very kind of strange, awkward DSL. Yeah, it's a little cars. With two to use, right? And well, now in Scala 2.13, it's a little better when you try it. For example, if you want to Define your email type normally you would just create a type, Ali Elias right and say this type email equals to A String, a refined string that must match this rejects but all of that you have to write as a type.

35:52 So it has a type syntax and before its color 2.30 on the Actually you would write this in. Yeah they the DSL was a little weird because to to being being able to write their rejects for example that has to be matched. You had to write these W and T at the beginning at the end of the rejects it was weird looking so yeah, it was amazing tricks in the with the type system to yeah. Yeah, there's but you look at it, you look at the code you're like, how is that even Valid scholar. Could I have used regular expressions for decades and I only get to a certain level of complexity before. Well, usually, what I do is break it down into multiple regular Expressions because one of them is too complicated and even then if I go back and look at it after any amount of time, I don't know what it is. So now, combine that with trying to express that regex in a type system.

36:52 Yeah. Like in the type not in I don't even know what that means not in a string. I I can't imagine it. So yeah, it's very weird and thing is colored 2.13. It's a little better because now to include the rejects at the type level, you don't need to use this W and T that you need to use before because now it's got two point. Thirteen has a Singleton types, right? So that's why the DSL improved a little bit there in refined, but it's still a little awkward, but the advantage of refined is that For example, if you have this email type, you have a type. Ali has email equals. This refined string that matches this rejects to create, for example, an email at compile time, you can do that.

37:40 And the compiler will detect if the image you're trying to pass is valid or invalid. So you could say Val email equals and you put a valid email there the compilation will work, okay? And if you pass, for example, an Just drink. The compilation will fail and it will tell you this value. Doesn't match this rejects. So now the compiler is able to detect errors at compile time. So, so that's what you do about. It gives you the benefits of being able to use the same validation at compile-time and run-time. And in the case of run time, you get back in either whereas a compile time you get back, just the value. Well in the evolution of all of these things, I can see that it would also So demonstrate that this is a valuable thing to do to validate a compile time.

38:31 Yeah. Yeah exactly yeah. And well uh say James was mentioning in refined has this refining V method to do the same refinement but at runtime, right? So you have compilation, validation at compile time and at runtime. So that's, that's awesome, right? So that's a problem that solved there. But are they boxed? The they The books. So they don't introduce any runtime overhead. But there is another problem now and well, it's like a problem. We already mentioned before if you just use type aliases, for example, you have this name type equals refined, the string know me, empty, and you do the same for a dress.

39:21 You have the same problem as before, right? You can intermix address and name except by accident. Didn't write. So we have the same problem again. So, the way to eliminate that problem will be combining refined with smart Constructors. So, so that way you have compiled time, validation and runtime validation, but because we are introducing a smart Constructors. Again, with case classes, that means boxing additional boxing again at runtime Right? So yeah, so so it seems there's no way out, right? So, so that was the problem. We were trying to solve in zo Prelude, right? So the problem is that?

40:07 Well, you have to choose your poison, right? What's do you prefer? Extra runtime overhead or do you want to leave with extra compile-time? Overhead, you can solve both problems at the same time so that for the there is no way to like have our cake and eat it too with With all the things that we want from from precise data modeling, it just didn't didn't exist. It seems so fundamental, you know, once you start talking about it because like, okay, we have types that the compiler will check, but then we have the creation of these types and whether they're valid or not, and that seems so essential to what we're doing. Yeah.

40:54 Yeah. I mean ideally, you would In a program almost never use primitive types like string and whatever. Like sure, everything would be a more specific than there's the boxing issue. There's the efficiency issue and that is always actors and maybe her 80% of the people using, the box is fine, but then, there's always going to be the others who go. It's, you know, this is going to cause US problems and so you have to solve that problem, but it really seems like this should be part of the The language. And as I, I'm slowly learning zo, one of the things that I kept asking it was what is co. And finally, I think it was kit because I said this feels like more of a language within a language.

41:41 And he said, yeah. And that's and this is part of that, what you're doing is part of that, is, is like, okay. Now, our new language has, here's how you create things and make sure that they're reliable without How do you solve the problem of boxing? Yeah, the well that was interesting read and well, before going to the Opera look, oh yeah, we got you can pick type. Oh, okay, you think. Well as color free now has opaque types, maybe with opaque types, all the problems are solved. Right. So the idea of opaque types is it's basically they are type Alice's, right? But they are more smart. So you can say opaque, opaque type name equals the string.

42:26 NG, right? So in every part of your application, the code the compiler won't know that under the hood to. This name is a string. It's a type of audience, but all your application doesn't know about that. It thinks it's a different type. Yeah you can't you can't accidentally intermix. Still can't intermix is drinking with names. So it's kind of a fix to type aliases. It's yeah, it's better. Type Alias it's a better type Alias exactly. Right. But you don't, if you want to add this additional validations, right? Like well, this name is a string, but it has to be a non empty string. You again, have to resort to a Smart Constructors, right?

43:15 And healers and box and you have daters compile-time valedictorian of Value, exactly. So again, we have the same problem, so yeah, it seemed there wasn't any way out. So that's why. That's why we this we started looking well. This was John's idea originally and he told me, well, maybe it will be nice to to implement this. Well, by the way, I am part of this part-time program of Joan. And yeah, we have this one-on-one Once every month and you can use that time as you wish, right? So burn you and and your project you're working on with him. Yeah, exactly.

44:03 Yeah. So so that's, that's awesome. Yeah. Because Jang knows most a lot, a lot about Scotland, functional priming. And at that time, I remember I wanted to explore more Escala, three macros because it's kind of free was it wasn't even released yet. It was almost there and every one of us knew about this new and Chinese color. Free macros, I saw the Alexander yaffe presentations, again she he put some nice videos in YouTube, like implementing a very basic version of quail in Scala free to demonstrate how escalatory macros work. And I told John well, I'd like to explore more about these scalloped free, macros think.

44:51 Cuz they seem really, really cool. Right? And he told me, well, maybe we can implement this. Well, we told them refined types to that time, they weren't smart types and the idea will be that they should be validated at compile time and at runtime and there shouldn't be any overhead. So that was the goal, John Carter division initially. He had that idea in his mind for good ergonomics, good. Section c, good compile-time validation. Ideally like, understandable, understandable, understandable. That want to be able to write your constraints in a rational, none way. Not? Yeah. Yeah. So like keep like, I think when you were talking with K2, some time ago, you mentioned right.

45:43 Like zo is like a state of mind, right? I think this is State of Mind is making everything as performance. As possible and as Urban ohmic as possible, right? Because that's how we are going to attract more people to functional programming, right? And so those were the objectives and we started exploring first, how to implement these smart types in Scala free, because that was my initial objective. And well, it was a little difficult because there is not too much organized. Documentation around macros, right? After talking, this really great talks from Alexander, I started to understand better macros and I was able to implement something very basic and after several weeks because we whether into being able to implement what we wanted finally worked.

46:43 So, the idea was that we should have these nice assertion DSL. And this assertion DSL. It's basically where you say this has to be a non empty string, for example, or this number has to be greater than 0 and less than 100. So we have a nice versions. Yeah, we have a nice DSL for assertions but we wanted to toss assertions to be evaluated at compile time. So that was the problem, right? And how to do that with Matt because the assertions are written in Scala, right? Like they're yeah, they're working normal. It's normal. Color code. Yeah. How do you take something that is his a Scala runtime thing and make it run at compile time. Exactly.

47:30 So that was a problem, right? And well, that's basically what quill does, right? It has an DSL that normally would be executed at runtime, but with the magic of quilt, everything is evaluated at compile time, right? So that's how we get. We have a normal Scala expression, converted to a SQL query, right? So, but I didn't know how to do that, right? I don't I didn't have all the knowledge that Alexander has, right? So so well but after looking at his talks everything was clear and we were able to implement the basic version and it worked so that that was really exciting. And well after that, The the next challenge was okay. Now we would like to make it work in Scala to as well. Maybe it's not possible, we didn't even know if it was possible because it's color to macros are completely different right from from Stella free macros and they are harder than dying.

48:38 Escala three and kit was the one who helped me there a lot. So, but because he has more experience in Scala to macros Dying myself, right? Because he has worked a lot in improving, the ergonomics by implementing macros under the hood, right? Like, he has improved to the assertion DSL, 40 test, right? So under the hood that uses macros, soak, it helped me with that and he made it work in Scala to so that was awesome. Yeah. And after that, we integrated both versions and we tried to make The syntax, the most similar possible between Scylla to and in Scala free. Because in Scala Fritter, some differences in the syntax, but they are very minimal and that's because of how its color free Microsoft are different in solitary, right? But yeah, and well, finally kit, and Adam did some improvements there.

49:45 Because before implementing this refined types that was The initial name, there was also this smart types but they were separate thing that John already had implemented before a 20.per loot, but it just worked at runtime. It didn't make any validations at compile time so it eliminated. The runtime overhead, that was the idea and but I think it went out real quick to is you also made it work so that when you're working when your underlying Type is like a string of primitive type. You made it so that that you're smart types don't have to be boxed but you can use like a case class as well.

50:31 So our example of a person a person is a case class which has boxing but then the name email and address are just strings and so those are not boxed. So you're able to then also unify this so that where you use a case class it works and where you use a primitive, it works. Yeah, exactly. So yes, you have mentioned this smart types Ardent boxed. Right? So at runtime, they will be just strings, right? And it is, they do compile time validation, right? Thanks to the implementation with macros, right? So you can if you want to create a valid person at compile time, you can do that to directly without using either and stuff like that are try tractors. Yeah, yeah, and if you want to do runtime value, Relations, you can still do that right with either.

51:23 Well actually not either in the zero product version, we use a better type that either that's validation right? So, this validation, it's kind of similar to either, but it's not short circuiting, right? So before we were saying that, if we're trying to create a person with name, email address and the name fails, everything will immediately fail and we will just get the error with the name, right? But With validation, you can validate all the fields in parallel and accumulate all the errors. So it's better than either. It's an applicative instead of hitting applicative. Right. So so that's the fancy terminal because Bruce and I were talking about applicative is a couple days ago.

52:08 Yeah, I probably understood it at the time but now it's gone. Yeah, just stop cumulation. It has the ability to accumulate. The are some make sense. So the thing is this doesn't sound Sound zo specific but it's part of The Zo Library. So Prelude. Yeah. Yeah it seems like this maybe should be something that's just separate thing that people can use without bringing all of Zo in. Yeah yeah well in fact there was a discussion around that some time ago and I've seen some people were precisely mentioning this topic these days in Twitter and well the uproot is a separately. Right. But it depends on 0, right? And well, the reason for that I think is that in the Prelude? There are, for example, super lutein, besides smart types has other things, right?

53:02 Like type classes, right? And in the approval to, there are instances of this type classes for the types in The Zo library, right? And that's why there is this dependency, but there was this discussion that maybe be in the future. This functionality of new types may be separated to a completely different Library, not super Loop, so, maybe that will happen. I don't know. But yeah, there was a bird's head from ngos for a base color for will have this built-in. I would be nice. It doesn't look great. So essential. Yeah, the more I think about, yeah just types in general. Yeah it's like what are what are your tips for you know, there? To should be there to help you.

53:52 Yeah, exactly. Did you you should be able. Well, I sometimes. We mentioned this typical examples about functional programming red. Like for example, if you have this division function, that just divides to interview integers is that pure not a typical function around functional prime rate. And you say well, it's not because if you try to divide by zero it will throw an exception. Right? Right. So in that case, what you do to make it appear, so you have two options, right? One option, it's like the typical one. Instead of returning ink, you can return an optional field Example, right? But the other option, any time you do a division, you have to return an option and then unwrap the option and I'm wrapped you'll Chun, right?

54:37 It's really painful, right? But the other option is used is using, for example, this is Mark types in the inputs, right? So instead of accepting two integers, you say, well, the first integer can be any integer, but the second one is an integer not equal to 0, 9 0, right? So that way, you can still return integer as a result and you don't have this overhead of the option, right? Having to to check the option value every time, right? So becomes a pure function and becomes a pure function, right over total total. Total total function. Exactly. Yeah. Or your option three is when you divide by zero just return 0, which flicks does this. Yeah.

55:23 And I'm so annoyed by this. Well, maybe we just don't understand it. Yeah, there there has been a lot of The around. Yeah, because I think rust, maybe does the same thing that oh, yeah. And were inspired by has been a lot of debate around whether or not. This is the right thing to do. And I think the right thing to do is to make your, your divisor a nonzero guaranteed. Yes. A little, let the compiler help you, right? Because, well, the other option will be, maybe not returning zero in that case, but returning, like the minimum value of NT Wright and you have to be very be careful about that because you have to remember that all the time, right? Is that you are considering user have to check, right? Well you might as well just program and see if you're going to do that. Yeah, exactly.

56:10 Right. So so we are not using their the power of the Scala compiler, right? It doesn't make too much sense. So yeah. And well, I wanted to mention also, this nice dies else we have for assertions there. They follow functional principles. So they are very composable. Yeah, you can compose a serrations every way you want. And we also have this custom rejects DSL. So when you try to validate your emails, for example, you can still say it has to match these Rejects and pass the rejects a string or you can use the rejects DSL that it's nicer. Looking. I think you'd like the regex DSL because then you're not ready in just a string, regex.

56:56 You actually have a type type Sell that your hand. Would it give you more specific errors? Well, would it tell you like maybe where it failed? Oh you're missing and a sign or something. Yeah, yeah. Well that a great now. Yeah, right now that doesn't happen right. But it cooled. Yeah. I think that will be a very nice Improvement that there are types of day error messages would be more present are messages out of red Jax's. That would be the turkey. Yeah, awesome. That would be something. Yeah. Yeah. Yeah. And the the assertion library is really cool because I don't know if you've used like a combinator library where you chained together like in this case assertions, but the way that you chain them, together is essentially building like an AST of of what needs to be validated. So you can say like, all right, let's group these two assertions and both of these need to pass.

57:52 But then, you can say, okay, or and so you can use like algebraic combinations of these different assertions. You can use them across different smart type so you can reuse your assertions, as well. Exactly, mmm. And so a lot of super cool stuff there. Well, yeah, I have not used your new smart type stuff yet and it is high on my list. Next week. On our, in our book writing session, we should explore this because I think we need to chapter in the, in our book on and I've been thinking a lot about how you compose data types in functional programming. Gramming. So this would fit. Yeah, yeah, yeah. I think this feature will be, it's really nice to including applications, right? It solves both problems, we had with the other approaches, the other libraries, and it works in Scala to in Antonin, Scalia free.

58:52 So that's great. We really wanted to make it work in Scala to because For several years, there will be lots of projects that will keep using ask Allah to write for sure. So if well, we didn't know if this was going to working in Scala to initially but yeah thankfully it works in Scarlet was well so that's awesome. So yeah, I mean, just coming from the python world where we went from python 2 to Python 3. It is a slog and there's going to be a lot of many, many draggers heels. And All that kind of stuff, so making it work in Scala to. I can see how you go hon. I don't know if this is going to work but it does that's going to make a big difference. I think people will it.

59:37 Yeah. Yeah, except yeah. So so I think that was very important and yeah, it will help with adoption, right? So there is some sure. So, if you skeletal, okay, you still can use it. And the good thing is that as I've mentioned before, the syntax is very similar. There are some just Three I think one or two details that are different between this color too and discolored, three versions. Yeah, it is. Yeah, that's the thing. You gotta look at it like over here, maintain it for scholar to and skull three! Whoo, we got to make that as easy as possible. Yeah. Yeah. Make upgradability easy, cool. Well, what else did we do? We miss anything awesome about smart types? Well, well, I think I think that That's basically it. That's the idea behind smart types. And well, there are still lots of improvements to do right, like having this more precise error messages, that will be great.

1:00:39 I think that will improve false ergonomics that something very important in Zeal. And well, we still need to add, for example, more assertions. And well, maybe there will be even more improvements on that side. And maybe I was thinking the other day, maybe we don't even need an assertion DSL. And we can use just playing Scala syntax right to the serrations. I don't know. I'm just thinking out loud that. Maybe it will be similar to send to what key to dating zo test, right? Like you can write your assertions, just normal Boolean expressions and they get analyzed under the hood by macros.

1:01:25 So so I think that's a possibility. Well, to make this even better, let's go. That would be amazing. I mean, all it all? Yeah, I mean, especially for me the more I think about types being, I mean data structures being immutable, then you go. Well okay, so you don't have methods anymore but you still have the construction problem and that's where you need to put all of your smarts. So that once it's created, you don't think about it anymore. Yeah. Except that you can't forget. About that completely, right? Because all every part of your application, you know that if you receive a person it's a valid person. It's impossible that it's invalid, right? So, you know, a bunch of logic just goes away, you know?

1:02:11 Yeah. You don't have to check if the name of this person is non empty and if the age of this person is, I don't know. No - and that's typical right age that to term use of your time. If you think about it, you're going I've Duplicate this testing code in every single function, you just a huge source of bugs because it's like because they want to forget it, do it. Yeah. Where you going to get it? You've you copy and paste incorrectly whatever and it's just dumb. Yeah, yeah, yeah. It's very easy to forget, right? We'll all, we've all been there. I think, right to you. You have lots of methods, right? And you repeat the same validation logic in all the methods and sometimes you forget about that in one method and that's because, Does the compiler is not helping you at. All right?

1:03:00 You have just strings and you can't do the compiler. That doesn't know anything else about your strings, right? It's just know they are strings, so it's transmitter and help you, right? So the idea of this is making the Scala compiler work for us, right? So use use the incredible power that the Scala compiler has, right? Because other languages other compilers don't have that power, right? Right. But it's colored as so-so. You lose it, we should use it awesome. Well, this is such amazing work and thank you for sharing it with us. Jorge and, and thanks for joining us on the podcast. Super fun to see you. Hopefully we can see him person sometimes, and yeah, yeah. I hope it may be next functional Scotland.

1:03:45 Yeah, well, it's been a pleasure for me. Also having this time with you guys to discuss about smart types, my Beginnings with Scala has. Well, so, so yeah, thanks. You think a lot for for it? Biting me here really interesting. Thanks.