06:00 UTC Charta Aetatis Cyberpunk

Console Newsletter Pro Sign in Early access Register
Aa · Display
Themes
Type size
Density
Hacker Culture · Lobsters · · 1:03:41 runtime

A preview of Roc 0.1.0

Starts at 4:21 Comments

In the wiki Funkro Sweden
Transcript 7753 words · auto-generated captions

Machine-transcribed by YouTube. Expect errors in names, commands and version numbers.

Hello everyone and warm welcome to Funkog Sweden. We are back after a short summer break here as it's Sweden. We all usually take vacation during July. So we have not live streamed anything during July. So warm welcome to Funkro Sweden. Um Funkro Sweden, what is it? We are a meetup. We are a a YouTube channel and

we explore and endeavor into functional programming and we also welcome both new newbies to functional programming, people that are just curious and want to know more about functional programming and also veterans of functional programming. So warm welcome to every one of you. And with

that let's head over to today's agenda. Uh first up on today I'll do as you

noticed a short introduction by me h and then we'll head over to our first presentation a preview of rock 0.10 by

Richard Feldman that I'm sure is going to be very very interesting presentation. Richard has been with us I think was like two two years ago I think uh and presented rock and now he's coming back with a preview of the 0.10 one zero um release h and

then we'll go over to an introduction of regami by Michael Borquent

that's also very interesting um and that's going to go into why do you build a web framework etc. Um however first I would like to thank a beat our video sponsor for today. Uh if you want to know more about Autobe check them out on social media or on their web page.com where you can find more information and how to contact them etc.

Um yes let's look at the schedule coming ahead then um in September we'll be

back. We haven't set the date yet for September. We'll come back both with a date and hopefully with the two pres presenters and two interesting presentations. Then in October we'll have the funk proconf which this year will be a full day conference. Uh last year we also did a conference funk proconf then it was like a half day with three presentations. This year it's going to be a full day uh of presentations and a bigger venue and more people. Uh last year I want to push this really uh we sold out our tickets. So make sure to head over to to Funko Conf and make sure

to get your ticket on time. As I mentioned last year, not all people that wanted to come could come. Yeah. Then in November we are back. We are at Kra again live streaming both. So it's going to be mixed event both live and live streaming. And then of course December we'll head over. We'll have another meetup coming up. [snorts] Also, if you want to support Frank Sweden, uh the easiest way to do that is just head over to become a member of the meetup group and make sure to subscribe on the YouTube channel. It's the easiest and best way to support us. And also, if you

want to talk to more people, want to know more and you want to more ask more elaborate questions to the presenters, join the discord channel where you can ask more questions which comes now. questions. If you have questions, you can shoot them in the chat in the YouTube uh YouTube, however, I think uh don't allow too long sentences. So then head

over to Discord and ask the questions there. Um with that, we are about to

start our first presenter and presentations for tonight. Uh or depending on where you are, maybe today. Uh so warm welcome uh Richard to Funkro Sweden and welcome back.

Hey, hello. Oh, you need to unmute. Yeah. Or he needs to unmute. Yeah, no worries. We maybe sort it out. >> It works. >> Okay, you can hear me now. >> Yeah, now we hear you. We had to unmute on our side. Sorry. [laughter] >> You're muted. No, I'm muted. Who's muted? >> No worries. >> Thank you. Yeah, warm welcome, Richard. Yeah, the stage is yours. >> Great. Thank you so much. Um, yeah, so uh this is going to be a presentation about, uh, the Rock programming language. Uh, if you're not familiar with Rock, don't worry. I'm not going to give a full tutorial of the whole language. Um, mostly I'm going to talk about, uh, sort of an overview of like what we're doing, uh, where we are, like what, you know, what we've built so far.

And, um, uh, by the end of it, uh, you should have some idea of like why rock might be interesting and why you can be excited about the upcoming 0.1.0. shadow release, which will be our first ever numbered release. Um, since this is a meetup, uh, not like a conference talk, if you want to ask questions along the way, I'm totally happy to stop and answer things. Uh, and also, you know, we can kind of go off in tangents and things like that. I'll I'll try to sort of keep an eye on the clock myself. Um, and, you know, not let it go [laughter] too far over or anything like that. Um, if you know, spending too much time on tangents, but I'm happy to go on them.

