<span id="mailbox-conversation"><div>This is not how *LiteralConvertible protocols work, though. You can only initialize variables with specific literal values recognized by the parser, it never magically casts a type to a different type for you. Similar implicit casts in my opinion confuse whoever reads the code afterwards.</div>
<div><br></div>
<div>```</div>
<div>
<div id="mb-reply">class SomeConvertible: BooleanLiteralConvertible {</div>
<div id="mb-reply"> typealias BooleanLiteralType = Bool</div>
<div id="mb-reply"> let boolean: Bool</div>
<div id="mb-reply"><br></div>
<div id="mb-reply"> internal required init(booleanLiteral value: SomeConvertible.BooleanLiteralType) {</div>
<div id="mb-reply"> self.boolean = value</div>
<div id="mb-reply"> }</div>
<div id="mb-reply">}</div>
<div id="mb-reply"><br></div>
<div id="mb-reply">func getBoolean() -> Bool {</div>
<div id="mb-reply"> return false</div>
<div id="mb-reply">}</div>
<div id="mb-reply"><br></div>
<div id="mb-reply">let validConvertible: SomeConvertible = false</div>
<div id="mb-reply">// Line below doesn’t compile</div>
<div id="mb-reply">let invalidConvertible: SomeConvertible = getBoolean()</div>
</div>
<div>```</div></span><div class="mailbox_signature">
<br><br></div>
<br><br><div class="gmail_quote"><p>On Fri, Dec 4, 2015 at 8:51 AM, Adrian Zubarev <span dir="ltr"><<a href="mailto:adrian.zubarev@devandartist.com" target="_blank">adrian.zubarev@devandartist.com</a>></span> wrote:<br></p><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">
<div class="bloop_markdown">
<p>We have finally a corner where people can share their ideas on how Swift can be improved, and I’m glad my ideas might be part of this fun.</p>
<p>Swift introduced a few <code>Convertible</code> protocols, but it still needs some more If you ask me. </p>
<p>Something like this:</p>
<pre><code class="swift">public protocol CustomConvertible {
typealias CustomType
public init(value: Self.CustomType) // or in a better way I can't come up with
}
</code></pre>
<p>But it should work in a way that we could create more different types like <code>enums</code> and <code>ErrorType</code>.</p>
<pre><code class="swift">protocol AConvertible: CustomConvertible { /* some stuff here*/ }
protocol BConvertible: CustomConvertible { /* some stuff here*/ }
struct A {
var foo: Int = 0
}
struct B {
var boo: String = "Hello World"
}
struct C: AConvertible, BConvertible {
typealias CustomAType = A
typealias CustomBType = B
var magical: SomeType
init(value: CustomAType) {
// implement it
}
init(value: CustomBType) {
// implement it
}
}
//===========================
let firstC: C = A() // this is what I'm missing in Swift
let secondC: C = B() // this would be a great syntax sugar
</code></pre>
<p>What do you think?</p>
<p></p>
</div>
<div class="bloop_original_html">
<style>
body {
        font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
        padding:1em;
        margin:auto;
        background:#fefefe;
}
h1, h2, h3, h4, h5, h6 {
        font-weight: bold;
}
h1 {
        color: #000000;
        font-size: 28pt;
}
h2 {
        border-bottom: 1px solid #CCCCCC;
        color: #000000;
        font-size: 24px;
}
h3 {
        font-size: 18px;
}
h4 {
        font-size: 16px;
}
h5 {
        font-size: 14px;
}
h6 {
        color: #777777;
        background-color: inherit;
        font-size: 14px;
}
hr {
        height: 0.2em;
        border: 0;
        color: #CCCCCC;
        background-color: #CCCCCC;
display: inherit;
}
p, blockquote, ul, ol, dl, li, table, pre {
        margin: 15px 0;
}
a, a:visited {
        color: #4183C4;
        background-color: inherit;
        text-decoration: none;
}
#message {
        border-radius: 6px;
        border: 1px solid #ccc;
        display:block;
        width:100%;
        height:60px;
        margin:6px 0px;
}
button, #ws {
        font-size: 12 pt;
        padding: 4px 6px;
        border-radius: 5px;
        border: 1px solid #bbb;
        background-color: #eee;
}
code, pre, #ws, #message {
        font-family: Monaco;
        font-size: 10pt;
        border-radius: 3px;
        background-color: #F8F8F8;
        color: inherit;
}
code {
        border: 1px solid #EAEAEA;
        margin: 0 2px;
        padding: 0 5px;
}
pre {
        border: 1px solid #CCCCCC;
        overflow: auto;
        padding: 4px 8px;
}
pre > code {
        border: 0;
        margin: 0;
        padding: 0;
}
#ws { background-color: #f8f8f8; }
.bloop_markdown table {
border-collapse: collapse;
font-family: Helvetica, arial, freesans, clean, sans-serif;
color: rgb(51, 51, 51);
font-size: 15px; line-height: 25px;
padding: 0; }
.bloop_markdown table tr {
border-top: 1px solid #cccccc;
background-color: white;
margin: 0;
padding: 0; }
.bloop_markdown table tr:nth-child(2n) {
background-color: #f8f8f8; }
.bloop_markdown table tr th {
font-weight: bold;
border: 1px solid #cccccc;
margin: 0;
padding: 6px 13px; }
.bloop_markdown table tr td {
border: 1px solid #cccccc;
margin: 0;
padding: 6px 13px; }
.bloop_markdown table tr th :first-child, table tr td :first-child {
margin-top: 0; }
.bloop_markdown table tr th :last-child, table tr td :last-child {
margin-bottom: 0; }
.bloop_markdown blockquote{
border-left: 4px solid #dddddd;
padding: 0 15px;
color: #777777; }
blockquote > :first-child {
margin-top: 0; }
blockquote > :last-child {
margin-bottom: 0; }
code, pre, #ws, #message {
word-break: normal;
word-wrap: normal;
}
hr {
display: inherit;
}
.bloop_markdown :first-child {
-webkit-margin-before: 0;
}
code, pre, #ws, #message {
font-family: Menlo, Consolas, Liberation Mono, Courier, monospace;
}
.send { color:#77bb77; }
.server { color:#7799bb; }
.error { color:#AA0000; }</style><div class="bloop_markdown"><p><br></p></div>
<div class="bloop_original_html"><div id="bloop_sign_1449235780140307968" class="bloop_sign">
<div><font face="helvetica, arial">— </font></div>
<div>Regards Adrian<br></div>
</div></div>
<div class="bloop_markdown"><p></p></div>
<style>body{font-family:Helvetica,Arial;font-size:13px}</style></div>
<div class="bloop_markdown"><p></p></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=0STgJFrDM1NW2hlUscsEzEbKCUfVMDHdFZGprHdNmYc67HqSKuVeKdrTviRbwLJY8iTQNth-2BB-2FLJvdmLa5fh8Cbng8nXDsS0ClyPbHMsubN8jGvy24bYEcimn2-2Bz8g79LL5R9a-2Fl-2FNSTbd6cKLGOOOpyqDlAgnRPzNZCHybwpuhG1d-2FKrah-2Bj19J-2FWDTCJOLcX9y1oJbLTOMs1Nehr3Rs-2FDajyKj-2Bh0LS9ms1eA-2FSNc-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;"></div></blockquote></div><br>