If you learn a functional programming language, like Haskell, you get exposed to a bunch of interesting, weird ideas. Most of them are different ways of solving problems that imperative languages already have accepted solutions to, and trying to use them there can feel pointless or janky.
Typeclasses aren’t quite like that. They’re a bit like traits or interfaces, which might make them seem redundant, but they have a deceptive degree of flexibility. Even better, a popular library already uses typeclasses, even if it doesn’t use that word, and I’ve extracted that pattern into this blog post.
This post is more a proof of concept or cool idea than a full library. The actual code to implement typeclasses isn’t complicated, but a robust solution would require a codegen plugin with a lot of edge cases to handle. Still, it’s an interesting tool to have in your kit, and a simple hand-spun version of typeclasses might be enough for your particular use case.
Read more…
What is the type of a function that loops forever, or crashes the program? How do you represent a function that can’t normally be called, or parameterize a type so that some versions of it can’t be created?
One solution is the bottom type, which is one of the simplest and weirdest concepts in programming. It’s closely related to “bottom”, something that is part of nearly every program but rarely shows up explicitly except through bottom types.
While bottom types show up in plenty of programming languages, Haskell in particular has two very different ways of representing bottom. I’ll start by talking about bottom separate from any language, and then move into what Haskell does. I can’t promise that this will be useful every day, but it’s a mind-expanding sort of idea that might help you see new solutions to old problems.
Read more…
Writing compute shaders in Godot is currently a bit intimidating, requiring relatively low-level work for simple operations. The documentation for RenderingDevice, the primary way you setup and run compute shaders, straight up tells you to read the Vulkan guide if you’re confused. I’m writing this tutorial series as evidence that, for simpler use cases, this is usually not necessary. Compute shaders can be tremendously powerful and flexible, and you shouldn’t be intimidated by them.
This post covers reading from and writing to textures. I’ve often found myself manipulating textures via compute shaders and using them on-screen in vertex or fragment shaders. It’s also how you write compositor effects.
This tutorial started by copying the Heightmap Demo, but has simpler examples, more explanations, and some broader context on why it’s structured as it is.
Read more…