codon
codon copied to clipboard
cannot typecheck program with empty list initializaton
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);
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)
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)