codon icon indicating copy to clipboard operation
codon copied to clipboard

cannot typecheck program with empty list initializaton

Open jplevyak opened this issue 1 year ago • 2 comments

def f(a, L=[]):
  L.append(a)
  return L
print(f(1))
print(f(2))

python3 returns [1] [1, 2]

codon says: "error: cannot typecheck the program"

No only is that not a particularly useful error, this is the sort of type inference which is very useful, e.g. Rust allows the idiom:

let mut vec = Vec::new(); vec.push(1); vec.push(2);

jplevyak avatar Mar 12 '23 00:03 jplevyak

One of the current restrictions/limitations of the type system is that type-checking happens per-function, so the external default empty-list argument can't be type checked (@inumanag can explain further).

Note that the general empty list pattern does work, though:

vec = []  # this is deduced to be a list of int
vec.append(1)
vec.append(2)

arshajii avatar Mar 12 '23 13:03 arshajii

Could you elaborate on what kind of empty list initialization work and what doesn't work? I don't quite understand here.

One of the current restrictions/limitations of the type system is that type-checking happens per-function, so the external default empty-list argument can't be type checked (@inumanag can explain further).

Note that the general empty list pattern does work, though:

vec = []  # this is deduced to be a list of int
vec.append(1)
vec.append(2)

jiangtianli91 avatar Mar 21 '23 13:03 jiangtianli91