feat: allow primitive types when interpolating#1129
Conversation
|
I think this would be the first step to opening up the floodgate for more types to be interpolated! I like the idea that we can allow either So we would have to either:
|
cc0cf1c to
d72b6f5
Compare
|
Earning Adrian his beers @delaneyj 😆 |
|
It's been 84 years... |
|
It's been 84 years... What about Stringer interface? |
|
See my comment above, we need to decide a way forward on that. Go doesn't support interfaces with functions in a generic type constraint. So it isn't as simple as adding We would instead have to open up the interpolation to work with We would either need to just interpolate those and risk interpolating in a way the engineer doesn't expect. Or return a runtime error when a non primative/stringer type is provided (not sure which is worse). |
|
I would also like to support the If we accept https://go.dev/play/p/VqJPlWYmc_S package main
import "fmt"
func main() {
var userA = User{
SecretKey: "super_secret_key",
Name: "Bob",
}
fmt.Printf("%v", userA)
}
type User struct {
SecretKey string
Name string
}Then, we leak the key: In React, if you pass a complex object to a React component, you get a runtime error: import React from 'react';
import ReactDOM from 'react-dom/client';
function App(props) {
return (
<div className='App'>
<h1>{props.data}</h1>
</div>
);
}
var data = {
a: "text"
}
ReactDOM.createRoot(
document.querySelector('#root')
).render(<App data={ data }/>)But, that's a runtime error, rather than a compile time error. At the moment, templ gives you a compile-time error for this scenario, which I think is better. To avoid leaking private stuff, we'd have to do something like this: https://go.dev/play/p/BonrvLUeXEl - at that point, you might even go a step further and have a runtime pluggable registry of functions for displaying types. Then, you'd be able to set a date format globally, or override it on a per-user basis: https://go.dev/play/p/sGll1QFmwQL The tradeoff of supporting stringer is that for the reduced effort for developers (don't have to type I think @joerdav's suggestion here moves things forward. You don't have to type |
|
Another benefit of the above, is that if we do open up to 'any', this should be backwards compatible. |
|
Definitely a step in the right direction. If you did accept |
a-h
left a comment
There was a problem hiding this comment.
Just a couple of nits, but looks good to go otherwise.
|
Think that's an improvement, thanks for the notes! I think I'll add some docs around this too. |
9e0feef to
430d36c
Compare
|
I made a first pass on the docs. @a-h you are usually very good at writing clear documentation, so let me know if you have any notes! |
Fixes #746