I though he was going to implement full blown call/cc , or import setjmp/longjmp , but a try/catch is essentially that, setjmp/longjmp. Use this knowledge with care.
This is even better because your labels are runtime values, so you can compute goto destinations at runtime via string interpolation or such, making it even more readable.
Also, you can have multiple labels with the same name, meaning the same goto call will jump to different labels depending in which block the goto is (since you do a linear search through the list), increasing maintainability.
GNU C extensions have runtime-evaluated labels. But GNU C is mostly heresy (despite being actual better version of C, not like C++), so be carefull and put ifdef __GNUC__ everywhere.
@@FastNotSave A goto call should always jump to the same instance of a label (the first one in the list) as it iterates through all blocks from the beginning, not from where it was called.
@@samgiz it would be a trivial change to make the iteration looking for next label start from the previous block being executed (and loop back to start again if not found on rest of list), in which case you really could have multiple identical labels, as the guy with headphones wrote, drastically increasing maintainability and code scanability
Holy moly! I last watched your videos 2 years ago, and there were so much ”more” of you then. Nice! Looking good. Your content is still top notch as usual.
19:10 You have optional arguments with default values. E.g. `let goto_block ?(name = None) ...` and call it `goto_block ~name:(Some "foo")`. Mutual recursion is possible by chaining the function definitions with `let rec foo = ... and bar = ...`.
I think that function used as an intermediate step, to control where to "jump" execution, is usually called a "trampoline". I've seen it used similarly to implement things like tail call optimization in languages that don't have it, like Javascript XD. Which seems like a completely different thing at first sight, but it¡s actually related. Tail calls behave as a jump or goto, and that's why they can be eliminated in the first place.
beautiful control flow, I didn't ever wonder before if goto could be implemented in any language teachers only resort to saying that goto is garbage but you come up with this beauty of state machine
You either die a hero, or live long enough to see yourself become the villain. That was the comment I would put while watching the first part of the video. I changed my mind when the irony and meming ended. I gotta say that this is the best video you uploaded in a while in my books. The former Tsoding (the real chad imo) would be proud of you.
I tried to do it myself before watching. I defined a binding operator that does the try-with, but you can't jump forward this way (I'm still hacking it I have some ideas). Also I think it can be done by defining labels as mutually recursive functions that tail-call each other. My half working solution looks like that : let* loop = make_label () in goto loop; goto exit; (* Doesn't work since exit is not defined *) let* exit = make_label () in ...
coming from c#, isn't there a conditional catch? you could divide the blocks in multiple catches each one for a label, and then throwing you owuld go in that 'case', like a sort of switch
If you install utop you can run OCaml REPL inside Emacs using C-c C-e to eval function and C-c C-b to eval buffer just like emacs lisp. Makes developing much easier.
I recently installed OCaml to get some project that someone else wrote to compile so I could use it. I learned a little bit of ML like 20 years ago and knew Caml and OCaml existed, but never used them. Your video hasn't changed my mind on goto, but it has changed my mind about even getting into OCaml.
Your approach of skipping blocks that shouldn't be executed reminded me of the same approach being used to implement loops in "M/o/Vfuscator", a C compiler that generates only mov instructions.
I've noticed that you always use Emacs for your workflow, btw superfast. My question: do you use it cause is really highly efficient in comparison to others modern IDEor text editors. I'm aware that this is a controversial area, but as an amateur programmer, is very confusing and I'd like to know professional opinions. Many, nowadays, are commenting that VScode is the replacement of Emacs. Do you agree? Emacs is obviously more powerful but young generations are not sticking to Emacs community. Thanks for any insight
Impresive but... try catch is a goto. Also := is from Algol, and i don't know wich is better, := for assigment or == for comparision, first is less error prone
I learned some lua for configuring neovim recently and was surprised to discover that in lua 5.2 they actually added goto to the language. Does that make lua the chad-est dynamic language now? Kind of ironic since they don't even know the only true correct number to start counting from, but then again, would a true chad care about that?
I have a hard time understanding tsoding's sarcasm. Most things just whoosh by me. Are people scared of for loops? Do students use recursive? What is soycaml? ~$ man tsoding-jokes
I see one possible improvement and two problems. If you change line 16 to also [] -> throw Goto entry than you have even some kind of interfunction jumps. The problem that arises from that is what happens if you try to jump a Label that is not defined and what if the user catches or throws Gotos themselves. But the bigger problem I see with this approach is to have the jumps being calculated at runtime. That kind of violates the advantage of goto in my eyes because of the huge Overhead beeing generated. But I am no programmer and don't have any education in this field! But I love your unique apoaches to everything you do. Keep on comming up with such interesting ideas. I hope you can keep making content and wish you the best of luck.
Language which does not have a goto is a crappy compilation target - and, therefore, a crappy metalanguage. You cannot compile parsers efficiently, for example, if you don't have a decently fast goto.
I love your content mate. This is one of the few channels that provides based HQ content for C chads. Nowadays, the majority of YT tech. content creators 're making garbage for JS soydevs: ""x Is A gAmE cHaNgEr!" "sToP uSiNg y! uSe ThIs wheelReinvented.js nOdE pAcKaGe InStEaD!"
"Today i woke up and chose the violence" and that's the story of how i subscribed and that was before the "double dick operator" which is a beautiful name and an excellent way to ensure nobody ever forgets it lol
Rust was initially implemented in ocaml, and it takes several details from ocaml including a lot of it's naming, but it deliberately draws from a lot of different types of languages.
call/cc preserves the execution context, it saves the state of the stack, no? This is a trampoline, it behaves similarly, but without preserving the state of the program, it only jumps to where it's told.
chaddest solution: ```hs main = replicateM_ 10 (putStrLn "Hello World") ``` i thought you'll use delimited continuations, but your solution is way worse! congrats! i suggest you look into continuations, delimited continuations, continuation passing style, and defunctionalization!