-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Use Val(x) and f(::Val{x}) #22475
Use Val(x) and f(::Val{x}) #22475
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -313,18 +313,33 @@ struct Colon | |
end | ||
const (:) = Colon() | ||
|
||
# For passing constants through type inference | ||
""" | ||
Val{c} | ||
Val(c) | ||
|
||
Create a "value type" out of `c`, which must be an `isbits` value. The intent of this | ||
construct is to be able to dispatch on constants, e.g., `f(Val{false})` allows you to | ||
dispatch directly (at compile-time) to an implementation `f(::Type{Val{false}})`, without | ||
having to test the boolean value at runtime. | ||
Return `Val{c}()`, which contains no run-time data. Types like this can be used to | ||
pass the information between functions through the value `c`, which must be an `isbits` | ||
value. The intent of this construct is to be able to dispatch on constants directly (at | ||
compile time) without having to test the value of the constant at run time. | ||
|
||
# Examples | ||
```jldoctest | ||
julia> f(::Val{true}) = "Good" | ||
f (generic function with 1 method) | ||
|
||
julia> f(::Val{false}) = "Bad" | ||
f (generic function with 2 methods) | ||
|
||
julia> f(Val(true)) | ||
"Good" | ||
``` | ||
""" | ||
struct Val{T} | ||
struct Val{x} | ||
end | ||
|
||
Val(x) = (@_pure_meta; Val{x}()) | ||
|
||
show(io::IO, ::Val{x}) where {x} = print(io, "Val($x)") | ||
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. Why this? Shouldn't we show the object for what it is, i.e. 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. The cool kids like "print it like you write it" these days. I personally feel this much more aesthetic. There is a doc string for 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. julia> dot([1, 2], [3, 4])
dot([1, 2], [3, 4]) ? 😄 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. It does kind of make sense to show it the way it's expected to be written, but we don't generally do this in other cases, e.g.
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. And also, 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. The 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. I think I'm ok with either printing in this case. |
||
|
||
# used by interpolating quote and some other things in the front end | ||
function vector_any(xs::ANY...) | ||
n = length(xs) | ||
|
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.
Is this public interface (meaning - does this require a
@deprecate
)?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.
Should be fine without :).
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.
unless packages have been extending it...
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.
DataArrays, StaticArrays, and IndexedTables define methods for something called
_broadcast!
but I don't think they're importing the Base one