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

introduce a function to check whether a value can be represented as a type #55150

Open
eschnett opened this issue Jul 17, 2024 · 5 comments
Open
Labels
feature Indicates new feature / enhancement requests

Comments

@eschnett
Copy link
Contributor

It would be nice if one could write x in Int32 etc. to check whether the value of x can be represented as Int32. This would be implemented as

in(x::Real, ::Type{T}) where {T<:Real} = x in typemin(T):typemax(T)

mimicking in(x::Real, r::AbstractRange{<:Real}).

Note that this is different from x isa Int32, which checks the type of x, not its value.

@mbauman
Copy link
Member

mbauman commented Jul 17, 2024

I'm having a hard time understanding what this might mean in general. Is [1,2,3] in UnitRange{Int}? Is (a=2, b=3) in Dict{Symbol, Int}? is "this" in Any? What if I pass a Union?

@jishnub
Copy link
Member

jishnub commented Jul 17, 2024

For the first example, one may use typemin(Int32) <= x <= typemax(Int32) to check if it lies in range.

@eschnett
Copy link
Contributor Author

I'm imagining types that implement typemin and typemax, thus Any wouldn't be a valid second argument (although that might work too). Thus UnitRange also wouldn't be valid.

My starting point is that types are very similar to set of values, and thus in makes sense.

Maybe "can be converted to" would be another way of thinking about this.

@mbauman
Copy link
Member

mbauman commented Jul 17, 2024

But it's gotta consider more than just typemin and typemaxT<:Real also includes rationals and floating points and unions thereof (and your current definition is wrong for those).

My point is more general than the examples I gave — in works on values and collections of values, and checks if any value is equal to one in the collection. This is implicitly considering types as a collection of values and there does exist a UnitRange that is equal to [1,2,3].

This could be a useful operation to have, but I don't think it should be called in, and I think it's gotta carefully consider what the generic behavior and definition would be.

@nsajko nsajko added the feature Indicates new feature / enhancement requests label Jul 17, 2024
@ararslan
Copy link
Member

Perhaps this would be a natural extension of isvalid(T, value), which is currently defined only for T as String or Char.

@mbauman mbauman changed the title Allow x in Int32 etc. introduce a function to check whether a value can be represented as a type Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Indicates new feature / enhancement requests
Projects
None yet
Development

No branches or pull requests

5 participants