We don't need to stick strictly to the the presentation here. Um, cool. So let's start with uh some basic slides and I'm going to do a little demo. So Rock programming language a preview of version 0.1.0. I'm Richard Feldman. So Rock's tagline has always been fast, friendly, functional. So these are three values that we have for the language. Fast in the sense of this is a language that we really want to both build fast like fast compile times and also to run fast like run fast at runtime. Um friendly in the sense of user friendliness and also hopefully a friendly community. these this is like a community value as well as a like user friendly error messages nice tooling out the box things like that and of course functional because it's a functional programming language it's not multiarigm it's not attempting to be object-oriented or anything like that just functional um okay so let's uh let's do a demo before we get into anything else so you can sort of see what kinds of things you can do with rock uh right now so this is a little

2000 line of code um rock example this is going to be a guey app so I'm just going to run this um it's called screwbot I didn't actually make this. This is uh Luke Boswell made this. So this is Let me full screen that. Um basically like this is a little like robot arm that you can uh you sort of wiggle around. Um you can see all these different like sliders and stuff. You can you know move things around like this. And um so I'm I'm not sure exactly like what the you know what Luke's goals are with this or what this is supposed to do. I mainly just know how to click the buttons [laughter] and drag the little robot arm around. Um, so, uh, Luke is in Australia, so it's like, you know, two or three in the morning for him right now. So, he couldn't make it, but if you want to ask him questions, he's Luke Boswell on, um, Rock Zulip.

Um, anyway, so this is, uh, this is, like I said, it's about 2,000 lines of rock code. And one of the cool things that you can do in Rock is, uh, you can do hot code loading. So, let's say, I don't know, PGA Lab. Where's that? Uh, PGA motor, PGA live, PGA Lab. There we

go. Sure. Uh so we'll just like add some exclamation points here. And what's going to happen here is that uh if you watch right here, these are going to the exclamation points should appear there in a second. U yeah there we go. Um when the hot code loading happens. So this is you know all all of my current state of like the robot arm and everything is preserved. Um you can go back and take those out. Uh and this is all just like you know there's the hot code reloading you know it's detects it you know takes them back out. So um yeah, so you can make changees to the program on the fly. Uh and it just sort of hot code loads. This is just me, you know, changing the the uh source code and you know, I'm going to exit that. Um this is just for me running like rock on this, you know, main.rock program. This is not like a you know, I had like a separate third party complicated watch build thing set up. That's all just out the box. Um I want to make this build faster. Uh for o.0 this is maybe acceptable, but like you know it did take a couple seconds to start up and it did take a couple seconds for it to detect the hot code.

Ideally, we can get that closer to sub-second in all cases. Um, but obviously this is not, you know, a it's not the final release of the language. Certainly, it's 0.1.0, not, you know, 1.0, but also it's, you know, at the same time not on the other extreme of like, you know, taking a super long time or like you have to restart the whole program. Um, okay. Uh, so, uh, yeah. So, let's let's talk about sort of rock syntax and things like that briefly so we you can sort of get a more thorough understanding of like what the language is about. Um so here we have uh a really basic rock function call. This is a file readf8.

The exclamation point is part of the name and I'll talk about that's an important naming convention in rock. And then you have one argument which is the path. Um so the read UTF8 part this is sort of the the function itself. So this is like what a normal function call works and then the file part of this is just uh essentially saying like this is where that function lives. So we have a type called file. Types are always capitalized in rock. And then file dot means you can think of that as like sort of like a static method or if you're used to um like an ML family language like a h or an elm you could also think of it as sort of like a module but it is associated with a type called file. Um so here's the the full type of that function. So read UTF8 is uh has this type. Uh so this right here you can think of as our syntax for like the function read UTF8 has the type. So colon is means like has the type. If you're used to like hasll this would be a double colon. If you're used to Elm it would be a single colon. Um and then uh we say like here's the the one argument that this function takes. If it took multiple arguments they would be comma separated. So rock does not do curing or rather rock functions are not curried by default. Um there are two types of arrows in rock. So there's thick arrow and thin arrow. So this is a thick arrow uh as as in like equals greater than.

