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

String in fn definition allows any type to be used #17

Closed
mojodojodev opened this issue May 4, 2023 · 2 comments
Closed

String in fn definition allows any type to be used #17

mojodojodev opened this issue May 4, 2023 · 2 comments
Labels
mojo-repo Tag all issues with this label

Comments

@mojodojodev
Copy link
Contributor

mojodojodev commented May 4, 2023

Bug Description

String type in fn allows any type to be used

Steps to Reproduce

Create a notebook on the playground with:

from String import String
fn add_strict(a: Int, b: Int) -> Int:
    print("\nrunning through int impl")
    return b + a
fn add_strict(a: String, b: String) -> String:
   print("\nrunning through string impl")
   return a + b
print(add_strict("should this work: ", 8))
let eight: Int = 8
print(add_strict("should this work: ", eight))
print(add_strict("should this work: ", True))
print(add_strict(5, 10))
print(add_strict(True, False))

Resulting in everything that doesn't match the (Int,Int) overload running through (String,String) and concatenating

running through string impl
should this work: 8

running through string impl
should this work: 8

running through string impl
should this work: True

running through int impl
15

running through string impl
TrueFalse

Context

With a single (Int, Int) fn trying to use any type other than Int fails to compile correctly, but having String either by itself or overloaded will allow any type through, is this intentional?

@mojodojodev
Copy link
Contributor Author

mojodojodev commented May 4, 2023

Never mind I see there are __init__ methods for all those types in https://docs.modular.com/mojo/MojoStdlib/String.html makes sense

@dom96
Copy link

dom96 commented May 4, 2023

That's definitely surprising. Python's string doesn't even have this behaviour: you need to stringify explicitly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mojo-repo Tag all issues with this label
Projects
None yet
Development

No branches or pull requests

3 participants