<div dir="ltr">It'd be trickier, but you could imagine allowing the user to declare a custom delimiter for their "verbatim literals", so a Regex type would use a "/".<div><br></div><div>Ruby, for example, has string literal constructs which allow a wide range of delimiters: <a href="https://simpleror.wordpress.com/2009/03/15/q-q-w-w-x-r-s/">https://simpleror.wordpress.com/2009/03/15/q-q-w-w-x-r-s/</a> (and Perl as well, I believe)</div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature"><div dir="ltr"><div>Jacob<br></div></div></div></div>
<br><div class="gmail_quote">On Tue, Dec 8, 2015 at 3:19 PM, Kametrixom Tikara via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I think we can extend this to something more general: VerbatimLiteralConvertible. A VerbatimLiteral would maybe start with a \" and end with "\ (Might not work with these delimiters). It would enable one to not have to escape special chars like " and \, so that they can be used just like that:<br>
<br>
let verb = \"And he said: "\o/""\<br>
let file = \"<br>
First line<br>
"Still in verbatim!<br>
"\<br>
<br>
We could then use this for the Reges type like:<br>
<br>
struct Regex : VerbatimLiteralConvertible {<br>
init(verbatimLiteral value: String) {<br>
...<br>
}<br>
}<br>
<br>
extension String {<br>
func match(regex: Regex) -> Range<String.Index>? {<br>
...<br>
}<br>
}<br>
<br>
let matches = string.match(\"[a-z]"test"\)<br>
<br>
Such a VerbatimLiteral also has the advantage of being able to copy-paste potentially big regex expressions in your code without the need to manually add all those "\" for escaping. Also it can be used to directly copy-past some text with correct line breaks and indentation.<br>
<br>
I'd really love to see this, I'm just unsure of how the parser could delimit such a VerbatimLiteral.<br>
<br>
Kame<br>
<div class="HOEnZb"><div class="h5"><br>
> On 08 Dec 2015, at 23:41, Brent Royal-Gordon via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br>
><br>
><br>
>> 1. The semantics of the =~ operator in other languages maps to a return value of Int? - matching position or nil with no match. This means you would likely need to write if str =~ foo != nil { … }<br>
><br>
> Actually, I suspect the return value would be more like Range<String.Character.Index>?, or maybe even Regex.Match?.<br>
><br>
>> 2. The semantics of the =~ operator produce state around matched groups, which is usually exposed to the language as either thread-local or block-local data.<br>
><br>
> This is hugely dependent on the language and I think it’s pretty obvious that Swift would take a return-value-based approach:<br>
><br>
> if let match = string =~ /”([^\\”]*(\\[\\”][^\\”]*)*)”/ {<br>
> let quotedString = match.substrings[1]<br>
> …<br>
> }<br>
><br>
>> For regex literals, my only concern is that it makes regex a language feature over an extended or standard library feature.<br>
><br>
> Not necessarily. The only thing that *has* to be in the language is support for parsing regex literals, and the only thing that has to be in the standard library is RegexLiteralConvertible. Even if there is a Regex type in the standard library, just that type could be dropped there without requiring huge compiler changes.<br>
><br>
> --<br>
> Brent Royal-Gordon<br>
> Architechies<br>
><br>
> _______________________________________________<br>
> swift-evolution mailing list<br>
> <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</div></div></blockquote></div><br></div></div>