开云体育

ctrl + shift + ? for shortcuts
© 2025 Groups.io

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

give 2 errors:?
Cannot assign to value: result of conditional operator '? :' is never mutable. ?Result values in '? :' expression have mismatching types '()' and 'Int'

?

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: Advice with XCFit BDD framework??

 

Right on Hassan. ?It's a different implementations ...?
So It is like Cucumber classic... I wonder if it is diet cuc or caffeine free??


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

?

?

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

 

开云体育

Per?, "Extensions can add new functionality to a type, but they can’t override existing functionality."

I'm surprised the compiler doesn't complain.

Quality Coding logo
JON REID / Quality Coding, Inc.

On Feb 26, 2021, at 2:24 PM, David Koontz <david@...> wrote:

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"

?

//

// 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?Int?type. This subscript?[n]?returns the decimal digit?n?places in from the right of the number:

  • 123456789[0]?returns?9
  • 123456789[1]?returns?8

…and so on:

  1. extension Int {
  2. subscript(digitIndex: Int) -> Int {
  3. var decimalBase = 1
  4. for _ in 0..<digitIndex {
  5. decimalBase *= 10
  6. }
  7. return (self / decimalBase) % 10
  8. }
  9. }
  10. 746381295[0]
  11. // returns 5
  12. 746381295[1]
  13. // returns 9
  14. 746381295[2]
  15. // returns 2
  16. 746381295[8]
  17. // returns 7


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?Int?type. This subscript?[n]?returns the decimal digit?n?places in from the right of the number:

  • 123456789[0]?returns?9
  • 123456789[1]?returns?8

…and so on:

  1. extension Int {
  2. subscript(digitIndex: Int) -> Int {
  3. var decimalBase = 1
  4. for _ in 0..<digitIndex {
  5. decimalBase *= 10
  6. }
  7. return (self / decimalBase) % 10
  8. }
  9. }
  10. 746381295[0]
  11. // returns 5
  12. 746381295[1]
  13. // returns 9
  14. 746381295[2]
  15. // returns 2
  16. 746381295[8]
  17. // returns 7


Re: What OO feature would you call Swift’s Extension

 

perhaps I am parsing your statement poorly Jon.

But I see the function in UserDefaults as Open - that's what the open keyword means. ?It is the "base" class that controls it's Open/Closed nature right? ?Not the Extension.

open?func?set(_?value:?Int, forKey defaultName:?String)

The extension takes advantage of the base classes Openness to redefine the set function. ?Maybe this is synmatics of English...

?

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:

"A seam is a place where you can alter the behavior in your program without editing in that place."

It gives us a place to break dependencies.

- Jon

On Feb 25, 2021, at 8:19 AM, Sleepyfox <sleepyfox@...> wrote:

'Seam' here refers to a concept popularised by Michael Feathers' book
"Working effectively with legacy code"


There's some good info here:
https://www.informit.com/articles/article.aspx?p=359417&seqNum=3

Fox
---

On Thu, 25 Feb 2021 at 14:28, David Koontz <david@...> wrote:

That example is very helpful - thank you.

I don't know the meaning of "act as our seam"? ?Why do we want/need a seam? ?What is a seam?

Clearly this caveman must get out my stick (ones) and dead snakes (zeros) to play with your example.









Re: TDD in type-checked FP languages

 

Envoyé de mon iPhone

Le 25 févr. 2021 à 20:29, Gregory Salvan <apieum@...> a écrit :

What do you call Handler pattern ? do you have a dummy example ?
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

 


Except for IO monads are values like anything else, at least in Haskell so “testing monads” usually amounts to testing functions.
For fine grained tests I try to avoid IO and use either the Handler pattern to group all IO actions together, or a custom implementation of a type class if I use mtl style.
What do you call Handler pattern ? do you have a dummy example ?


Re: TDD in type-checked FP languages

 


As for edges, I presume you mean the point of integration with something outside of the system? I write Learning Tests for the thing outside the system, which are usually integrated tests, and then at the point of integration I choose whether to integrate with the real thing or with a function that invokes the real thing.

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
"Working effectively with legacy code"


There's some good info here:


Fox
---

On Thu, 25 Feb 2021 at 14:28, David Koontz <david@...> wrote:

That example is very helpful - thank you.

I don't know the meaning of "act as our seam"? Why do we want/need a seam? What is a seam?

Clearly this caveman must get out my stick (ones) and dead snakes (zeros) to play with your example.


Re: What OO feature would you call Swift’s Extension

 

That example is very helpful - thank you.

I don't know the meaning of "act as our seam"? ?Why do we want/need a seam? ?What is a seam?

Clearly this caveman must get out my stick (ones) and dead snakes (zeros) to play with your example.


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.

On Feb 24, 2021, at 8:33 PM, David Koontz <david@...> wrote:

Don’t follow you Jon. ?Maybe an example would help. I’m just a simple caveman.?

David Koontz

?? ? (360) 259-8380
? ? ?

On Feb 24, 2021, at 9:52 PM, Jon Reid <jon@...> wrote:

?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.

Quality Coding logo
JON REID / Quality Coding, Inc.

On Feb 24, 2021, at 7:44 PM, David Koontz <david@...> wrote:

I’ve been pondering for a few days about this. Asked a knowledgeable colleague and still wondering.?



Swift has this awesomeness called an Extension.?


it is not exactly OO inherentness but it’s close. Maybe as close as X = X * X is a power.?

I’m thinking it is a Open/Closed abiding Inheritance- What do you think?




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:

?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.

Quality Coding logo
JON REID / Quality Coding, Inc.

On Feb 24, 2021, at 7:44 PM, David Koontz <david@...> wrote:

I’ve been pondering for a few days about this. Asked a knowledgeable colleague and still wondering.?



Swift has this awesomeness called an Extension.?


it is not exactly OO inherentness but it’s close. Maybe as close as X = X * X is a power.?

I’m thinking it is a Open/Closed abiding Inheritance- What do you think?



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.

Quality Coding logo
JON REID / Quality Coding, Inc.

On Feb 24, 2021, at 7:44 PM, David Koontz <david@...> wrote:

I’ve been pondering for a few days about this. Asked a knowledgeable colleague and still wondering.?



Swift has this awesomeness called an Extension.?


it is not exactly OO inherentness but it’s close. Maybe as close as X = X * X is a power.?

I’m thinking it is a Open/Closed abiding Inheritance- What do you think?



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.?



Swift has this awesomeness called an Extension.?


it is not exactly OO inherentness but it’s close. Maybe as close as X = X * X is a power.?

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?



I've not opened the can on this set of tools... wonder if you all have advice to give?

Years ago I learned and used a mash-up of Fitness & Selenium called StoryTest-IQ (??) we called it a custom DSL and it worked well for a while - became a bit of a chore to keep all the Acceptance Level test functioning as the code under test mutated with various additions. ?Oh the good old days - when Java was king and the world could still talk on a port that wasn't port 80 or 8080.

What does the term cucumberish mean?