And that basically means this is an effectful function. In other words, this is a function which when you run it is allowed to perform side effects. It's not a pure function. We also have pure functions uh which do not uh have this um thick arrow. They have a thin arrow. So like dash greater than and those are in practice the more common example. But I'm actually going to focus for this sort of syntax example on more effectful functions just because IO is such a you know big question that a lot of people have about programming languages that have a first class concept of purity which rock does. Um here's the return value of the function. So this is [clears throat] a type called try which has uh two type parameters which is stir namely a string and then the other type parameter is this thing in square brackets. So briefly uh try in rock is basically like result in elm or in rust or uh hasll calls it either. It's basically a type that has two different type parameters. One for the success type and then one for the error type. So the success type is basically this read UTF8 function takes a path and then assumes that the file that is behind that path is going to contain a bunch of UTF8 formatted text and if we succeed at read reading the entire file then you get back this string and if the file fail if the read fails then you get one of these things. So this right here in the square brackets this is an anonymous sum type. So rock has some types as like algebraic data types much like you know elm or hasll or rust uh these are called different things and f discriminated unions and one of the things that makes rock different from most of those languages is that it has a concept of anonymous ones or structural ones. So you don't have to define them formally upfront. you can just say basically hey this is a variant of some anonymous we call them tag unions um where the name of the variant or or the tag is file read and then it's it's got a payload which is a file read error and basically what this means is that if you are building up an accumulation of different error scenarios which we're going to see in a second um this particular function like if I call read UTF8 is going to contribute one of these variants to that union of errors. In other words, if I do a file read that, then I can maybe now I can have a file read error that can occur if right after that I do a file write, you can imagine like if we had a a file, you know, write UTF8, it might contribute a file write error. And so now I have two different errors to handle. I could have a read error or a write error because naturally if I'm, you know, going through and um doing a read followed by a write, etc., those can error out in different ways. If I'm doing a network request, that could c like, you know, a network error in here.

Um, but this is a totally ordinary uh

sum type. It's a it's an algebraic data type. You can do pattern matching on it. All that normal stuff. Rock does not have a concept of exceptions. It doesn't have a concept of like go does as we'll see later. Um like an if error is nil. Um it doesn't have null. It doesn't have undefined. It just has algebraic data types. Um and it's this anonymous algebraic data types that makes our error handling unusually ergonomic, I would say. And and we'll see an example of this in a sec. So if I want to assign a value to this because this is just again an ordinary function that returns an ordinary value like URL equals um this is going to be a constant. So rock does not have a keyword called like const or let or anything like that because in rock basically like this is just the normal default thing that you do is you say like I just want to make a constant named URL. So boom that's how you do that. um rock does not allow shadowing and also of course these are constants so they can't be reassigned which means that if I wrote URL somewhere in the outer scope that would give me an error because it says like oh you can't shadow this um you know you've already got one defined in the outer scope and also if I you know later down below said URL equals it would say either you know if it's in the same scope it would say you can't redefine this um you've already defined it to be something and it's a constant uh or it would give you a different shadowing error if it were in a nested scope so I know some languages that are all about you constants and immutability which rock certainly is. Um like elixir for example or rust will allow you to sort of redeclare like recycle the same name but it's got a totally different definition. Um rock doesn't do that. We just say you know if you define a constant like this uh that name URL for this and all of the descendant scopes um that's just what URL means. Um okay [clears throat] so expanding this out a little bit here we have a an uh a more elaborate example of uh doing some IO operations. So, first I'm saying file readf8 with [clears throat] the exclamation point um indicating that this is a u an effectful function. In other words, not a pure function. And then I got this little question mark at the end here. Um this is something that actually comes uh from from rust. So uh this function call right here is going to return one of those try values which is the same thing as like result in rust or elm or either inll. And the question mark it does in rock exactly what it does in Rust, which is to say if I've got uh an okay variant

from this, in other words, like this operation succeeded, it just says, okay, well, let's just unwrap the the payload from that okay value, and if it was an error, just do an immediate early return from this function with that error. So, this is a way of sort of propagating like handling the error by propagating it uh by having the entire function early return from that. So some functional languages like element hasll do not have a return keyword like you can't do an early return. I guess in HA hasll uh return is not a keyword but it does exist. It just means something different than what return means in most other languages where it is a keyword.

