Expansive icon indicating copy to clipboard operation
Expansive copied to clipboard

.ToString() should be called automatically

Open deadlydog opened this issue 10 years ago • 3 comments

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!

deadlydog avatar Mar 26 '15 21:03 deadlydog

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.

anderly avatar Mar 27 '15 15:03 anderly

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.

deadlydog avatar Apr 06 '15 16:04 deadlydog

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.

anderly avatar Apr 08 '15 13:04 anderly