2 hours ago · 10 min read1986 words · Tech · hide · 0 comments

This isn’t really that surprising, but sometimes you’ve just gotta point out the obvious stuff, you know? Anyway, Crystal’s global type inference can be easily manipulated into doing arbitrary computations at compile time. Of course you can do whatever you want with macros, but this doesn’t rely on a single syntax macro, and only lightly uses macro methods for convenience. The usage of macro methods can be removed, which I’ll explain at the end. The rules of the game are that we can’t use the macro keyword, and we can only use {{ }} for macro methods sparingly. For a slight extra challenge, we can’t use new either. Not that it would be very useful—it only does something at run time—but the goal is to write code that will do as little as possible at runtime. Let’s start with Crystal type inference. This function returns a String: def get_message : String return "wow this is such a useful function!" end You can tell by the : String after the method name. Crystal’s main trick is that you…

No comments yet. Log in to reply on the Fediverse. Comments will appear here.