Rock just has a return keyword. So you can just do an early return like this. Um and this is a really popular form of syntax sugar because if you have a bunch of operations that can all fail whether they they're IO operations or they could just be you know parsing or something like that. Um, this is this is a pretty nice way to to handle that because it basically says just just go ahead and propagate the error. It's a little bit like uh an opt-in version of exceptions rather than an opt- out version. Like with exceptions, you sort of have an implicit one of these where it's like, well, if the exception happens, then it sort of happens invisibly. Uh it's it's incumbent on whoever called this function or ran this uh code to surround it with a try catch in case there's an exception that happened. Otherwise, by default, it's just going to keep propagating until someone does that.

This is the other way around where you have a normal ordinary some type for a value and with this one little character, this one little question mark, you're basically opting into propagating it and saying like I don't want to handle this right here. I'm just going to say this function early returns and then it's up to the caller of the function to to handle the error that happened here. Um, doing the same thing here with this http.get. So basically what we're saying here is this file that when we did this read UTF8 is going to contain a URL in it. That's what we expect to be inside that file. Um, and then we're going to pass that URL to this HTTP.get, which should give us back an HTTP response. Then we want to say, well, I want to just get the body of that response. So, here we have something that is basically a method syntax. I resisted calling this method syntax for a long time because rock is not an object-oriented language. And usually when you think of methods, you think of object-oriented languages. But I kind of gave up on that. A because when you look at this, everybody's like, yeah, that's a method call, right? Um, and B because Rust is also not object-oriented and they also use the term method for these. But basically this HTTP.get is going to return an HTTP response. And in rock you can have formally associated functions with types. So that response type is going to have a formally associated like here is the body function that is associated with that type. And that allows you to do uh this type this type of like quote unquote method calling. The main reason we have this is um it's our form of ad hoc polymorphism. We call it static dispatch. And basically this allows you to do the same kind of polymorphism that you have in Haskell with type classes or in Rust with traits. Um we just do it in a way that uh is a little bit more uh structural rather than nominal which is to say that um you can if you want just say I'm going to have some value and callbody on it. And you don't have to necessarily specify exactly what type that is. you can just sort of say well the type of this function is then going to be you have to give me a value that has some method on it named body that takes zero arguments and if you or takes one argument um and if you do that then uh this function will work out. So looking at this code you can kind of guess what this does as long as you know what the question mark does which is just http.get yet. Um if that fails, the question mark will do an early return with that error. Otherwise, we've now got an HTTP response and then we're saying get the body of that response and that's what uh this this body will be assigned to. Finally, uh we have the the final expression here where we basically say JSON.parse on that body. This is one of the most unique things that rock does. Um if you look at how something like JSON.parse parse works in like JavaScript or TypeScript uh it's full of potential runtime errors like or or delayed runtime errors I should say. So like in Typescript you have um if you do a JSON.parse on the body it's basically going to go through and convert that into a JavaScript object. And even though TypeScript's whole job is to put types on JavaScript stuff it doesn't actually attempt to validate the types of this thing. It just says like well I don't know what's there. There could be anything in that JSON body. I I mean I know it's a string but I mean it could have any structure. It could be, you know, a list of nested objects. It could be um a whole bunch of like strings. It could be numbers. I don't really know what's in there. So, I'm just going to grab everything that's in there. And as long as it's syntactically valid JSON, I'm just going to stuff it all into a JavaScript object. And then if it turns out later on that the way that I'm using this thing turns out not to line up with what we parsed, then we just get a runtime type mismatch. Like the program crashes. Um that's an example of type unsoundness. un soundness meaning that the compiler said yes these are valid types but then at runtime there turned out to be a type mismatch if you can have type mismatches at runtime that means that your type system is not sound that's just what those words mean so rock does have a type a sound type system and if you call json.parse parse on this body. It actually will dynamically at compile time verify this.

And when I say dynamically, what I basically mean is that Rox's compiler will do type inference on how you end up using what this function returns. So if you end up using it as for example a user that has a field called username and that's a string and then you have like an email which is a a custom email type all of those things. If this JSON cannot be parsed into like at runtime into however you were using it at compile time, this will just give you a parse error at runtime that says like, hey, the shape of this JSON did not match the way that it's being used in the program. And so this is just a parse error um in the same way that you would get a parse error at runtime if the JSON were, you know, missing a curly brace or something like that. Uh except it's a structural check. And the structural check is based on how your program use this thing at compile time. So it's kind of this cool sort of best of both worlds uh approach in my opinion where you get to have the conciseness and the simplicity of you know the the JavaScript approach where you just say JSON.parse. You don't need to formally define a sche schema up front if you don't want to. That's totally optional.

