Skip to content

String Interpolation #968

@BraedonWooding

Description

@BraedonWooding

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 % to try) so I guess it would be something like fmt "{MyObj}" which would just translate to format("{}", MyObj).

'fmt' is just a name I chose to demonstrate the idea it is an implementation detail so probably not something to 'argue' about, more focus on the idea.

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.

😄

Metadata

Metadata

Assignees

No one assigned

    Labels

    proposalThis issue suggests modifications. If it also has the "accepted" label then it is planned.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions