Keyboard Shortcuts
Likes
- Testdrivendevelopment
- Messages
Search
Re: Swift Extension on Array - not working ... hmmm
After making the LineUp class that can be iterated starting with 1 (Zero is for programmers) Customers start at ONE. Now I want the iterator to wrap around... like a team that bats around the line up... ?so into the Modulo arithmetic... I'm doing this experiment in a Swift Xcode Playground... so not to sure about how to do unit testing inside the Playground??? indexWrapped <= 0 ?? index = indexMax : index = indexWrapped - 1 ? Now can anyone put this is simple caveman logic... seems like a perfectly good place to use my most hated ? :?operation. ?I don't GROK. ? class LineUp { ? ? var lineup = ["Bob", "Tony", "Iris", "Jane", "Nan", "Dave", "George", "Ringo", "Shannon" ] ?? ? ? ? subscript(n: Int) -> String { ? ? ? ? var index = 0 ? ? ? ? let indexMax = lineup.count ? ? ? ? let indexWrapped = n % indexMax ? ? ? ? indexWrapped <= 0 ?? index = indexMax : index = indexWrapped - 1 ? ? ? ? return lineup[index] ? ? } } ?
? |
||
Re: Swift Extension on Array - not working ... hmmm
// on? a slightly different note class LineUp { ? ? var lineup = ["Bob", "Tony", "Iris", "Jane", "Nan", "Dave", "George", "Ringo", "Shannon" ] ?? ? ? ? subscript(n: Int) -> String { ? ? ? ? return lineup[n-1] ? ? } } ? let list = LineUp() ? print("\(list[1]) batter") ? list.lineup.append("Paul") print("\(list[10]) battting last") ? |
||
Re: Swift Extension on Array - not working ... hmmm
OUCH! that black background - hurts the eyes... ? var str = "Hello, playground" ? // https://docs.swift.org/swift-book/LanguageGuide/Extensions.html // Extensions can add new subscripts to an existing type. ? enum Inning: Int { ? ? case first = 1, second, third, fourth, fifth, sixth, seventh, eighth, ninth ? ? // using a Instance Subscript ? ? subscript(n: Int) -> Inning { ? ? ? ? return Inning(rawValue: n)! ? ? } } ? //let current = Inning[1] let inning = Inning(rawValue: 1)! let current = inning[8] print("\(current) inning") ? ? // Better technique enum Inning2: Int { ? ? case first = 1, second, third, fourth, fifth, sixth, seventh, eighth, ninth ? ? // using a Type Subscript ? ? static subscript(n: Int) -> Inning2 { ? ? ? ? return Inning2(rawValue: n)! ? ? } } ? let current2 = Inning2[1] print("\(current2) inning") ? ? ? // Type Subscript from https://docs.swift.org/swift-book/LanguageGuide/Subscripts.html enum Planet: Int { ? ? case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune ? ? static subscript(n: Int) -> Planet { ? ? ? ? return Planet(rawValue: n)! ? ? } } ? let home = Planet[3] print(home) ? |
||
Re: Swift Extension on Array - not working ... hmmm
Figured it out ... with lots of help from the doc:??at Botton of page is Type Subscript with an example
|
||
Re: Swift Extension on Array - not working ... hmmm
开云体育Per?, "Extensions can add new functionality to a type, but they can’t override existing functionality." I'm surprised the compiler doesn't complain.
|
||
Swift Extension on Array - not working ... hmmm
Help an old timer out... I don't know why this does NOT work... I want to redefine the subscript method... in Swift ...? var str = "Hello, playground" ? // https://docs.swift.org/swift-book/LanguageGuide/Extensions.html // Extensions can add new subscripts to an existing type. ? class Inning { ?? ? var array = ["first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth"] } ? let inning = Inning() ? print ("\(inning.array[1]) inning") // prints "second inning" ? extension Array { ? ? subscript(digitIndex: Int) -> Int { ? ? ? ? let decimalBase = 1 ? ? ? ? return digitIndex - decimalBase ? ? } } ? //print("\(Inning[1]) inning") let x = inning.array[1] ? print ("\(x) inning") ? ? // expect first inning via use of extension Much like the document does here: Subscripts Extensions can add new subscripts to an existing type. This example adds an integer subscript to Swift’s built-in?
…and so on:
|
||
Re: What OO feature would you call Swift’s Extension
perhaps I am parsing your statement poorly Jon. open?func?set(_?value:?Int, forKey defaultName:?String) ? Back to the bigger picture - I think we are both saying that the Extension capability in Swift is an implementation of OO inheritance that conforms to the Open/Closed (SOLID) principle. |
||
Re: What OO feature would you call Swift’s Extension
开云体育David,I think of the "seam" metaphor as "a place where two pieces are loosely stitched together, so we can safely connect different pieces." But Michael Feathers is more precise:
It gives us a place to break dependencies. - Jon
|
||
Re: TDD in type-checked FP languages
Envoyé de mon iPhone
Le 25 févr. 2021 à 20:29, Gregory Salvan <apieum@...> a écrit :It’s basically passing around a structure containing functions doing the IOs you need. In Haskell, you can use various mechanisms to represent side effecting functions: Typeclasses (ie. interfaces which are resolved by the compiler or even dynamically depending on a type, think multiple dispatch), Free monads, or record-of-functions aka. Handler pattern. Hth Arnaud |
||
Re: TDD in type-checked FP languages
What do you call Handler pattern ? do you have a dummy example ? |
||
Re: TDD in type-checked FP languages
If you write pure code you'd probably push your state changes, IO (monads in haskell)... to the edges of your program, you also probably inject your functions as arguments. You asked about type-checked FP languages, it includes julia lang I've used as main language these last years (partially dynamic). I think it's a bit different because of multiple dispatch. By injecting functions and not structures (immutable by default) you lose benefits of multiple dispatch. So typically with a dummy ex. if you want to test function dosomething(res::AbstractIO, ...) where write(res::AbsractIO, ...) is called and your program use a structure of type IO <: AbstractIO. I'd make a test double TestIO <: AbstractIO with a method write(res::TestIO, ...) and inject a structure TestIO instead of IO in dosomething function. If domething calls open(res::AbstractIO) and close(res::AbstractIO) for ex. you don't need to override them if what you're testing is just write. Honestly, I was not very comfortable with this at first, thinking of LSP... structures are immutable, you can't inherit concrete types (just abstract ones), so it's safe and efficient. I still wonder if there's better solutions. |
||
Re: What OO feature would you call Swift’s Extension
'Seam' here refers to a concept popularised by Michael Feathers' book
toggle quoted message
Show quoted text
"Working effectively with legacy code" There's some good info here: Fox --- On Thu, 25 Feb 2021 at 14:28, David Koontz <david@...> wrote:
|
||
Re: What OO feature would you call Swift’s Extension
开云体育Let's say you have code that uses UserDefaults (a persistent key-value store available in Foundation for all Apple platforms). Specifically, it calls the method for setting a persistent integer. Persistence, ugh. This is making the code untestable.We'd like to make it possible to use a Test Double instead. We can start by defining an empty protocol. I'll be lazy about naming it. protocol UserDefaultsProtocol { } Using an extension, declare that UserDefaults (defined by Apple) conforms to our new protocol: extension UserDefaults: UserDefaultsProtocol {} The compiler is happy so far. Then we can go into the declaration of UserDefaults, find the method we want among the many it defines: open func set(_ value: Int, forKey defaultName: String) Copy the entire line, paste it into UserDefaultsProtocol, and remove the open: protocol UserDefaultsProtocol { ? ? func set(_ value: Int, forKey defaultName: String) } The extension declares that UserDefaults conforms to this protocol. And it already declares this method. So it does conform, without adding anything to the body of the extension! The compiler is still happy. Now instead of directly accessing the UserDefaults.standard singleton, we declare a property to act as our seam: var userDefaults: UserDefaultsProtocol = UserDefaults.standard By default, the property is the actual UserDefaults.standard singleton unless we say otherwise. (Normally, I'd make this an immutable `let` property, passing it in via initializer with the default value.) Now instead of UserDefaults.standard.set(42, forKey: "meaning") we can change that to userDefaults.set(42, forKey: "meaning") to reference the property instead of the singleton. A manual test confirms this refactoring. And now we have testable code, because we can supply a Test Double that also conforms to UserDefaultsProtocol. Woot! …So this example isn't Open/Closed for tacking on an additional function. Instead, it's Open/Closed for conforming to a new interface?where that interface is a slice of the original.
|
||
Re: What OO feature would you call Swift’s Extension
开云体育Don’t follow you Jon. ?Maybe an example would help. I’m just a simple caveman.?David Koontz ?? ? Email: ?david@... ?? ? (360) 259-8380 ? ? ?http://about.me/davidakoontz On Feb 24, 2021, at 9:52 PM, Jon Reid <jon@...> wrote:
|
||
Re: What OO feature would you call Swift’s Extension
开云体育My favorite use of Swift extensions is wrapping a seam around an existing object:- Take a type. Any type. I don't have to own it. - Define a new Protocol which is a slice of that class (ISP) - Using an extension, declare that the type (which I may not own) conforms to the protocol (which I do own). - No implementation necessary because it already conforms to the slice of its own interface. - I can now use the Protocol instead of the type.
|
||
What OO feature would you call Swift’s Extension
I’ve been pondering for a few days about this. Asked a knowledgeable colleague and still wondering.?
I’m thinking it is a Open/Closed abiding Inheritance- What do you think? |
||
Re: Advice with XCFit BDD framework??
On Wed, Feb 24, 2021 at 5:53 PM David Koontz <david@...> wrote:
What does the term cucumberish mean?In the absence of any other context, it probably refers to a BDD tool called Cucumber -- -- Hassan Schroeder ------------------------ hassan.schroeder@... twitter: @hassan Consulting Availability : Silicon Valley or remote |
||
Advice with XCFit BDD framework??
Has anyone of us used the XCFit BDD framework? What does the term cucumberish mean? |