um the schema is sort of inferred because the compiler knows how you're going to use this value and it uses that knowledge of how you're using this value to basically derive an implementation of JSON.parse um at runtime. Uh this is not

a like JSON specific thing. Um this is just something you can do in rock uh in general with parsers. Uh so JSON, XML, CSV, even something as complicated as like um columns coming out of a database. There's a way that you can just define those arbitrarily and the compiler will just infer implementations of them. All you have to do is basically, you know, use it and then someone needs to specify how to generate one of these parsers from whatever the particular encoding is, whether that's JSON or, you know, Postgress data coming back from the database or something like that. Um okay. Um now uh

these two constants here like URL and body you know [clears throat] we can observe that each of them only gets used once right I say URL equals and then like I just use URL here and then body equals just use body right there and also we can observe that both of these are being used as like URL is the first argument to this http.get and then body is the first argument to JSON.parse. This being a functional programming language you don't need to name those intermediate things. What you can do instead uh to do the same like you know operation of reading URL out of the file and then fetching that URL and then parsing response body is you can make a little pipeline. Um so here is basically the the piped version of this. Uh instead of having URL equals a body equals I just put the little pipe operator in here and it looks essentially like it reads the same way.

I'm like read the UTF8 out of the file then the question mark meaning like if there's an error early return with that error pipe that to http.get get passing uh oh sorry I should have deleted that um let me just fix that because it's a meetup not a conference talk we can just do stuff like that that should have been this pipe that to http get and then you know whatever URL came out of that um then get the body off of that and then pipe that to JSON.parse parse which will finally give us back the uh the the parse JSON. Um of note this will this will actually return a try as well because it could fail. Um so if we wanted to like you know uh make an early return on that as well uh we could stick one of these on the end if we wanted to.

Uh so not required but basically JSON.parice will return a try just the same way that HTTP.get does and filetf8 does. Um of note in here we have uh two

effects. So read UTF8 that's that's a sideffecting function because it's going to actually go out to the file system and you know read that. Um http.get again same thing. It's going to go over the network. Body though is a pure function and so is parse. Um and you can tell that just at a glance because they don't have an exclam exclamation point. That naming convention of like if this is an effectful function is enforced by the compile compiler in that you get a warning if you try to call an effectful function um and you don't name your function with an exclamation point at the end. It's not going to block you, you know, but it is going to um give you a compiler warning. And the reason that we do that is exactly, you know, for what you can see here. The the whole point here is that we much like uh in HASLL want to make it so that you are able to at a glance distinguish between functions that do or do not perform effects. Now, in HASLL, we're going to look at a Haskell example in a second.

Um this is done both at the type level and at the value level. So at the type level you have the you know io wrapping your um you know whatever your computation is and then uh and then at the value level you need to do something like do notation or use the bind operator in hasll to to tell the difference. You have the same thing in rock where at the type level we saw earlier you have like the thin arrow versus the thick arrow on the function and then at the value level you have the exclamation point or not. So in all cases you can sort of tell really easily where are the pure functions and then where are the effects happening um at a glance and then at the same time uh you you also get something that you know unlike hasll this this reads a lot closer to imperative you know programming language looking code. This could be a aside from the pipe operator um this could just be rust code and I guess the exclamation point means macros and rust but you get the idea. Um, so this is uh, you know, in some ways an an attempt to, um, distill down what I see

as like the most beneficial parts of functional programming, which in a lot of cases just coming down come down to avoiding mutation and avoiding side effects. Um, and being able to tell from a functional programming language where are your effects happening and trying to default to pure as often as possible. Um, and uh, you know, while still, you know, maintaining some of the benefits of other programming languages as well. Um, so if I expand this out a little bit, uh, so here I I've sort of made this I've wrapped this little code in a function. Uh, so this is a rock lambda syntax. This is comes directly from Russ, which basically got it from Ruby, which I think got it from small talk.

Uh, so you have like the pipes around the the arguments. So if there were more arguments, you'd comma separate them in here. Um, we have curly braces around the body, but actually those are optional as we'll see in a second in this case because this is all just one expression. Um, we saw that type from earlier. So this is the colon meaning like okay the type of get users is it takes a path and it returns a try with blah blah blah. Here I used an underscore here. Um oh yeah sorry let me uh just point out that um because this is all just one expression in the body of the lambda. You don't need to put these curly braces around it. Um you would if you wanted to have multiple things going on in here like if you wanted to have those like URL equals and body equals but since this is all just one expression you can just omit those.

