Chris Lattner
๐ค SpeakerAppearances Over Time
Podcast Appearances
And so you have...
Function one calls function two, calls function three, calls function four.
Along that call stack, there are try blocks, right?
And so if you have function one calls function two, function two has a try block, and then within it, it calls function three, right?
Well, what happens if function three throws?
Well, actually, start simpler.
What happens if it returns?
Well, if it returns, it's supposed to go back out and continue executing and then fall off the bottom of the try block and keep going and all's good.
If the function throws, you're supposed to exit the current function and then get into the accept clause, right?
And then do whatever code's there and then keep falling on and going on.
And so the way that a compiler like Mojo works is that the call to that function, which happens in the accept block, calls the function, and then instead of returning nothing, it actually returns a variant between nothing and an error.
And so if you return normally, fall off the bottom or do return,
You return nothing, and if you throw an error, you return the variant that is, I'm an error.
So when you get to the call, you say, okay, cool, I called a function.
Hey, I know locally I'm in a try block.
And so I call the function, and then I check to see what it returns.
Aha, if it's that error thing, jump to the accept block.
Exactly.
And so the compiler does all this for you.
And, I mean, one of the things, if you dig into how this stuff works in Python, it gets a little bit more complicated because you have finally blocks, which now you need to go into, do some stuff, and then...