-
Notifications
You must be signed in to change notification settings - Fork 72
tagged template literals should be allowed (for completeness) #54
Description
From the current README:
The following are not supported due to a lack of real-world use cases:
optional construction:
new a?.()
optional template literal:a?.`{b}`
constructor or template literals in/after an Optional Chain:new a?.b(), a?.b`{c}`
For this issue I'd like to understand:
- What are the reasons to NOT support tagged template literals?
- If there's no great technical issues with allowing them, WHY NOT support tagged template literals?
For discussion purposes, I'll provide a somewhat silly & simple toy problem-esque example:
/*
Prompt - In as few characters as you can, write a function 'splitOrJoin' that satisfies
two requirements:
1) If a string is passed in, split it and return an array of individual words, for example:
splitOrJoin('split this into an array'); // ['split', 'this', 'into', 'an', 'array']
2) If an array is passed in, join each element with spaces and return the resulting string:
splitOrJoin(['join', 'this', 'into', 'a', 'string']); // 'join this into a string'
*/
// Now your first instinct might be to write something with a ternary
const splitOrJoin = input => input.split ? input.split` ` : input.join` `; // this works
// Or, you could write it slightly differently
const splitOrJoin = input => input[input.split ? 'split' : 'join']` `; // also works
// But then you realize they're actually the same length when removing unnecessary characters
splitOrJoin=i=>i.split?i.split` `:i.join` ` // 43 chars
splitOrJoin=i=>i[i.split?'split':'join']` ` // 43 chars
// Hmm, how else could we shorten it? Oh yeah! There's this new thing called optional chaining
const splitOrJoin = input => input.split?.` ` ?? input.join` `; // also would work
// or the shortened version
splitOrJoin=i=>i.split?.` `??i.join` ` // 38 chars
// There! That's about as short as I can think to make it right now.Now, you might think that code-golf is stupid (and I'd of course avoid using this pattern in production code--or anywhere you'd like your code to be readable and easily understood for that matter); but the fact is: tagged template literals are valid syntax in JavaScript.
Therefore, if you can write i.split` ` I'd also expect you to be able to write i.split?.` ` for the sake of completeness. Hopefully you can see past the ridiculousness of the example to see the reasoning for this.
So I'd like hear some logical argument around the two questions asked in the beginning. What are the reasons to arbitrarily (seemingly) disallow use of tagged template literals in conjunction with optional chaining? If there are, technically, no sound reasons for not supporting tagged template literals, why not allow them just like they're already allowed elsewhere?