September 25, 2015 Re: Scriptlike v0.9.4 - Perl-like interpolated strings, full examples and more. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky | Am 23.09.2015 um 16:33 schrieb Nick Sabalausky:
> (...)
>
> void main()
> {
> int somevar = 42;
> mixin interp;
> iwriteln!("This is ${somevar}.");
>
> int another = 17;
> iwriteln!("This won't work, using ${another}.");
> }
>
> Seems like it would be too awkward and confusing to be worthwhile. :(
>
True, it's still far from being good. There is another possible syntax variation:
mixin template iwriteln(string str, int line = __LINE__)
{
auto __dummy_/*~line*/ = () {
import std.stdio;
// pretend to parse the input string
write("Result: ");
write(result);
writeln();
return true;
} ();
}
void main()
{
int result = 42;
mixin iwriteln!"Result: #{result}";
}
Works only for top-level calls, but has less visual noise, so maybe it makes sense to include that as a convenience function.
|
September 25, 2015 Re: Scriptlike v0.9.4 - Perl-like interpolated strings, full examples and more. | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | On 2015-09-25 02:15, H. S. Teoh via Digitalmars-d-announce wrote: > I wanted to work on it, but haven't actually gotten to it yet. > Basically, the idea is relatively simple: > > // compile-time variant > void writefln(string format="", A...)(A args) > if (format.length > 0) > { > ... // implementation here > } > > // runtime variant > void writefln(string format="", A...)(A args) > if (format.length == 0 && args.length > 0 && > is(typeof(args[0]) == string)) > { > ... // current implementation > } Not sure why you need to complicate it with template constraints. Just overload the function? -- /Jacob Carlborg |
September 25, 2015 Re: Scriptlike v0.9.4 - Perl-like interpolated strings, full examples and more. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Meta | On 09/23/2015 08:42 PM, Meta wrote:
>
> What about even just removing the syntax distinction between string
> mixins and template mixins?
>
> mixin "int i = 0";
> mixin declareI!();
>
I like that idea. It it feasible? I'd always assumed the syntaxes were different because they needed to be for some sort of technical reason. But now that I look at it...maybe that could work after all?
|
September 25, 2015 Re: Scriptlike v0.9.4 - Perl-like interpolated strings, full examples and more. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky | On Friday, 25 September 2015 at 14:38:33 UTC, Nick Sabalausky wrote:
> I like that idea. It it feasible? I'd always assumed the syntaxes were different because they needed to be for some sort of technical reason. But now that I look at it...maybe that could work after all?
At first glance I can't see any problem with it, other than the fact that it makes it somewhat ambiguous whether you're mixing in a string returned from a CTFE function or a template mixin in some cases.
mixin template mixable()
{
int i = 0;
}
string mixable()()
{
return "int i = 0;";
}
However, this currently causes a compile time error anyway, so I don't believe it's a problem.
//Error, two templates with the same name
mixin mixable!();
|
September 25, 2015 Re: Scriptlike v0.9.4 - Perl-like interpolated strings, full examples and more. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | On Fri, Sep 25, 2015 at 02:14:30PM +0200, Jacob Carlborg via Digitalmars-d-announce wrote: > On 2015-09-25 02:15, H. S. Teoh via Digitalmars-d-announce wrote: > > >I wanted to work on it, but haven't actually gotten to it yet. Basically, the idea is relatively simple: > > > > // compile-time variant > > void writefln(string format="", A...)(A args) > > if (format.length > 0) > > { > > ... // implementation here > > } > > > > // runtime variant > > void writefln(string format="", A...)(A args) > > if (format.length == 0 && args.length > 0 && > > is(typeof(args[0]) == string)) > > { > > ... // current implementation > > } > > Not sure why you need to complicate it with template constraints. Just overload the function? [...] It's to work around a dmd bug that doesn't allow overloads between templates and non-templates. But I just checked, looks like that bug may have been fixed since, so now the following overloads would work: void writefln(string format, A...)(A args) { ... } // current void writefln(A...)(string format, A args) { ... } // new T -- Don't drink and derive. Alcohol and algebra don't mix. |
September 26, 2015 Re: Scriptlike v0.9.4 - Perl-like interpolated strings, full examples and more. | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | On 2015-09-25 23:28, H. S. Teoh via Digitalmars-d-announce wrote: > It's to work around a dmd bug that doesn't allow overloads between > templates and non-templates. But I just checked, looks like that bug > may have been fixed since, so now the following overloads would work: > > void writefln(string format, A...)(A args) { ... } // current > void writefln(A...)(string format, A args) { ... } // new 1. "writefln" is already a template [1]: void writefln(Char, A...)(in Char[] fmt, A args) 2. Both of your above examples are templates 3. The easiest way to workaround that bug is to make the non-template function a template function without arguments: void foo()(int a); void foo(T)(T a, T b); But if the bug is fixed, then that's great :) [1] https://github.com/D-Programming-Language/phobos/blob/master/std/stdio.d#L1362 -- /Jacob Carlborg |
Copyright © 1999-2021 by the D Language Foundation