Hacker News new | ask | show | jobs
by zhodge 3996 days ago
Hm, at first glance I am as well, especially with tagged template strings. Those look particularly simple but powerful.

Generally though, is it reasonable for features such as these to be included at the language level? I primarily use ES5 JavaScript so I'm used to pulling in a decent amount of modules (in this case handlebars, underscore, etc.) for things like templating.

But I will be relieved to have a language-standard solution to what feels like such a common problem for the environments in which JS is used primarily, despite the tradeoff that's made in accessibility due to feature bloat.

4 comments

Yes, for at least two reasons:

Unsafe string-munging is too easy and seductive. Doing things the safe/secure way needs to be at least as easy.

And it makes for a growable language, the way Lisp macros do. E.g. https://github.com/erights/quasiParserGenerator This can reduce the demand for feature bloat in future language standards.

> for things like templating.

I'm excited for not having external libraries to handle string interpolation.

I too was reluctant of using many of the new ES6 features, but as I've begun to adapt them I've come to appreciate them a lot! In fact, when using Babel to compile ES2015/2016 you can in many cases rid yourself of things like Underscore/lodash[1], Promise libraries, etc.

I've recently started a new project in React/Flux, and I've cut away 3 or 4 dependencies compared to previous projects by fully embracing ES2015/2016, as Babel automatically provides the necessary polyfills and compilation.

As for template strings, I think they make sense. Most programming languages have them in someway or another, like Ruby "hello #{world}" or Python 'Hello {world}'.format(world='World').

[1] https://www.reindex.io/blog/you-might-not-need-underscore/

lodash has ~200 modular methods. That means it covers much more ground than the handful of ES5/6 built-ins provided and because it's modular you can use what you need without the stuff you don't.
Most modern languages already have this feature, JavaScript's the strange one to not have a quick way of putting variables into a string and the first to have to name it though because it was missing it for so long. And by your comment have evidentally chosen a confusing name. This isn't to replace templating engines.

Admittedly you could trivially extend the string prototype to have something more akin to string.format or printf, which was something you couldn't do in other languages for a long time, making it less of a problem.

And still you cant 'lazily' store string vars without evaluating.

In python you can save a string like: template = '%s is %s' and _then_ evaluate its output: print template % ('john', 'tall')