Closed dan-zheng closed 4 years ago
@swift-ci create
Forum replies (by @rjmccall) suggest that the "lack of type inference" is expected behavior, given how function builder syntax sugar is expanded.
That makes sense as a technical explanation. I'm still curious about answers to Anandabits (JIRA User)'s use-case-motivated questions:
Understood. What isn't clear to me though is why this is necessary in function builders if an analogous fluent builder API doesn't run into the same issues. Is there a non-obvious difference in type checking consequences between the two approaches to syntax? Or is the intent to discourage libraries that would rely on this kind of inference altogether?
Should libraries give up on ever using function builders for this style of type inference? Are libraries encouraged to use explicit generic parameters or a different pattern (e.g. fluent method-chaining builders) instead?
// Will this `<Float>` generic parameter type inference never be possible?
let model = Sequential {
Conv2D<Float>(...) // first layer, feeding output to:
AvgPool2D(...) // AvgPool2D<Float>, feeding output to...
Flatten() // Flatten<Float>
Dense(...) // Dense<Float>
Dense(...) // Dense<Float>
}
Serious discussions on function builders' future design evolution should probably stay on the evolution forums, just to avoid having a million separate conversations that people feel obliged to follow. Please tag me when you post.
Thanks John.
I've just replied on the forums: https://forums.swift.org/t/function-builder-cannot-infer-generic-parameters-even-though-direct-call-to-buildblock-can/35886/15.
Feel free to close this issue if centralizing discussion on the forums makes sense. I'm hesitant to close myself since there's an attached rdar.
Additional Detail from JIRA
| | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug, FunctionBuilder, TypeChecker | |Assignee | None | |Priority | Medium | md5: 56625d74937947d4f1c1a26936e26eefIssue Description:
Function builder generic parameter type inference fails, even though type inference succeeds for manually-written
buildBlock
calls.Cross-posted here from https://forums.swift.org/t/function-builder-cannot-infer-generic-parameters-even-though-direct-call-to-buildblock-can/35886.
Context
Our deep learning library defines a Sequential type for sequentially composing neural network layers. It's powered by a function builder called LayerBuilder.
Sequential is currently used like this:
Ideally, we'd like the function builder to infer layer generic parameters so that only one specialization needs to be specified:
But it doesn't work:
Direct calls to
LayerBuilder.buildBlock
do type-check inferring some generic parameters, which makes me feel that this is a type inference deficiency specific to function builders:Full reproducer
Uncomment FIXME comment for type inference error: