SE-0023 API Design Guidelines: Mutating vs Functional


(Erica Sadun) #1

I'm finding the thread on the SE-0023 Review overwhelming. I'm starting a new thread and I'm including a link to a write-up of why I believe naming is better served with functional/procedural differentiation over mutating/non-mutating.

https://github.com/erica/SwiftStyle/blob/master/Grammatical.md

If I have not addressed any of Dave's concerns, I will be happy to update this.

-- Erica


(TJ Usiyan) #2

'Functional' is pretty vague in this context. Mutating cuts to
differentiating trait.

"A function produces something" can fall down if I have a function with
side effects.

private var state = 0
func generateCount() -> String {
    state += 1
    return "I've been called \(state) times"
}

We could narrow it by saying that a pure function has no side effects but
this just draws us further from the relevant point, in my opinion.

···

On Sun, Jan 24, 2016 at 7:09 PM, Erica Sadun via swift-evolution < swift-evolution@swift.org> wrote:

I'm finding the thread on the SE-0023 Review overwhelming. I'm starting a
new thread and I'm including a link to a write-up of why I believe naming
is better served with functional/procedural differentiation over
mutating/non-mutating.

https://github.com/erica/SwiftStyle/blob/master/Grammatical.md

If I have not addressed any of Dave's concerns, I will be happy to update
this.

-- Erica

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Haravikk) #3

Also, how do we differentiate between functions that have @warn_unused_result? In your example the string that’s returned isn’t actually required to be used, so someone could call the function purely with the intention of increment state; not that it would be a very useful thing to do, but some functions have return values that aren’t necessarily required, like removeLast() on Arrays (I may not actually want the last array element, or I already know what it is and just want it removed).

···

On 25 Jan 2016, at 06:12, T.J. Usiyan via swift-evolution <swift-evolution@swift.org> wrote:

'Functional' is pretty vague in this context. Mutating cuts to differentiating trait.

"A function produces something" can fall down if I have a function with side effects.

private var state = 0 
func generateCount() -> String {
    state += 1
    return "I've been called \(state) times" 
}

We could narrow it by saying that a pure function has no side effects but this just draws us further from the relevant point, in my opinion.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Erica Sadun) #4

I incorporated your feedback. Please reload: https://github.com/erica/SwiftStyle/blob/master/Grammatical.md

-- E

···

On Jan 25, 2016, at 2:52 AM, Haravikk <swift-evolution@haravikk.com> wrote:

On 25 Jan 2016, at 06:12, T.J. Usiyan via swift-evolution <swift-evolution@swift.org> wrote:

'Functional' is pretty vague in this context. Mutating cuts to differentiating trait.

"A function produces something" can fall down if I have a function with side effects.

private var state = 0 
func generateCount() -> String {
   state += 1
   return "I've been called \(state) times" 
}

We could narrow it by saying that a pure function has no side effects but this just draws us further from the relevant point, in my opinion.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Also, how do we differentiate between functions that have @warn_unused_result? In your example the string that’s returned isn’t actually required to be used, so someone could call the function purely with the intention of increment state; not that it would be a very useful thing to do, but some functions have return values that aren’t necessarily required, like removeLast() on Arrays (I may not actually want the last array element, or I already know what it is and just want it removed).