-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
String Interpolation #968
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
There are a few problems that need to be addressed before this could ever become a thing:
Zig tends to avoid adding "fluffy feel good once in a while features" because, as you said, it wants to be simple and less bloated than most alternatives. I'd say, that if features only reason to exists is "Eeeeh, why not." then I don't really think it is worth adding. Though, I will say that There is, however, a few things hidden in this proposal that should probably be discussed (either here or have their own proposals):
|
One other note! Did not think about only allowing this at |
Been meaning to reply, just forgot sorry. I actually completely agree, and I don't really see this in 'Zig' the more I think about it; however I still felt it was worth creating the issue just incase others had opinions :). Also means its a place to drive people who ask this in the future. On your other points, I agree that referring to objects is quite nice; printing different format strings at runtime is extremely important too, though I think that is probably the place for a library to come in? Since it is quite a niche place, however I may be wrong. And yes it was a complete comptime feature so that's why I didn't mention allocators :). But I actually don't think it's a need or even a want really, I think Zig is nice enough already with printing things. |
👍 |
I couldn't find any issue that matched this, so I felt this should be created.
Most languages have a nicer way to do a format then having to do something like
format("{}", myObj)
, often they either take the 'Swift' route and do something like"\{myObj}"
with the\{
being existent in all strings; I personally don't like this as it does seem to do 'stuff' behind your back without you asking for it.Whereas the 'C#' way which is something like
$"{MyObj}"
, requires a symbol to designate that it is interpolated, I feel that this way is nicer in terms of usability and demonstrates clearly if a string should be interpolated you can write '{' by double writing it like{{
kinda like escaping it; we could also allow escaping it via the typical escape keyword\{
but that may be too similar to the first method to confuse people.The third way is I guess more inline with the fact that Zig doesn't really use symbols for things like this and uses 'words' (i.e. transition from
%
totry
) so I guess it would be something likefmt "{MyObj}"
which would just translate toformat("{}", MyObj)
.Why have this?
It helps when you have long format strings as you can easily prevent weird bugs of putting things in wrong orders (though those bugs are typically caught easily). I don't have too many reasons for why they really are 'needed' so I wouldn't say that this is a necessity but it's one of those things I find that most people just go 'huh that's nice', kinda like how the 'for' loop in zig is not necessary but it just makes things nice.
On the other hand the negative of having it is 'bloating the language' (something I disagree that this would do but I can understand this view), you could also argue it is doing stuff 'behind' your back, but I would argue it is not doing anymore than what something like 'try' is using.
😄
The text was updated successfully, but these errors were encountered: