Code Monkey home page Code Monkey logo

Comments (6)

wouter-swierstra avatar wouter-swierstra commented on July 28, 2024

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

onmyway133 avatar onmyway133 commented on July 28, 2024

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.

wouter-swierstra avatar wouter-swierstra commented on July 28, 2024

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 necessary

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

chriseidhof avatar chriseidhof commented on July 28, 2024

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.

aryairani avatar aryairani commented on July 28, 2024

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.

chriseidhof avatar chriseidhof commented on July 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.