Can't get going on Numerics

Hi. I'm just trying to get some basic examples going, but no luck so far.

The only function example I can find,

Code Block
func logit<NumberType: Real>(_ p: NumberType) -> NumberType {
log(p) - log(onePlus: -p)
}
print(logit(12.456))

gets me a "Cannot convert return expression of type 'Float' to return type 'NumberType'" message from Xcode 12 beta on macOS 10.15.5. (It gives various suggestions, all of which don't help or lead me in the right direction.)

This other attempt seems (* to me) like it should work:

Code Block
func testPow<T: Real>(_ x: T, toThe y: T) -> T { pow(x, y) }
print(testPow(10.0, toThe: 12.3456))

but I get "Type of expression is ambiguous without more context." I can't image what the missing contextual info might be here.

(Both examples import both Foundation and Numerics.)

Thanks for any help!
Jeff
Answered by Engineer in 616108022
These are both pretty simple to resolve:

log and log(onePlus:) are static methods on Real, so you need to spell them [Type].log(p):

Code Block
func logit<NumberType: Real>(_ p: NumberType) -> NumberType {
NumberType.log(p) - NumberType.log(onePlus: -p)
}
/* or, use type inference and only use the leading `.`*/
func logit<NumberType: Real>(_ p: NumberType) -> NumberType {
.log(p) - .log(onePlus: -p)
}


Same thing for your second example (use .pow or T.pow).

Providing free-function versions is something that we might consider in the future (so that you can elide the [Type].), but we want to avoid the collision with the typed functions defined by Darwin in the very short term.
Accepted Answer
These are both pretty simple to resolve:

log and log(onePlus:) are static methods on Real, so you need to spell them [Type].log(p):

Code Block
func logit<NumberType: Real>(_ p: NumberType) -> NumberType {
NumberType.log(p) - NumberType.log(onePlus: -p)
}
/* or, use type inference and only use the leading `.`*/
func logit<NumberType: Real>(_ p: NumberType) -> NumberType {
.log(p) - .log(onePlus: -p)
}


Same thing for your second example (use .pow or T.pow).

Providing free-function versions is something that we might consider in the future (so that you can elide the [Type].), but we want to avoid the collision with the typed functions defined by Darwin in the very short term.
Can't get going on Numerics
 
 
Q