- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Expand documentation on Val{T} #15729
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- v1.11.2
- v1.11.1
- v1.11.0
- v1.11.0-rc4
- v1.11.0-rc3
- v1.11.0-rc2
- v1.11.0-rc1
- v1.11.0-beta2
- v1.11.0-beta1
- v1.11.0-alpha2
- v1.11.0-alpha1
- v1.10.7
- v1.10.6
- v1.10.5
- v1.10.4
- v1.10.3
- v1.10.2
- v1.10.1
- v1.10.0
- v1.10.0-rc3
- v1.10.0-rc2
- v1.10.0-rc1
- v1.10.0-beta3
- v1.10.0-beta2
- v1.10.0-beta1
- v1.10.0-alpha1
- v1.9.4
- v1.9.3
- v1.9.2
- v1.9.1
- v1.9.0
- v1.9.0-rc3
- v1.9.0-rc2
- v1.9.0-rc1
- v1.9.0-beta4
- v1.9.0-beta3
- v1.9.0-beta2
- v1.9.0-beta1
- v1.9.0-alpha1
- v1.8.5
- v1.8.4
- v1.8.3
- v1.8.2
- v1.8.1
- v1.8.0
- v1.8.0-rc4
- v1.8.0-rc3
- v1.8.0-rc2
- v1.8.0-rc1
- v1.8.0-beta3
- v1.8.0-beta2
- v1.8.0-beta1
- v1.7.3
- v1.7.2
- v1.7.1
- v1.7.0
- v1.7.0-rc3
- v1.7.0-rc2
- v1.7.0-rc1
- v1.7.0-beta4
- v1.7.0-beta3
- v1.7.0-beta2
- v1.7.0-beta1
- v1.6.7
- v1.6.6
- v1.6.5
- v1.6.4
- v1.6.3
- v1.6.2
- v1.6.1
- v1.6.0
- v1.6.0-rc3
- v1.6.0-rc2
- v1.6.0-rc1
- v1.6.0-beta1
- v1.5.4
- v1.5.3
- v1.5.2
- v1.5.1
- v1.5.0
- v1.5.0-rc2
- v1.5.0-rc1
- v1.5.0-beta1
- v1.4.2
- v1.4.1
- v1.4.0
- v1.4.0-rc2
- v1.4.0-rc1
- v1.3.1
- v1.3.0
- v1.3.0-rc5
- v1.3.0-rc4
- v1.3.0-rc3
- v1.3.0-rc2
- v1.3.0-rc1
- v1.3.0-alpha
- v1.2.0
- v1.2.0-rc3
- v1.2.0-rc2
- v1.2.0-rc1
- v1.1.1
- v1.1.0
- v1.1.0-rc2
- v1.1.0-rc1
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- v1.0.0-rc1
- v0.7.0
- v0.7.0-rc3
- v0.7.0-rc2
- v0.7.0-rc1
- v0.7.0-beta2
- v0.7.0-beta
- v0.7.0-alpha
- v0.6.4
- v0.6.3
- v0.6.2
- v0.6.1
- v0.6.0
- v0.6.0-rc3
- v0.6.0-rc2
- v0.6.0-rc1
- v0.6.0-pre.beta
- v0.6.0-pre.alpha
- v0.5.2
- v0.5.1
- v0.5.0
- v0.5.0-rc4
- v0.5.0-rc3
- v0.5.0-rc2
- v0.5.0-rc1
- v0.5.0-rc0
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1237,24 +1237,15 @@ If you apply :func:`supertype` to other type objects (or non-type objects), a | |
julia> supertype(Union{Float64,Int64}) | ||
ERROR: `supertype` has no method matching supertype(::Type{Union{Float64,Int64}}) | ||
|
||
.. _man-val-trick: | ||
|
||
"Value types" | ||
------------- | ||
|
||
As one application of these ideas, Julia includes a parametric type, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Never understood what kind of "these ideas" are being referred to. I guess many people who just followed the |
||
``Val{T}``, designated for dispatching on bits-type *values*. For | ||
example, if you pass a boolean to a function, you have to test the | ||
value at run-time: | ||
|
||
.. doctest:: | ||
|
||
function firstlast(b::Bool) | ||
return b ? "First" : "Last" | ||
end | ||
|
||
println(firstlast(true)) | ||
|
||
You can instead cause the conditional to be evaluated during function | ||
compilation by using the ``Val`` trick: | ||
``Val{T}``, designated for dispatching on bits-type *values*. | ||
Normally, you can't dispatch on a value such as ``true`` or ``false``, | ||
but the ``Val`` type makes this possible: | ||
|
||
.. doctest:: | ||
|
||
|
@@ -1270,6 +1261,12 @@ For consistency across Julia, the call site should always pass a | |
``Val`` type rather than creating an instance, i.e., use | ||
``foo(Val{:bar})`` rather than ``foo(Val{:bar}())``. | ||
|
||
It's worth noting that it's extremely easy to mis-use the ``Val`` | ||
trick, and you can easily end up making the performance of your code | ||
much *worse*. If you're contemplating using ``Val``, please read the | ||
more extensive discussion in :ref:`the performance tips | ||
<man-performance-val>`. | ||
|
||
.. _man-nullable-types: | ||
|
||
Nullable Types: Representing Missing Values | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"knows" I think.