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

[Proposal] Custom literals for string-based objects. #5403

Closed
orthoxerox opened this issue Sep 23, 2015 · 3 comments
Closed

[Proposal] Custom literals for string-based objects. #5403

orthoxerox opened this issue Sep 23, 2015 · 3 comments
Labels
Area-Language Design Resolution-Duplicate The described behavior is tracked in another issue

Comments

@orthoxerox
Copy link
Contributor

A string literal or a raw string literal can be prepended by an identifier:

var element = xml@"<foo>
    <bar/>
</foo>";
var pattern = r"^[[:alpha:]]+$";

This is automatically rewritten to a class constructor or a static function invocation that has a matching attribute:

[StringLiteral("r")]
public class Regex
{
    public Regex(string pattern) //A constructor that can accept a single string argument is required.
    { ... }

    [StringLiteral("ri")] [EditorBrowsable(EditorBrowsableState.Never)]
    public static Regex CreateIgnoreCase(string pattern)
    { ... }
}

If there are two StringLiteral attributes with the same parameter in scope, it is a compilation error.

Potential use cases: simplifying the syntax for regexes, constant byte arrays or UTF-8 encoded strings. If compile-time metaprogramming is ever implemented in C#, the same syntax can be reused for safe XML literals, like in VB.

@alrz
Copy link
Member

alrz commented Sep 23, 2015

Dup of #4971

@dsaf
Copy link

dsaf commented Sep 23, 2015

Actually, it's a duplicate of #263. #4971 was a duplicate itself. I am pretty sure we will see more of these.

@gafter
Copy link
Member

gafter commented Sep 24, 2015

You can already do this using an extension method:

var element = @"<foo>
    <bar/>
</foo>".Xml();
var pattern = "^[[:alpha:]]+$".Regex();

@gafter gafter closed this as completed Sep 24, 2015
@gafter gafter added the Resolution-Duplicate The described behavior is tracked in another issue label Sep 24, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Language Design Resolution-Duplicate The described behavior is tracked in another issue
Projects
None yet
Development

No branches or pull requests

5 participants