<div dir="ltr"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span class="im" style="font-size:13px">> Yes. Because StringLiteralConvertible inherits from ExtendedGraphemeClusterLiteralConvertible, ExtendedGraphemeClusterLiteralConvertible inherits from UnicodeScalarLiteralConvertible.<br><br></span><span style="font-size:13px">Yes, I know. But I wonder why that is, considering that something that is StringLiteralConvertible will seemingly never use the required initializer from UnicodeScalarLiteralConvertibl</span><span style="font-size:13px">e.</span></blockquote><div> </div><div><div class="gmail_default" style="display:inline">I don't know it </div><div class="gmail_default" style="font-family:georgia,serif;display:inline">either. Maybe it is because when you convert a String to Unicode, you will need the other two init()s. As you examples show, there maybe one or two Ints to make one Character.</div><br></div><div><div class="gmail_default" style="font-family:georgia,serif;display:inline"><br></div></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">let a : Int = "\u{65}" // e</blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">let b : Int = "\u{65}\u{0301}" // é</blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">let c : Int = “hello"</blockquote><div><br></div></blockquote><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span style="font-size:13px">I wanted to know if there was a way to write something like (for example) uscalar”\u{65}”, that would only be a unicode scalar literal, and not a string literal. Or if there was any other way to tell the compiler “please use the initializer from UnicodeScalarLiteralConvertibl</span><span style="font-size:13px">e instead of the one from StringLiteralConvertible”.</span></blockquote><br style="font-size:13px"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span style="font-size:13px">I guess the more fundamental question I wanted to ask was “why must StringLiteralConvertible conform to UnicodeScalarLiteralConvertibl</span><span style="font-size:13px">e?”</span></blockquote><div><br></div><div class="gmail_default" style="font-family:georgia,serif">For "\u{65}" , in your words, you call it a Unicode Scalar Literal. But in formal, it is a String Literal that include special characters.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><p class="" style="background-color:transparent;border:0px;font-size:1.4em;margin:0px 0px 15px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(65,65,65);line-height:20.299999237060547px;font-family:Helvetica,Arial,sans-serif">String literals can include the following special characters:</p><ul class="" style="background-color:transparent;border:0px;font-size:10px;margin:0px 0px 12px 24px;outline:0px;padding:0px;vertical-align:baseline;list-style:none;color:rgb(0,0,0);font-family:Helvetica,Arial,sans-serif"><li class="" style="background-color:transparent;border:0px;margin:0px;outline:0px;padding:0px 0px 0px 1.3em;vertical-align:baseline;list-style-type:none"><p class="" style="background-color:transparent;border:0px;font-size:1.4em;margin:0px;outline:0px;padding:0px 0px 6px;vertical-align:baseline;color:rgb(65,65,65);line-height:20.299999237060547px">The escaped special characters <code class="" style="background-color:transparent;border:0px;font-size:0.85em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(128,128,128);font-family:Menlo,monospace;word-wrap:break-word">\0</code> (null character), <code class="" style="background-color:transparent;border:0px;font-size:0.85em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(128,128,128);font-family:Menlo,monospace;word-wrap:break-word">\\</code> (backslash), <code class="" style="background-color:transparent;border:0px;font-size:0.85em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(128,128,128);font-family:Menlo,monospace;word-wrap:break-word">\t</code> (horizontal tab), <code class="" style="background-color:transparent;border:0px;font-size:0.85em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(128,128,128);font-family:Menlo,monospace;word-wrap:break-word">\n</code> (line feed), <code class="" style="background-color:transparent;border:0px;font-size:0.85em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(128,128,128);font-family:Menlo,monospace;word-wrap:break-word">\r</code>(carriage return), <code class="" style="background-color:transparent;border:0px;font-size:0.85em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(128,128,128);font-family:Menlo,monospace;word-wrap:break-word">\"</code> (double quote) and <code class="" style="background-color:transparent;border:0px;font-size:0.85em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(128,128,128);font-family:Menlo,monospace;word-wrap:break-word">\'</code> (single quote)</p></li><li class="" style="background-color:transparent;border:0px;margin:0px;outline:0px;padding:0px 0px 0px 1.3em;vertical-align:baseline;list-style-type:none"><p class="" style="background-color:transparent;border:0px;font-size:1.4em;margin:0px;outline:0px;padding:0px 0px 6px;vertical-align:baseline;color:rgb(65,65,65);line-height:20.299999237060547px">An arbitrary Unicode scalar, written as <code class="" style="background-color:transparent;border:0px;font-size:0.85em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(128,128,128);font-family:Menlo,monospace;word-wrap:break-word">\u{</code><em style="background-color:transparent;border:0px;margin:0px;outline:0px;padding:0px;vertical-align:baseline">n</em><code class="" style="background-color:transparent;border:0px;font-size:0.85em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(128,128,128);font-family:Menlo,monospace;word-wrap:break-word">}</code>, where <em style="background-color:transparent;border:0px;margin:0px;outline:0px;padding:0px;vertical-align:baseline">n</em> is a 1–8 digit hexadecimal number with a value equal to a valid Unicode code point</p></li></ul></blockquote><div><br></div><div class="gmail_default" style="font-family:georgia,serif">If you want to use a UnicodeScalar, you should use struct <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Reference/Swift_UnicodeScalar_Structure/">UnicodeScalar</a>. Here are the examples.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default"><div class="gmail_default"><font face="georgia, serif">let uScalar = "a".unicodeScalars.first! // 97</font></div><div class="gmail_default"><font face="georgia, serif">print(uScalar.dynamicType) // UnicodeScalar, not an Int</font></div><div class="gmail_default"><font face="georgia, serif"><br></font></div><div class="gmail_default"><font face="georgia, serif">extension UInt32 {</font></div><div class="gmail_default"><font face="georgia, serif"> func hex() -> String {</font></div><div class="gmail_default"><font face="georgia, serif"> return String(format:"0x%2X", self)</font></div><div class="gmail_default"><font face="georgia, serif"> }</font></div><div class="gmail_default"><font face="georgia, serif">}</font></div><div class="gmail_default"><font face="georgia, serif"><br></font></div><div class="gmail_default"><font face="georgia, serif">var aUnicodeScalar = UnicodeScalar(97)</font></div><div class="gmail_default"><font face="georgia, serif">print(aUnicodeScalar.value) //97</font></div><div class="gmail_default"><font face="georgia, serif"><br></font></div><div class="gmail_default"><font face="georgia, serif">aUnicodeScalar = UnicodeScalar(0x61)</font></div><div class="gmail_default"><font face="georgia, serif">print(aUnicodeScalar.value) //97</font></div><div class="gmail_default"><font face="georgia, serif">print(aUnicodeScalar.value.hex()) //0x61</font></div><div class="gmail_default"><font face="georgia, serif"><br></font></div><div class="gmail_default"><font face="georgia, serif">aUnicodeScalar = UnicodeScalar("a")</font></div><div class="gmail_default"><font face="georgia, serif">print(aUnicodeScalar.value) //97</font></div><div class="gmail_default"><font face="georgia, serif"><br></font></div><div class="gmail_default"><font face="georgia, serif">aUnicodeScalar = UnicodeScalar("e")</font></div><div class="gmail_default"><font face="georgia, serif">print(aUnicodeScalar.value.hex()) // 0x65</font></div><div class="gmail_default"><font face="georgia, serif"><br></font></div><div class="gmail_default"><font face="georgia, serif">aUnicodeScalar = UnicodeScalar("é")</font></div><div class="gmail_default"><font face="georgia, serif">print(aUnicodeScalar.value.hex()) // 0xE9</font></div><div class="gmail_default"><font face="georgia, serif"><br></font></div><div class="gmail_default"><font face="georgia, serif">aUnicodeScalar = UnicodeScalar("\u{65}")</font></div><div class="gmail_default"><font face="georgia, serif">print(aUnicodeScalar.value.hex()) // 0x65</font></div><div class="gmail_default"><font face="georgia, serif"><br></font></div><div class="gmail_default"><font face="georgia, serif">// aUnicodeScalar = UnicodeScalar("\u{65}\u{0301}") </font></div><div class="gmail_default"><font face="georgia, serif">// above doesn't work as there are two characters instead of one</font></div><div class="gmail_default"><font face="georgia, serif">// struct UnicodeScalar only conforms UnicodeScalarLiteralConvertible</font></div><div class="gmail_default"><font face="georgia, serif"><br></font></div><div class="gmail_default"><font face="georgia, serif">extension UnicodeScalar:ExtendedGraphemeClusterLiteralConvertible {</font></div><div class="gmail_default"><font face="georgia, serif"> public init(extendedGraphemeClusterLiteral value: String) {</font></div><div class="gmail_default"><font face="georgia, serif"> self = String(value.characters.first!).unicodeScalars.first!</font></div><div class="gmail_default"><font face="georgia, serif"> }</font></div><div class="gmail_default"><font face="georgia, serif">}</font></div><div class="gmail_default"><font face="georgia, serif"><br></font></div><div class="gmail_default"><font face="georgia, serif">aUnicodeScalar = UnicodeScalar(extendedGraphemeClusterLiteral: "\u{65}\u{0301}")</font></div><div class="gmail_default"><font face="georgia, serif">print(aUnicodeScalar.value.hex()) // 0x65</font></div><div class="gmail_default"><font face="georgia, serif"><br></font></div><div class="gmail_default"><font face="georgia, serif">zhaoxin</font></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jan 11, 2016 at 10:08 PM, Loïc Lecrenier <span dir="ltr"><<a href="mailto:loiclecrenier@icloud.com" target="_blank">loiclecrenier@icloud.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">> Yes. Because StringLiteralConvertible inherits from ExtendedGraphemeClusterLiteralConvertible, ExtendedGraphemeClusterLiteralConvertible inherits from UnicodeScalarLiteralConvertible.<br>
<br>
</span>Yes, I know. But I wonder why that is, considering that something that is StringLiteralConvertible will seemingly never use the required initializer from UnicodeScalarLiteralConvertible.<br>
<span class=""><br>
> Is there a way to write something that is a unicode scalar literal, but not a string literal?<br>
> Yes. You have already done it by extension UnicodeScalarLiteralConvertible only. String literal is what people read. Unicode is something string encoding to store in computer and the computer read.<br>
><br>
> for example:<br>
><br>
> let uScalar = "a".unicodeScalars.first! // 97<br>
> print(uScalar.dynamicType) // UnicodeScalar. NOT an Int<br>
<br>
</span>Sorry, I am not sure what this answers :(<br>
<br>
I wanted to know if there was a way to write something like (for example) uscalar”\u{65}”, that would only be a unicode scalar literal, and not a string literal. Or if there was any other way to tell the compiler “please use the initializer from UnicodeScalarLiteralConvertible instead of the one from StringLiteralConvertible”.<br>
<br>
I guess the more fundamental question I wanted to ask was “why must StringLiteralConvertible conform to UnicodeScalarLiteralConvertible?”<br>
<br>
Thanks,<br>
Loïc<br>
<div class="HOEnZb"><div class="h5"><br>
><br>
> On Mon, Jan 11, 2016 at 4:54 AM, Loïc Lecrenier <<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>> wrote:<br>
> Hi :)<br>
><br>
> I have been trying to understand the StringLiteralConvertible protocol, but there is something that I still can’t explain.<br>
><br>
> //-----------------------------<br>
><br>
> extension Int : UnicodeScalarLiteralConvertible {<br>
> public init(unicodeScalarLiteral value: UnicodeScalar) {<br>
> self = 1<br>
> }<br>
> }<br>
><br>
> extension Int : ExtendedGraphemeClusterLiteralConvertible {<br>
> public init(extendedGraphemeClusterLiteral value: Character) {<br>
> self = 2<br>
> }<br>
> }<br>
><br>
> extension Int : StringLiteralConvertible {<br>
> public init(stringLiteral value: String) {<br>
> self = 3<br>
> }<br>
> }<br>
><br>
> let a : Int = "\u{65}" // e<br>
> let b : Int = "\u{65}\u{0301}" // é<br>
> let c : Int = “hello"<br>
><br>
> //-----------------------------<br>
><br>
> If I only write the first extension: I can only initialize a, and its value will be 1.<br>
> If I write the first two extensions: I can initialize a and b, and their values will be 2.<br>
> And if I keep the three extensions: a, b, and c will all have a value of 3.<br>
><br>
> So it seems like the compiler prefers calling the initializer from (in order of preference):<br>
> 1. StringLiteralConvertible<br>
> 2. ExtendedGraphemeClusterLiteralConvertible<br>
> 3. UnicodeScalarLiteralConvertible<br>
><br>
> But for something to be StringLiteralConvertible, it needs to be ExtendedGraphemeClusterLiteralConvertible and UnicodeScalarLiteralConvertible, which means I have to define two initializers that will never be called.<br>
><br>
> Is that correct?<br>
> Is there a way to write something that is a unicode scalar literal, but not a string literal?<br>
><br>
> Thank you,<br>
><br>
> Loïc<br>
><br>
><br>
><br>
><br>
><br>
><br>
> _______________________________________________<br>
> swift-users mailing list<br>
> <a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
> <a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
><br>
><br>
><br>
> --<br>
><br>
> Owen Zhao<br>
<br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><br>Owen Zhao<br></div></div></div>
</div>