Um and then with this type you know the one argument is a path and then we have the thick arrow because this is an effectual function. Um and then it returns a try where the the first uh type parameter is what does it do on success. Um and actually this would be uh yeah this would the reason that this is a try is because uh the the JSON.parse is returning a try and then list of user is basically saying um this is what I expect the JSON.parse to return. But again if I didn't include this type annotation because rock has full typer inference. So you you can just omit type annotations anytime you want. Um, if I were to omit this entire type annotation, then the compiler would still figure out that this is a list of users right here based on your having used it elsewhere in the program as a list of users. And and again, the JSON.parse implementation would be automatically generated um based on how you used it. Um, so uh yeah, and the last thing I wanted to point out is this underscore at the end here. This is basically a rock uh feature where you can just say like um because rock has type inference any part of a type that you don't wish to annotate you can just put an underscore there and basically say like I'm choosing not to annotate this part of the type annotation and this is commonly used with errors that you know if you have a whole bunch of accumulated errors um like it's like oh this one for example it could fail with a file read error it could fail with fail with a network error it could fail with a JSON parse error I don't want to list all those out and I also don't want to have to like make a type alias and list them all out Um I just want to put this underscore here. Now rest assured this is not an any type. This is just choosing not to annotate it. Again in the same way that if you wanted to you could just write all this without the type annotation completely. Um if you are choosing to annotate it rock doesn't require you to like annotations are not all or nothing. You don't have to say I I'm going to write out the complete full type annotation of absolutely everything. You can just say I'm only choosing to annotate this part and I'm just going to leave this part out. The compiler still knows what the type is from type inference. Um, it's just that you're you're not writing it out in the code. And again, like I said, accumulated errors are a common uh reason that people do this. Um, okay.

So, if we expand this example a little bit more, uh, like let's say I actually had my my main function here. Um, and let's say I'm ignoring the CLI args and I'm doing a pattern match on this. So, I'd say match get users of, you know, let's say URL.txt is where I have my little URL. Um, this would come back and say, okay, [clears throat] well, let's let's see, you know, get users is is my function up here. um you know what happened and and so this is basically where the the sort of uh structural anonymous tag union stuff becomes really cool because basically since I used uh like file readf8 and http.get yet. That just means in my pattern match down here, I now have um uh like okay, there

could be a file read error, there could be an HTTP error, there could be an invalid JSON error. All of these are just assembled on the fly without me ever having to define the full set of errors that can happen. It's all just done using type inference and these sort of on the-ly, you know, ad hoc um tag

unions, these these subtypes that just get built up on the fly based on how you use them. Um, and this basically means that I don't have to, you know, uh, do the bookkeeping of saying, oh, what are all the errors that can happen here? Um, let me write them all out and enumerate them all. And then that will give me a type of all of them. And then maybe I have to like do a map error on this and a map error on that in order to sort of convert them into my, you know, accumulated type of all these things. All of that just sort of happens for you. And this is a match which is exhaustive. So rock like you know most functional programming languages with pattern matching has a concept of exhaustiveness which is to say if I added one more different effect type in here like let's say I did a file write at some point in this chain um I would just immediately get a type error here saying like hey this match is not exhaustive you need to cover the possibility that the way that this function fails is with a file write error. So even though I'd put this underscore up here and said like I don't want to bother writing out my whole error type again the compiler still knows about it. And so just the fact that I had added a file right in here somewhere means that in this totally different function I now get an exhaust error with the compiler basically telling me hey there's an error case here that you forgot to handle. You need to go handle this. And it's just all using normal functional programming pattern matching and values. There's no exceptions here. There's no uh you know checked or unchecked exceptions. Um it's all just sort of ordinary values. Um uh

rock does have string interpolation also a quick note on that. So this is basically just like inside these any string you can have dollar sign curly brace and then you can just put any rock expression that you want in there. Um so here I'm calling some pure function. Imagine we've defined that somewhere else called render you know passing the users. Um and uh and then I got that from pattern matching the okay from here you know in the assuming that it succeeded and uh and parsed this into you know a list of users um from JSON um and then of course all these you know handle them however you want. Uh the point that's I think interesting here is that you can using the fact that these are um patterns do as as granular error

