Skip to content

Add an optional 'Prefix' parameter to $echo #2206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

NotsoanoNimus
Copy link
Contributor

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.

@BWindey
Copy link
Contributor

BWindey commented Jun 13, 2025

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.

@NotsoanoNimus
Copy link
Contributor Author

@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. 😃

Anything else is too complex IMHO.

@lerno
Copy link
Collaborator

lerno commented Jun 14, 2025

An alternative is to make it configurable as a build parameter: --echo-prefix myecho

@NotsoanoNimus
Copy link
Contributor Author

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?

@lerno
Copy link
Collaborator

lerno commented Jun 16, 2025

It could I guess, although that is more work.

@NotsoanoNimus
Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants