mojo icon indicating copy to clipboard operation
mojo copied to clipboard

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

Open lovemyliwu opened this issue 1 year ago • 2 comments

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 avatar May 30 '23 02:05 lovemyliwu

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.

lattner avatar May 30 '23 21:05 lattner

+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 avatar Jun 05 '23 21:06 Mogball