Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a FAQ entry for “computed/constrained type parameters”. #33631

Merged
merged 5 commits into from
Oct 28, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update doc/src/manual/faq.md
Co-Authored-By: Matt Bauman <[email protected]>
  • Loading branch information
tpapp and mbauman committed Oct 22, 2019
commit ece0df46732fc5373ea5c360aa2556cc8a042de6
14 changes: 13 additions & 1 deletion doc/src/manual/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,19 @@ julia> sqrt(-2.0+0im)

### How can I constrain or compute type parameters?

Julia does not support a special syntax for expressing constraints for type parameters, but these can be enforced in [constructors](@ref man-constructors).
The parameters of a [parametric type](@ref man-parametric-types) can hold either
types or bits values, and the type itself chooses how it makes use of these parameters.
For example, `Array{Float64, 2}` is parameterized by the type `Float64` to express its
element type and the integer value `2` to express its number of dimensions. When
defining your own parametric type, you can use subtype constraints to declare that a
certain parameter must be a subtype ([`<:`](@ref)) of some abstract type or a previous
type parameter. There is not, however, a dedicated syntax to declare that a parameter
must be a _value_ of a given type — that is, you cannot directly declare that a
dimensionality-like parameter [`isa`](@ref) `Int` within the `struct` definition, for
example. Similarly, you cannot do computations (including simple things like addition
or subtraction) on type parameters. Instead, these sorts of constraints and
relationships may be expressed through additional type parameters that are computed
and enforced within the type's [constructors](@ref man-constructors).

As an example, consider
```julia
Expand Down