loading...

A win for dynamic typing?

yujiri8 profile image Ryan Westlund ・2 min read

A while back I posted Dynamic typing is a sin, where I tried to show that there are not really any big benefits of dynamic typing, provided the static type system it's being compared to is a good one. I think I might've found one: template systems.

There might not be a way for a static language to implement anything equivalent to Mako (I used to use Jinja as my example before I found out it doesn't actually support arbitrary expressions as I thought; Mako does). I've been trying to use Crystal for a rewrite of a project that uses Jinja, but being a static language presents some issues. If I use Crystal's built-in ECR, the templates will have to be compiled in, and editing them will require recompiling.

There is a project Crinja that tries to implement Jinja for Crystal. But it supports only a very limited subset of Crystal expressions inside template tags. It seems like you can't even do function calls. And basically everything you can do seems to be implemented from the ground up in Crinja rather than just allowing the functionality of Crystal.

So I thought ECR would actually better for my use case than Crinja; it's built with macros, so it allows arbitrary Crystal expressions with compiled templates. As long as I could avoid hard-coding the list of templates (like have it scan a directory at compile time for them), I'd be satisfied. But even doing that is proving extremely difficult or impossible. Though it may just be a limitation of Crystal's macro system. I'd like to hear if anyone's gotten this done with another static language with macros, particularly Rust?

Posted on by:

yujiri8 profile

Ryan Westlund

@yujiri8

I'm a programmer, writer, and philosopher. My Github account is yujiri8; all my content besides code is at yujiri.xyz.

Discussion

markdown guide
 

macros can have a public api like this. It doesn't look too hard.
dynamically choosing templates is also possible.
but I dynamic loading templates is not possible AFAICT.
existing template frameworks like tera use serde, which allows dynamic loading.