Skip to content
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

Language support for Interpolated Strings #83

Open
foobit opened this issue Jul 3, 2020 · 2 comments
Open

Language support for Interpolated Strings #83

foobit opened this issue Jul 3, 2020 · 2 comments

Comments

@foobit
Copy link

foobit commented Jul 3, 2020

Really neat language! Would adding support for Interpolated Strings be difficult?

Building strings can get wordy using the + operator (pun intended):
"The area of interest is bounded by " + p1.X + "," + p1.Y + " and " + p2.X + "," + p2.Y + "."

IMO, having the values inlined instead of at the end (like string.format) makes it easier to read.

Possible suggested styles:

C#6

$"The area of interest is bounded by {p1.X}, {p1.Y} and {p2.X}, {p2.Y}"

Javascript

'The area of interest is bounded by ${p1.X}, ${p1.Y} and ${p2.X}, ${p2.Y}'

Swift

'The area of interest is bounded by \(p1.X), \(p1.Y) and \(p2.X), \(p2.Y)'

@Rohansi
Copy link
Owner

Rohansi commented Jul 3, 2020

Hey, thanks! Agree that it'd be awesome to have. Don't really have a preference for syntax but I do like JavaScript's tagged templates.

Implementation shouldn't be too bad - the most complicated part might be the lexer changes required. It'll need to save its state upon reaching ${ and then return to being a string when it reaches the matching }. And handle nested string interpolation, of course!

If you're up for it just let me know if you need help with anything.

@foobit
Copy link
Author

foobit commented Jul 3, 2020

I agree tagged templates are very nice. However, I can't see myself using them for everyday strings. I'm testing Mond using lots of user facing strings.

A related use case for me is for localization. Positional arguments string.format is helpful, but named arguments would be better. However, that is kind of out of scope for the base language. Having, good string handling is key for writing higher level sentence generation.

I'll try implementing one of the basic interpolation strings first to learn the internals of Mond. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants