Template Strings #2216
Replies: 14 comments
-
C# already has verbatim interpolated strings. Your example seems to just be a raw string, though, and has nothing to do with interpolation. |
Beta Was this translation helpful? Give feedback.
-
You can achieve something similar of what I think you want with the following code: public static string Template(Func<IEnumerable<string>> func)
{
var builder = new StringBuilder();
foreach (var item in (func ?? throw new ArgumentNullException(nameof(func))).Invoke())
builder.Append(item);
return builder.ToString();
}
void main()
{
IEnumerable<string> _str()
{
var i = 0;
while (i <= 5)
{
yield return i.ToString();
yield return Environment.NewLine;
}
}
var str = Template(_str);
Console.WriteLine(str);
} |
Beta Was this translation helpful? Give feedback.
-
Maybe I was not clear enough: I know about the string interpolation feature in C# the new feature is about string templates: """ """ this is the operator in Kotlin and it allows for multi line strings and expressions (if,for,do, lambdas, method calls etc.) right in the string. I think this would be useful to be able to generate code. |
Beta Was this translation helpful? Give feedback.
-
Your mention about string interpolation is what makes this unclear. Kotlin only allows expressions in string templates, just like C#. You can't have assignments or loops. Your example produces a String that happens to contain that Kotlin code, it doesn't execute anything. If your proposal is about producing a string that contains such code and has nothing to do with interpolation, then see the following proposal. It's based more on C++'s implementation of raw strings rather than Python's or Kotlin's, but the premise is the same. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
I'm not sure what what's supposed to be? That doesn't look like Kotlin syntax. What language lets you use guillemets for interpolation? |
Beta Was this translation helpful? Give feedback.
-
Yes, you're right: the blue text is string output as it is, the « character is the equivalent of { in c# interpolated string the » charcacter is the } in the c# interpolated string. The purple colored text is part of an instruction or expression as reserved word. The last picture shows a function definition with an implicit return statement of the evaluated string template. The srting template is started with (3) ''' and also end with (3) ''' anything that is enclosed in « » will be evaluated, it's like a mustache template in javascript. Can such a feature go in C# ? The language is an extention of java called Xtend. |
Beta Was this translation helpful? Give feedback.
-
You can use interpolated verbatim strings: var message = $@"
Hello {name}!
"; You have to escape double-quotes by doubling them up, but otherwise it's a raw string. Combining raw strings with templates seems like it would defeat the purpose as you end up having to escape something. |
Beta Was this translation helpful? Give feedback.
-
I can't see how this would help with not having to split and concatenate the string over and over to keep things readable when there is a need to have an output value for a logical evaluation or loop iteration for multiple values. It it's not about not being able to have the result but about how easy and readable it is. I still believe this would be good feature to have. There are many situations when code generation can be used to shorten the time and possible errors of repetitive code. The posted example dose not support statements evaluation of any kind: if, while, for. Thanks anyway for all the answers. |
Beta Was this translation helpful? Give feedback.
-
I can't imagine that such a use case would be very common and it sounds like it would be quite complicated to implement, especially supporting arbitrary block of code within the string. It's basically rebuilding the MVC razor engine within the language. |
Beta Was this translation helpful? Give feedback.
-
I don't know if this is widely applicable enough for integration into C#. Or even appropriate given the current landscape: generally the industry has moved away from putting templating directly into code, and instead pushed it out to templating frameworks like Razor. This gives a better separation of concerns. (Regardless, if you want to pursue this I encourage you to edit your original comment to be clearer -- I had to read down to your latest comment to really understand what you were after.) |
Beta Was this translation helpful? Give feedback.
-
The special thing about the strings in Xtend (in the screenshots) is actually the greyspace. Such a string generates a string builder in java which is aware of indentation. In that way It is different than For Xtend it makes sense to have such a construct, because the initial purpose of the language was to generate code from a domain specific language. To have the same with Kotlin or groovy you would call stripMargin on the string. It would be a cool addition to C#, but only really beneficial if there is IDE support for it like I'm Xtend. Here is the info from their docs (scroll down to whitespace handling) |
Beta Was this translation helpful? Give feedback.
-
This makes me think of Razor. |
Beta Was this translation helpful? Give feedback.
-
More like T4 Text Template rather .. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
In Xtend a java based language (transpiles to java code) there is a feature called string templates that allows easy code generation with logic like javascript mustache template engine but only in the java context and looks like this:
It is often a case when you need to generate class models or other type of repetitive code (even GraphQL entites and not only) that is why I thought this to be a good feature because it can use all the context of dotnet framework and custom referenced dlls.
https://www.eclipse.org/xtend/documentation/203_xtend_expressions.html#templates
T4 text template - was a similar tool but now it is not supported in dotnet core
INITIAL OLD:
I find very useful the string interpolation feature and I was thinking even more useful would be a string template feature like Kotlin has:
It would be of great help in code generation.
Beta Was this translation helpful? Give feedback.
All reactions