Comments (6)
@onmyway133 I've rewritten the explanation below. Is this better?
There is one problem with this definition: the defaultValue
may be evaluated, regardless of whether or not the optional is nil
. This is usually undesirable behavior: an if-then-else statement should only execute one of its branches, depending on whether or not the associated condition is true. Similarly, the ??
operator should only evaluate the defaultValue
argument when the optional argument is nil
. For example, if we were to call ??
as follows:
optional ?? defaultValue
We really do not want to evaluate defaultValue
if the optional
variable is non-nil -- it could be a very expensive computation that we only want to run if it is absolutely necessary. We can resolve this issue as follows:
func ??<T>(optional: T?, defaultValue: () -> T) -> T {
if let x = optional {
return x
} else {
return defaultValue()
}
}
from functional-swift.
it could be a very expensive computation that we only want to run if it is absolutely necessary
But in this case, it just returns defaultValue
, so this is not a very expensive computation ?
from functional-swift.
I haven't said what defaultValue
is. It could still be a huge
computation. To make it even more concrete:
5 ?? aVerySlowFunction(17)
In this example, you do not want to run aVerySlowFunction, but directly
return 5.
On 23 October 2014 21:04, Khoa Pham [email protected] wrote:
it could be a very expensive computation that we only want to run if it is
absolutely necessaryBut in this case, it just returns defaultValue, so this is not a very
expensive computation ?—
Reply to this email directly or view it on GitHub
#27 (comment)
.
from functional-swift.
Maybe another way to think of this is that we want to have the same behavior as ||
(the or operator) in most languages. The ||
operator only evaluates it's right operand if the left operand is not true. For example, in Objective-C, we might say:
NSUInteger count = myArray.count || [self computeComplicatedCount];
In case myArray.count
returns a non-zero number, we don't want computeComplicatedCount
to be called...
And we want to have the same behavior for ??
. I think this has been some really good feedback, we will see how we can incorporate this...
from functional-swift.
A bit late to the party here, but maybe you want to say that in the original, the expression passed to defaultValue will be evaluated before ??
is even called. "may be evaluated" is pretty ambiguous. It will definitely be evaluated.
The expensive operation occurs before ??
, and we're passed a value or reference, returning that value is not expensive, so that's why @onmyway133 is reasonably confused.
defaultValue: T
is a value, not a computation (huge or otherwise). You want to change it to accept a computation instead of a value, so that you can decide internally whether or not to execute it.
from functional-swift.
Thanks, this has been really good feedback. We've changed the wording and will update this in the next version of the book.
from functional-swift.
Related Issues (20)
- rename CollectionType.decompose into CollectionType.decomposed HOT 1
- Book revision in the book. HOT 2
- Wrong output in Theoretical Background: Currying HOT 1
- Ambiguous Issue HOT 1
- Swift 3 HOT 1
- About The book what name is Functional Swift first chapter HOT 2
- Syntax error in book
- Syntax error in book
- Syntax error In book
- Phrasing error in book
- Syntax error in book
- Syntax error in book
- Thinking Functionally - Example: the complicated canSafelyEngageShip(_:friendly:) function HOT 1
- generate function in Wrapping Core Image chapter crashes HOT 6
- Infinite loop in FileLinesIterator HOT 3
- error while calling colorGenerator HOT 5
- get error while call colorGenerator
- Hi,where is the playground? HOT 1
- Workaround for qsort intermediate let HOT 1
- Page 27: Sample code has `subtract` function, but text talks about `difference` function HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from functional-swift.