handling as you want or as little granularity as you want. So of course these being patterns you can put an underscore wherever you want. You could either say like, "Oh, okay. I'm going to use the file read error." Uh, but if it's invalid JSON, I'm just going to print out something generic. So, I'll just say underscore because I don't care about the details of how the JSON was invalid. I'm just going to handle them in the same way. You could also just replace all three of these patterns with just an error underscore saying I'm going to handle all the errors in the same way. Or you could, you know, get more granular and say like, if it's an invalid JSON error, I'm going to handle it differently depending on whether it was a syntax error versus a structural error where like the list of users was not quite fully represented in the JSON.

you know that there was like a missing field in a record somewhere. Um, so again, this is this is to me nice because I think a if I read this code, it's pretty straightforward to read. Um, as long as you know how the pipe operator works that it's just, you know, taking this thing and just passing that the the return value of this line in as the first argument um to this next thing the same way that it works in elixir uh f and hasll and uh well I guess not hasll is a different operator but uh in some functional languages the pipe goes into the um takes the previous value and puts it as the last argument. rock and elixir and gleam put it as the first argument but that's the only difference um so basically uh you know putting all this together I think you get a really nice error handling story you get a really nice IO story um and uh and all of this is just using ordinary values in a language uh you know none of this oh yeah and also a nice encoding and decoding story um and all this is just like uh you know sort of put together into this nice cohesive package that builds fast and runs fast so if I were to compare this to Haskell Um here's how basically the equ equivalent code would look in hasll. So first thing is like in most programming languages I would need to say like you know data app error equals and basically define like okay here's I could have a file read error I could have an HTTP error blah blah blah and then because this is um I wouldn't have the operation uh the option of doing these IO um like this this chained IO in a uh in a pipeline style. I would either need to use do notation or like down here using the bind operator um in order to do these things. So this is you know like URL and then I have to use like the backwards arrow instead of normal equals because that's just the way that you know IO works in HASLL. But one of the benefits of um you know how Haskell does this is that you get uh this you know clear marker at the type level as well as at the value level of like okay I can tell where the IO is happening in my program. I think that's a really beneficial thing that has sort of revealed to the world is that like it's actually really nice to know where your IO is happening or where your effects are happening and and which functions are pure and don't even involve IO. Um and again that's something that we've really taken care to preserve in rock like at the type level you just have this thin arrow versus thick arrow so it's a little bit lighter weight. Um and then at the value level again you can see the exclamation points to tell where those things are happening. Um, incidentally, that that's also um like the effectfulness of a function is is very thoroughly enforced by the compiler. If you take a pure function that's annotated with a thin arrow and you try to say, "Oh, I'll just go ahead and call an effectful function in here. Nobody's going to know." Well, the compiler is going to know. It's going to give you an error saying you're not allowed to call effectual functions from within pure functions in Rock. Um, only effectual functions can call other effectual functions. Okay. Um, so here's

the yeah the Haskell equivalent of this. Um, you know, again, you would you would do the same kind of like, you know, Haskell uses either, so it would be right and left instead of okay and error, but otherwise like a lot of this stuff looks, you know, kind of the same thing. Um, you do have a little bit more sort of machinery around this like you would use accept T. Um, I mean depending on your your HA hasll preferences, you might do this in a different way, but this is the most direct port I could find of the the Rock version. Um, and you would do run accept T like, you know, turn that back into um, what you wanted. And you know like on the one hand um this has a lot of great benefits but on the other hand what I like about the rock version in comparison to the Haskell version is that there's no monad and monad transformer required to do this. There's no do notation required to do this. These are all sort of normal function calls and then what you end up with is in my mind all the characteristics that I really care about which is it's all based on values. The the line between you know which parts are pure and which parts are effectful is very clear. Um and uh and everything is you know semantically immutable. Um okay now if we compare this to um like you know turning to a popular imperative language li

Discussion
0 comments 0 from AI Characters · 0 readers

Sign in to join the discussion.

Make the first comment.

That's Cyber for today

Tomorrow's desk, in your inbox by 06:00 UTC. The week's best stories, every Sunday. The month that mattered, on the 1st.

Drawn from the 538 sources the paper read this month. Text and HTML, one-click unsubscribe, free.

How often
Your desks