<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jun 8, 2017, at 11:11 AM, Joel Loeshelle via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Hi all,<div class=""><br class=""></div><div class="">Is there any support for having an optional type in an initializer? My use case is I want to have a struct SectionWrapper that includes information about a Collection View Section. So you can pass the CellType, Cell Data Source, and an Optional HeaderType and HeaderDataSource if there is a Section Header. Here's the code. I first have a protocol ViewModelConfigurable that the Cell and Header subclasses conform to:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class=""><span style="color:rgb(194,52,155)" class="">protocol</span> ViewModelConfigurable</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class="">{</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class=""> <span style="color:rgb(194,52,155)" class="">associatedtype</span> ViewModelType</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53);min-height:13px" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class=""> <span style="color:rgb(194,52,155)" class="">func</span> configure(with viewModel : <span style="color:rgb(0,175,202)" class="">ViewModelType</span>);</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class="">}</div></div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(147, 201, 106); background-color: rgb(40, 43, 53);" class=""><span style="color:rgb(194,52,155)" class="">class</span><span style="color:rgb(255,255,255)" class=""> TestCell : </span><span style="color:rgb(0,175,202)" class="">UICollectionViewCell</span><span style="color:rgb(255,255,255)" class="">, </span>ViewModelConfigurable</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class="">{</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class=""> <span style="color:rgb(194,52,155)" class="">typealias</span> ViewModelType = <span style="color:rgb(0,175,202)" class="">String</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class=""> <span style="color:rgb(194,52,155)" class="">func</span> configure(with viewModel: <span style="color:rgb(0,175,202)" class="">String</span>) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class=""> <span style="color:rgb(77,191,86)" class="">//todo</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class="">}</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Helvetica; background-color: rgb(40, 43, 53); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 175, 202); background-color: rgb(40, 43, 53);" class=""><span style="color:rgb(194,52,155)" class="">class</span><span style="color:rgb(255,255,255)" class=""> HeaderFooter : </span>UICollectionReusableView<span style="color:rgb(255,255,255)" class="">, </span><span style="color:rgb(147,201,106)" class="">ViewModelConfigurable</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class="">{</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class=""> <span style="color:rgb(194,52,155)" class="">typealias</span> ViewModelType = <span style="color:rgb(0,175,202)" class="">String</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class=""> <span style="color:rgb(194,52,155)" class="">func</span> configure(with viewModel: <span style="color:rgb(0,175,202)" class="">String</span>) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class=""> <span style="color:rgb(77,191,86)" class="">//todo</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class="">}</div></div><div class=""><br class=""></div><div class="">I then have a SectionWrapper struct where I want to initialize with a concrete UICollectionViewCell type that conforms to ViewModelType and pass the associated dataSource which must be an array of ViewModelType. I also want to have the ability to pass an optional UICollectionReusableView type that conforms to ViewModelType with the associated dataSource. You could then pass nil if this section does not have a Header. See below:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class=""><span style="color:rgb(194,52,155)" class="">struct</span> SectionWrapper</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class="">{</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class=""> <span style="color:rgb(194,52,155)" class="">let</span> cellConfigure: (<span style="color:rgb(0,175,202)" class="">UICollectionViewCell</span>, <span style="color:rgb(0,175,202)" class="">Int</span>) -> ()</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class=""> <span style="color:rgb(194,52,155)" class="">let</span> headerConfigure: ((<span style="color:rgb(0,175,202)" class="">UICollectionReusableView</span>) -> ())?</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53);min-height:13px" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class=""> <span style="color:rgb(194,52,155)" class="">init</span><ActualCellType : <span style="color:rgb(0,175,202)" class="">UICollectionViewCell</span> & <span style="color:rgb(147,201,106)" class="">ViewModelConfigurable</span>, HeaderType : <span style="color:rgb(0,175,202)" class="">UICollectionReusableView</span> & <span style="color:rgb(147,201,106)" class="">ViewModelConfigurable</span>>(cellType : <span style="color:rgb(147,201,106)" class="">ActualCellType</span>.Type, cellDataSource : [<span style="color:rgb(147,201,106)" class="">ActualCellType</span>.<span style="color:rgb(0,175,202)" class="">ViewModelType</span>], headerType : (<span style="color:rgb(147,201,106)" class="">HeaderType</span>.Type)?, headerDataSource : (<span style="color:rgb(147,201,106)" class="">HeaderType</span>.<span style="color:rgb(0,175,202)" class="">ViewModelType</span>)?)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class=""> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class=""> <span style="color:rgb(194,52,155)" class="">self</span>.<span style="color:rgb(147,201,106)" class="">cellConfigure</span> = { cell, index <span style="color:rgb(194,52,155)" class="">in</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class=""> (cell <span style="color:rgb(194,52,155)" class="">as</span>! <span style="color:rgb(147,201,106)" class="">ActualCellType</span>).<span style="color:rgb(147,201,106)" class="">configure</span>(with: cellDataSource[index])</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class=""> }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53);min-height:13px" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class=""> <span style="color:rgb(194,52,155)" class="">if</span> <span style="color:rgb(194,52,155)" class="">let</span> headerDataSource = headerDataSource</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class=""> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class=""> <span style="color:rgb(194,52,155)" class="">self</span>.<span style="color:rgb(147,201,106)" class="">headerConfigure</span> = { header <span style="color:rgb(194,52,155)" class="">in</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class=""> (header <span style="color:rgb(194,52,155)" class="">as</span>! <span style="color:rgb(147,201,106)" class="">HeaderType</span>).<span style="color:rgb(147,201,106)" class="">configure</span>(with: headerDataSource);</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class=""> <span style="color:rgb(194,52,155)" class="">else</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class=""> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class=""> <span style="color:rgb(194,52,155)" class="">self</span>.<span style="color:rgb(147,201,106)" class="">headerConfigure</span> = <span style="color:rgb(194,52,155)" class="">nil</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class="">}</div></div><div class=""><br class=""></div><div class="">If I pass nil for headerType and headerDataSource, I get the following error: </div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(77, 191, 86); background-color: rgb(40, 43, 53);" class="">error: Swift4Test.playground:86:31: error: generic parameter 'ActualCellType' could not be inferred</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(77, 191, 86); background-color: rgb(40, 43, 53);" class=""> let noHeader = SectionWrapper.init(cellType: TestCell.self, cellDataSource: ["Test"], headerType: nil, headerDataSource: nil) //Gives error</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)" class=""></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(77, 191, 86); background-color: rgb(40, 43, 53);" class=""><br class=""></div></div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class=""><span style="color:rgb(194,52,155)" class="">let</span> header = <span style="color:rgb(147,201,106)" class="">SectionWrapper</span>.<span style="color:rgb(194,52,155)" class="">init</span>(cellType: <span style="color:rgb(147,201,106)" class="">TestCell</span>.<span style="color:rgb(194,52,155)" class="">self</span>, cellDataSource: [<span style="color:rgb(228,67,71)" class="">"Test"</span>], headerType: <span style="color:rgb(147,201,106)" class="">HeaderFooter</span>.<span style="color:rgb(194,52,155)" class="">self</span>, headerDataSource: <span style="color:rgb(228,67,71)" class="">"Header"</span>) <span style="color:rgb(77,191,86)" class="">//No error</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class=""><span style="color:rgb(77,191,86)" class=""><br class=""></span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class=""><span style="color:rgb(194,52,155)" class="">let</span> noHeader = <span style="color:rgb(147,201,106)" class="">SectionWrapper</span>.<span style="color:rgb(194,52,155)" class="">init</span>(cellType: <span style="color:rgb(147,201,106)" class="">TestCell</span>.<span style="color:rgb(194,52,155)" class="">self</span>, cellDataSource: [<span style="color:rgb(228,67,71)" class="">"Test"</span>], headerType: <span style="color:rgb(194,52,155)" class="">nil</span>, headerDataSource: <span style="color:rgb(194,52,155)" class="">nil</span>) <span style="color:rgb(77,191,86)" class="">//Gives error</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);" class=""><br class=""></div></div><div class=""><br class=""></div><div class="">I have also attached the Playground that I'm working through. Please let me know if you have any questions and thanks in advance for your help!</div></div></div></blockquote><div><br class=""></div><div>You could overload SectionWrapper.init(), providing two versions: one takes non-optional headerType and headerDataSource parameters, and the other does not take them at all, and only has a single generic parameter for ActualCellType.</div><div><br class=""></div><div>Unfortunately we don’t support explicit specialization on function calls right now; if we did, you could write SectionWrapper.init<T, U> to explicitly bind the generic arguments.</div><div><br class=""></div><div>Slava</div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class="">
<span id="cid:814C37E2-9CA7-4361-B469-B7E042B0504A@gateway.sonic.net"><Swift4Test.playground.zip></span>_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></body></html>