Expansive
Expansive copied to clipboard
.ToString() should be called automatically
First off, this is a great library. Thanks!
One major (and easy to fix, I presume) nuisance is that we can't pass non-string objects into the .Expand() function.
For example, this breaks at run-time:
int age = 5;
"I am {age} years old".Expand(age);
instead we currently have to manually call .ToString():
int age = 5;
"I am {age} years old".Expand(age.ToString());
If you could change .Expand to take params Object[] args, and the just call .ToString() automatically on each parameter passed in, it would avoid us having to do it manually everywhere.
Thanks!
Thanks for posting this. I can certainly look at making this change. In the meantime, since Expansive simply gets installed as a single .cs file in your project, you should be able to make this change on your end until I can update the NuGet package.
Thanks again.
I went to go ahead and make this change, but it looks like the signature I would use is already in use:
public static string Expand(this string source, params object[] models)
And I'm not really sure what this "models" function is used for?
Basically I was just going to have public static string Expand(this string source, params object[] args) call .ToString() on each of the args and forward it off to the public static string Expand(this string source, params string[] args) function.
See here: https://github.com/anderly/Expansive#advanced-multi-model-string-templating
So, for now, an easier approach (using your example above) might just be calling
int age = 5;
"I am {age} years old".Expand({ age: age });
I'm going to look at simplifying expansions with simple primitive types to handle your use case.