As you can write "Python" in any language I could have gone with "var foo interface{}" for everything and used type assertions (relying on the panic) on nearly every line
1
This isn't far from just using "foo" in python. For as long as things work, they work. Once they don't work you just get an exception with a trace-back. It's pretty much the same so far.
1
Obviously Go being a strictly typed language would not see anyone use "interface{}" for everything. That would be silly.
1
On top of that, it's pretty obvious to spot code that can panic. Type assertions can be used safely with the extended form that informs us of success or failure.
1
In addition, any real code-base that is meant to be sound and correct will carefully check all errors. Like you do in Go all the time, right?
1
I came to the conclusion that Go would have been better, at least for the type safety aspect. Some other aspects make it less practical, which made me... go with Python this time.
1
But this got me thinking. Was I hasty to the conclusion about type safety in one language over the other?
1
Let's start with some Python first. a =obj["a"] That's surely tricky from type-safety point of view. We can just craft things differently and use type-safe: a = obj.a With accompanying type certainty along the way.
1
What about this: a = obj[5] It doesn't even matter if this is Go or Python, now does it?
1
None of the languages I know of routinely model the size of an array in the type system. It's just not practical.
2
Replying to @zygoon
some languages model open slices differently to fixed length slices. Eg in rust the difference between [T] and [T; n] but this isn't used often and is rarely properly useful since indices are usually either zero or some variable which can't be compile-time checked anyway.

Dec 17, 2019 ยท 9:47 PM UTC

1