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

[Feature Request] Implicitly declared variables in def should default to object type #290

Closed
lovemyliwu opened this issue May 30, 2023 · 2 comments
Labels
enhancement New feature or request mojo Issues that are related to mojo mojo-repo Tag all issues with this label

Comments

@lovemyliwu
Copy link

Bug description

in global scope, define variable normally, change variable value type work fine.
in function local scope, change variable value type cause compile-time error.

Steps to reproduce

a=None
a=1
a=1.0
a='hello'
print(a)

def your_function():
    a=None
    # uncomment below will cause error
    #a=1
    #a=1.0
    #a='world'
    print(a)

your_function()


### System information

```shell
11:hugetlb:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod029c02b1_c89f_4674_b551_48e4b08f59a7.slice/cri-containerd-b46dbfc6830da1403a6a7c6a3422b452326bbc9473f7f26618dc58226d027e37.scope
10:memory:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod029c02b1_c89f_4674_b551_48e4b08f59a7.slice/cri-containerd-b46dbfc6830da1403a6a7c6a3422b452326bbc9473f7f26618dc58226d027e37.scope
9:cpu,cpuacct:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod029c02b1_c89f_4674_b551_48e4b08f59a7.slice/cri-containerd-b46dbfc6830da1403a6a7c6a3422b452326bbc9473f7f26618dc58226d027e37.scope
8:perf_event:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod029c02b1_c89f_4674_b551_48e4b08f59a7.slice/cri-containerd-b46dbfc6830da1403a6a7c6a3422b452326bbc9473f7f26618dc58226d027e37.scope
7:devices:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod029c02b1_c89f_4674_b551_48e4b08f59a7.slice/cri-containerd-b46dbfc6830da1403a6a7c6a3422b452326bbc9473f7f26618dc58226d027e37.scope
6:freezer:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod029c02b1_c89f_4674_b551_48e4b08f59a7.slice/cri-containerd-b46dbfc6830da1403a6a7c6a3422b452326bbc9473f7f26618dc58226d027e37.scope
5:blkio:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod029c02b1_c89f_4674_b551_48e4b08f59a7.slice/cri-containerd-b46dbfc6830da1403a6a7c6a3422b452326bbc9473f7f26618dc58226d027e37.scope
4:net_cls,net_prio:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod029c02b1_c89f_4674_b551_48e4b08f59a7.slice/cri-containerd-b46dbfc6830da1403a6a7c6a3422b452326bbc9473f7f26618dc58226d027e37.scope
3:cpuset:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod029c02b1_c89f_4674_b551_48e4b08f59a7.slice/cri-containerd-b46dbfc6830da1403a6a7c6a3422b452326bbc9473f7f26618dc58226d027e37.scope
2:pids:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod029c02b1_c89f_4674_b551_48e4b08f59a7.slice/cri-containerd-b46dbfc6830da1403a6a7c6a3422b452326bbc9473f7f26618dc58226d027e37.scope
1:name=systemd:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod029c02b1_c89f_4674_b551_48e4b08f59a7.slice/cri-containerd-b46dbfc6830da1403a6a7c6a3422b452326bbc9473f7f26618dc58226d027e37.scope
0::/
@lovemyliwu lovemyliwu added the bug Something isn't working label May 30, 2023
@lattner
Copy link
Collaborator

lattner commented May 30, 2023

This is because the top-level scope is "weird and magic" due to how the notebooks work. We need to build out the model to be more consistent for sure.

Within a function, implicitly declared variables get the type of their first value assigned into them. This is probably not the right thing - within a def, we will need to maintain dynamic typing (including type transformations like python has) for compatibility. Our base object isn't super built out and set up for this yet, which is why we have a "default to the first type" approach.

@Mogball
Copy link
Collaborator

Mogball commented Jun 5, 2023

+1 to what Chris said. Top-level code inside a notebook has different scoping and typing semantics than inside a function, fn or def. One of the things should be that implicitly declared variables in a def should be object type, but there are still limitations with the object type.

@Mogball Mogball changed the title [BUG]: local var can not change value type but global var can [Feature Request] Implicitly declared variables in def should default to object type Jun 5, 2023
@Mogball Mogball added enhancement New feature or request and removed bug Something isn't working labels Jun 5, 2023
@ematejska ematejska added the mojo Issues that are related to mojo label Sep 7, 2023
@ematejska ematejska added the mojo-repo Tag all issues with this label label Apr 29, 2024
@linear linear bot closed this as completed May 2, 2024
@linear linear bot reopened this May 2, 2024
@linear linear bot closed this as completed May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request mojo Issues that are related to mojo mojo-repo Tag all issues with this label
Projects
None yet
Development

No branches or pull requests

4 participants