-
Notifications
You must be signed in to change notification settings - Fork 2.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
[BUG]: Mojo permits the use of any constructor for implicit conversions #1310
Comments
Int
to String
Int
/SIMD
/... to String
I was thinking that it is simply because String has a constructor that takes an Int value- however , now I am not so sure about how implicit conversions really work, and could not find in the programming manual where this is explained. Good question 👍🏽 |
@guidorice But the constructors are not called explicitly. Hard coding implicit type conversion for these types (counter example do exist, like Actually, I think the problem is more fundamental, as the following will also work. Too surprising at least for me. fn f(s: String):
print(s)
fn main():
f(1) Things can get more confusing really easily, if we use this kind of conversion pervasively:
The error says |
Int
/SIMD
/... to String
I like it, but an |
also, if you want to control this in the mean time, I've been wrapping the argument in a single tuple and it usually works. |
Implicit conversions are going to be headscratchers in the future. At least marking a constructor as @implicit would document that such a conversation is taking place. |
And I think it will work well with |
|
i wonder if we'll ever get the implicitest conversion |
Agree, we need to make implicit conversions be opt-in with an |
And fix the fn f(a: Int, b: String):
...
fn f(a: Float64, b: Float64):
...
f(1, 1) # calls the first one |
It does seem counter-intuitive, that the numeric types don't win in that overload. But the docs say the precedence is:
|
@guidorice I don't find it counter-intuitive, as the overload resolution rules are clear. I see this as an example of a foot-gun enabled by questionable library design, or more precisely, fn __init__[T: Stringable](x: T) -> String:
return x.__str__() After a bit more thinking, I actually do find it counter-intuitive. The |
One option for moving forward in the short-term would be to have an explicit |
@JoeLoser Why do we need |
This seems like it's going along the right lines, but special-casing The standard solution for this in strongly-typed languages with automatic promotion is to distinguish construction (takes an argument and creates a new object) from conversion (returns an alternative representation of the same number). For example, The rule is that >>> 4 == "4"
False
>>> 4 == 4.0
True So |
@scottamain @JoeLoser did tagging this issue documentation remove the mojo-stdlib label? There is probably more we could do in the docs, but I think this is mostly a stdlib issue (for specific conversions) and/or a Mojo issue (adding a way to separate implicit conversions from other constructor types. |
No, Will removed the stdlib tag, maybe because this is primarily a language behavior in terms of how implicit conversion happens. Also, this issue was reported before we published this documentation about implicit conversion, which describes the current behavior but without acknowledging this issue. I think we should wait until something changes before updating those docs, but maybe we should add a note with a link to this issue. |
@bethebunny has a proposal |
@bethebunny do you want to push on this some more or want @modularml/mojo-standard-library to run with it? |
Yea, consider this to be a bug too.. from tensor import Tensor
fn print_tensor[dtype: DType](x:Tensor[dtype]):
print(x)
fn main() raises:
print_tensor[DType.float64](Int(1))
print_tensor[DType.float64](1)
print_tensor[DType.float64](Tensor[DType.float64](1)) |
still waiting on the implicitest conversion |
recursively search for the path of least resistance from one type to another, and automatically follow it :] |
I have a more general question about this issue. Is any implicit conversion needed in the language? Would getting rid of it impact the usability of the language if we have automatic materialization ( |
Bug description
As title. Maybe it's working as intended, but I consider it a bug, for Mojo is not JavaScript. Is the behaviour there to circumvent the traitless-ness of
print
?Steps to reproduce
I think the following code shouldn't type check.
System information
The text was updated successfully, but these errors were encountered: