[swift-evolution] Figuring out what you get for free

Michel Fortin michel.fortin at michelf.ca
Sun Sep 17 22:24:35 CDT 2017


It could certainly be improved:

- In step 2, the compiler could propose a fixit for subscript where the unknown associated type is replaced by a placeholder.
- Fixits from steps 2, 3, and 4 should be combined together as a single fixit.

And with this: you add the conformance to your type, Xcode automatically suggest everything you need to add, and you then add it in one click.

> Le 17 sept. 2017 à 22:13, T.J. Usiyan <griotspeak at gmail.com> a écrit :
> 
> I guess the question is, "Do we want this to be the process we expect of and explain to newcomers?"
> 
> On Sun, Sep 17, 2017 at 7:32 PM, Michel Fortin via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> Le 17 sept. 2017 à 18:00, Félix Cloutier via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> a écrit :
>> 
>> I found that for Sequence, but Sequence is far from the only protocol with default implementations, and not all of them have maintainers willing to write and update documentation to the degree that Apple will.
> 
> How I do it is like this:
> 
> 1. Make a dummy struct (or class) that claim conformance to a protocol:
> 
> struct Z: Collection {
> }
> 
> 2. Compiling, then deciphering the errors tells me that type deduction doesn't work for associated type `Index` because there is no subscript. So I add one:
> 
> struct Z: Collection {
> 	subscript (index: Int) -> Int {
> 		get { return index }
> 	}
> }
> 
> 3. Compiling again, I now get a suggestion (fixit) telling me to add `startIndex` and `endIndex`. I add the suggested code:
> 
> struct Z: Collection {
> 	var startIndex: Int
> 
> 	var endIndex: Int
> 
> 	subscript (index: Int) -> Int {
> 		get { return index }
> 	}
> }
> 
> 4. Compiling again, I get another suggestion (fixit) telling me I'm missing `index(after:)`. I add it and write an implementation inside the braces. And here I am:
> 
> struct Z: Collection {
> 	func index(after i: Int) -> Int {
> 		return i + 1
> 	}
> 
> 	var startIndex: Int
> 
> 	var endIndex: Int
> 
> 	subscript (index: Int) -> Int {
> 		get { return index }
> 	}
> }
> 
> 5. And now it compiles. Hurray!
> 
> I made a collection type and did not have to read any documentation at all. The hardest step is the first one where you have to figure out how to make deduction work for the associated types based on the error messages.
> 
> 
> -- 
> Michel Fortin
> https://michelf.ca <https://michelf.ca/>
> 
> 
> 
> -- 
> Michel Fortin
> https://michelf.ca <https://michelf.ca/>
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> 
> 

-- 
Michel Fortin
https://michelf.ca

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170917/c9158046/attachment.html>


More information about the swift-evolution mailing list