Skip to content

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

Closed
@orthoxerox

Description

@orthoxerox

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions