Transcript from the "Writing AssemblyScript" Lesson
>> Let's write some assembly scripts, and important to note about these notes that I'm working off of. I made a link back to the repo and the repo should have the completed lesson for every step that we go through. That way, it's easy for me to keep going and keep going.
[00:00:21] And I build up like something by the end it's gonna get a little complex. And I don't want people to become intimidated by missing a step. And then you realize you missed a step somewhere and like none of it works or you're getting a weird error or things like that.
[00:00:35] I really wanna follow the blessed path, the paved road for this, so I've created a. A series of exercises that are all represent different steps of what we're gonna take. So if you're ever confused and lost, you will see this working repo, you can always go back there.
[00:00:50] Clone it and you'll jump right back to where we are, however, as I said before, you learn better when you code yourself. Don't copy and paste, if you can, try to copy paste, try to code things out directly. Build out those neuromuscular pathways by typing and let it run through your arms to your brain and let it absorb.
[00:02:17] And in this case, that just means adding the type references to the variables and the function that we're using. So add a type, we're gonna say, n is always gonna be a 32-bit integer, and this function is always gonna return a 32 bit integer. So if you've never dealt with Typescript before, it looks a little strange.
[00:03:24] We have to be explicit for every single step, implicit computing is it works things like SQL statements. That's implicit you're giving a set of directions, or even HTML, you're saying a set of directions that you want the machine to follow. Versus explicit programming, you're saying do this. If you say, here's a pro tip, go on Twitter right now and be like, hey, what do you think difference we employ a declarative.
[00:03:56] And imperative programming is and then you'll watch that erupt into this giant flame or people to argue about imperative versus declarative. Doesn't really matter to me. All I care about is specificity because we need the specificity to execute our low level programming that is WebAssembly. All right, sorry for that rants, I just, I had to do it, next up is tabs versus spaces.
[00:04:24] We'll argue about that later, all right, so we've now written a bit of assembly script. Now what's left to do is compile it, so I'm gonna run NPM run, ASMR, build, and you don't have to, but you know, it's nice to see it. Let us look at our did I not save, it's let's make sure I don't see anything in here optimize that What is wrong?
[00:04:58] Easy mistake, so I wrote this function in assembly script, but I never exported it. And because assembly script uses a form of it uses ahead of time compilation, similar to C or Java or anything like that. And any strongly typed language is gonna use ahead of time compilation.
[00:05:19] So it catches those type errors before they happen, assembly script does the same thing, it compiles ahead of time. So if there's a function that we write, but it's never accessed, it actually tree shakes those out and it won't actually. It won't create it, and won't use it because it's not being called to why include unnecessary code into your file.
[00:05:38] So we looked at that optimize that lot, and the untouched that what are functions that in there. Why because I forgot to export it, so let's export that function, and then now let's rerun that build. And now we see the minus one function, and if we look carefully, that looks exactly like the example we wrote earlier.
[00:06:06] So now we know the code we wrote in web assembly, last time was good and valid. It's been compiled down to WebAssembly from assembly scripts. And again, I really love this for learning if you're trying to figure out well how to if statements work. How to for loops, work from assembly script into web assembly.
[00:06:26] Writing the code out and compiling it and seeing what the compiler is doing is such a valuable learning tool. So I'm probably gonna keep referring back to these watch files, just because it's really interesting to know what's going on under the hood.