verve-lang
verve-lang copied to clipboard
Variadic types trick compiler / runtime.
Attempting to generalize the maybe type in the tests, I found two issues:
type maybe<type> {
Just(type)
None()
}
implementation printable<maybe<string>> {
fn to_string(a) {
match a {
Just(x) => "x"
None() => "None<string>"
}
}
}
fn print_maybe<type>(x: maybe<type>) -> void {
print(x) // not all maybes are printable
}
print(Just("abc"))
// maybe<int> is not printable...
print_maybe(Just(42))
$ ./verve input.vrv
1.53825e-154
1.53825e-154
I think the two issues are:
- the output of
print(Just("abc"))
is some undefined value (the number changes slightly between runs) interpreted as a float. - print_maybe can be used with non-printable maybes, (when one kind of maybe is printable.).
Other than the fact that the type checker is not that smart yet (😞), there are a couple hacks that I have to fix to make it more sane:
- The signature of
print
says it takes aprintable
, but it never callsto_string
in the value. It's an old version of the function that expects the pointer to be NaN-boxed and checks the type tag. - In the case that the print function doesn't recognise the type tag, it'll print anything as doubles: that was a quick hack to add double support without rewriting the whole print logic (I know, it's bad)
I'll try to fix these issues soon, and it should, at least, improve the error, instead of just printing garbage.
And here I thought I had discovered a horrid runtime flaw, and all I did was hit some temporary hack.
Accidentally closed it, just meant to mention it from the commit message... :(