Skip to content
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

Allow compile-time concatenation of strings #1297

Open
vlstill opened this issue Aug 1, 2024 · 1 comment · May be fixed by #1299
Open

Allow compile-time concatenation of strings #1297

vlstill opened this issue Aug 1, 2024 · 1 comment · May be fixed by #1299

Comments

@vlstill
Copy link
Contributor

vlstill commented Aug 1, 2024

Currently the spec does not allow any operations on string. I think it would be useful to relax this a little bit and allow compile-time concatenation of strings.

  • It allows breaking longer strings into multiple lines in the source code (e.g. for log functions).
  • It makes it easier to combine strings with CPP macros.

I can see two options for syntax for this:

  • The concatenation operator ++ -- this seems quite natural to me -- ++ concatenates bit-vectors, so why not also strings? In both cases the operation is associative but not commutative, so also algebraically it seems to be a reasonable match (unlike + which is usually commutative too). But the type signatures are not completely consistent ((bit<N>, bit<M>) -> bit<(N+M)> vs. ((string, string) -> string) but I think that is still OK.

    "foo" ++ " " ++ "bar" ~~~> "foo bar"
  • An implicit, C-style concatenation, i.e. two literals separated by whitespace are merged

    "foo" " " "bar" ~~~> "foo bar"

I believe the first option is superior, even though the second could be a little more CPP-friendly, but personally I find it quite peculiar.

In compiler, this could be handled by constant folding. It would also impact type checking.

@jonathan-dilorenzo
Copy link
Collaborator

Discussed in the P4 LDWG. Generally supportive of this suggestion. We are planning to go with option 1.

@vlstill will write a PR to support this in the compiler and a suggestion for amending the spec accordingly.

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 a pull request may close this issue.

2 participants