You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have read the roadmap and priorities and I believe this request falls within the priorities.
What is your request?
Come up with a way to allow users to write a single function that accepts Int, IntLiteral and scalar SIMD types. I assume there is sound rational for Int not simply being defined as alias Int = Scalar[DType.index], so maybe this would require the introduction of another trait?
What is your motivation for this change?
I've noticed a bit of pain point when attempting to write functions that are meant to accept any possible integral scalar type (Int and integral SIMD scalars). You can easily write a function to accept SIMD scalars like such.
fnfoo[type: DType](s: Scalar[type]):
constrained[type.is_integral(), "Expected an integral"]()
print(s)
However, even though you can easily convert an Int to a Scalar[DType.index], trying to call this function with an Int will fail since it can't implicitly parameterize type. So in order to maintain a clean interface one must write a second overload that accepts Int.
fnfoo(i: Int):
foo[DType.index](i)
I imagine this behaviour could become burdensome for numerical library writers.
Any other details?
No response
The text was updated successfully, but these errors were encountered:
That works well, but it results in accepting Bool implicitly as well which might be undesirable? I'm not sure that the opinion of the team is on that sort of behaviour honestly.
It would be nice to provide a Arithmetic or Numeric trait for example. One tricky part is you want this to be a generic based on the associated type, right? Consider something like an Addable trait which requires a type to implement the add dunder function. What should the return type be as mandated by the trait? You can't just force it to be Int or something, since you'd want both Int and UInt to conform to Addable. However, they return their respective types, e.g. Int's dunder add returns an Int, and UInt as the return type for UInt case. So we need some more powerful language features (associated types is what I think @jeff and others have been calling this) in order to do this right.
Review Mojo's priorities
What is your request?
Come up with a way to allow users to write a single function that accepts
Int
,IntLiteral
and scalarSIMD
types. I assume there is sound rational forInt
not simply being defined asalias Int = Scalar[DType.index]
, so maybe this would require the introduction of another trait?What is your motivation for this change?
I've noticed a bit of pain point when attempting to write functions that are meant to accept any possible integral scalar type (
Int
and integralSIMD
scalars). You can easily write a function to acceptSIMD
scalars like such.However, even though you can easily convert an
Int
to aScalar[DType.index]
, trying to call this function with anInt
will fail since it can't implicitly parameterizetype
. So in order to maintain a clean interface one must write a second overload that acceptsInt
.I imagine this behaviour could become burdensome for numerical library writers.
Any other details?
No response
The text was updated successfully, but these errors were encountered: