Add an optional 'Prefix' parameter to $echo
Addresses Issue #1873 and attempts to resolve the $echo "better prefix" discussion by letting developers choose their own.
Code written against older versions of C3 which do not support prefixes will not be affected/broken because the prefix is the second (optional) param, i.e. $echo becomes $echo MESSAGE[, PREFIX] ;. Thus, messages without a PREFIX will still output the typical ] prefix.
The only notable constraint at the time of submission is that surrounding the two constant expressions with parentheses (see below) will not compile properly.
module main;
import std::io;
macro beacon($msg)
{
$echo $msg, @sprintf("[%s:%s]> ", $$FILE, $$LINE);
}
fn void main()
{
$echo "Old-school, backwards-compatible";
$echo("simple, still works");
$echo "example,test", "PFX>>> ";
// DOES NOT WORK: $echo("parens", "<<< WITH >>> ");
beacon("It's this location!");
io::printn("Hello, World!");
}
$ ./build/c3c compile-run /tmp/tmp.YHrcPSCpdl/hw.c3
] Old-school, backwards-compatible
] simple, still works
PFX>>> example,test
[hw.c3:18]> It's this location!
Program linked to executable 'hw'.
Launching ./hw
Hello, World!
Program completed with exit code 0.
Not sure about what I think of this solution, but the parens not working is because the $echo never needs parens anyway, just like the $for and $if, the parens are only used for operator precedence.
So something like $echo ("Hello"), ("prefix> "); works, I think.
@BWindey Thanks for the feedback, and makes sense about the parentheses.
macro @echo($message)
{
$echo $message, "$echo: ";
}
// ...
@echo(@sprintf("Test message on line %s", $$LINE));
$echo: Test message on line 12
Something like this for those who want to customize a prefix is an easy solution, without settling on one option. It only adds a slight overhead to the builtin but resolves the "what should it be" problem for everyone who cares. Those who don't care can just keep $echoing as they do now. :smiley:
Anything else is too complex IMHO.
An alternative is to make it configurable as a build parameter: --echo-prefix myecho
An alternative is to make it configurable as a build parameter:
--echo-prefix myecho
I think that's a great idea. Would this still work if someone wanted to use a prefix that includes tokens like $$FILE?
It could I guess, although that is more work.
It could I guess, although that is more work.
I suppose the original proposal is perhaps a more flexible option (whatever that gains C3).
But I'm willing to help with an implementation for either option/both; as no matter what is chosen, something extra on top of $echo would need to be configured. Whether that's a cmdline param or an extra macro.
I added a simple --echo-prefix build option now. It can definitely be improved on. Currently it supports {FILE} and {LINE}
I added a simple --echo-prefix build option now. It can definitely be improved on. Currently it supports {FILE} and {LINE}
Nice! We can close this in favor of your changes if the branch should be axed. Thank you.
Thank you!