Hacker News new | past | comments | ask | show | jobs | submit login

I agree with most of your comment, but I think there's a miscommunication problem here.

When Python "enforces types at runtime" this is the actual types, not type hints. Type hints are not a runtime artifact. The "actual type" here is "string", and enforcing it means Python would not allow invalid operations on it without error'ing. A programming language that will let you do basically anything to any value because it doesn't enforce type safety is of course C. Python is safer than C.




Yes and no. C will let you do things like:

  *(unsigned long*)0xFFFFFF14 = 0x749235f8;
It will not let you do things like:

  *0xFFFFFF14 = 0x749235f8;
or

   char* s = "abc";
   *(unsigned long*)0xFFFFFF14 = s;
It also won't let you call thing.method without thing.method definitely existing.

Best of all, all of these are enforced at compile time.

Now, C absolutely will let you convert an int to a pointer, or a char to an int, or an array to a pointer, or... well, it will let you convert lots of things to lots of things. Some of those things are unsafe unless you are quite sure what you're doing.

> A programming language that will let you do basically anything to any value because it doesn't enforce type safety is of course C.

False on several grounds. You can't call something that isn't a function. You can't call a function on something that doesn't have it. You can't use an int as a pointer or an array without casting it. You can't use an int as a dict (not that C has any built-in idea of what a dict is...)

> Python is safer than C.

Depends on what you're doing, and what kinds of errors you are more prone to.


> You can't call something that isn't a function.

Sure you can. That's exactly what you do when you call a function returned by `dlopen` -- it might be a function, but it might also just be garbage. It might even be garbage that's coincidentally marked as executable and does some stuff before eventually crashing!

> You can't call a function on something that doesn't have it.

This is also very easy to do -- you can default-initialize a structure containing function pointers, and then call one of them. You'll be calling some random crap on the stack, which might or might not do anything of interest. But either way, C will happily let you do it.

C isn't completely untyped (there are, as you've observed, things it will forbid at compile time), but it's about as weak as a static typing system can be.


I think you're talking past the comment you're replying to.

In both examples, you have tell the compiler exactly what the types are. In the first one you have to cast the return value of `dlsym` to a function pointer, and at that point, as far as the compiler is concerned, it's a function. In the second example you have to explicitly declare the struct containing function pointers, so of course you can call its members.

So it's strange to suggest that C is somewhat untyped; it's not untyped at all. C just makes it very easy to force the compiler to assume different type for a value (that's necessary for low-level code like the examples in the comment you replied to; and of course that makes it very easy to do unsafe things, I don't think anyone disputes that).

And about the "weak typing system" comment, note that nobody agrees on what exactly that means. Just as an example, see how this page[1] defines "weaker" vs "stronger" types, and how it's completely different from the way you're using here. In general, I find people call type systems that don't do what they expect or want "weak", but that's not an useful discriminator: at that point you might just call it "bad typing" to dispel any illusion that it has any objective meaning.

[1] http://book.realworldhaskell.org/read/types-and-functions.ht...


I didn't say it was "somewhat untyped." I said it has a very weak static typing system.

Just because "weak typing" means something different in Haskell doesn't mean it can't be used meaningfully (and beyond "bad") in the context of C. C's typing is historically referred to as "weak" because C's notion of casting doesn't distinguish between type and value transmutation: pointer-to-pointer casts don't convert the referent (because they can't), which in turn gives the C compiler very little leeway in proving that the program's types as declared have any particular meaning at runtime.

Compare this to Python, which is "strongly" typed in the same sense: doing `y = str(x)` on `x` means that `type(y)` actually is `str`, and not merely a promise to the runtime. It's enforced, which makes it strong.


My point about "weak typing" is this: if someone says "hey, I'm designing a new language, its type system is going to be weak", they gave you no information whatsoever about the type system other than maybe "some people on the Internet will probably complain about it".

That's why it's useless to say things like "it's about as weak as a static typing system can be" and "has been historically considered weak".

If you had explained your objection to C's type system (like you have now), I wouldn't have said anything. And I've been on the Internet long enough to have seen C's typing is historically referred to as "weak" because X for *many* values of X, so I disagree that that's the only or even main objection.


I find this level of nitpicking puzzling.

You know what I meant and what error I was clarifying for the comment I was replying to. Yet you went out of your way to point all sorts of irrelevant mistakes in my post, when the gist of it was right.

Do you feel it was more important to correct me on these trivial things, or was my reply more or less correct when fixing the conceptual error in this sentence?:

> "In what reasonable sense can Python be said to "enforce types at runtime" here?"

Here's a nitpick of my own to your post:

> "Some of those things are unsafe unless you are quite sure what you're doing."

Wrong. Type (un)safety does not depend on you "being quite sure of what you're doing".

PS: assume I know C and that I've programmed complex things using it. No need to catch yourself with statements like "not that C has any idea of what a dict is". Assume we are both C programmers. It will make you sound more polite.


1. You might need to re-read the site guidelines about assuming good faith.

2. Your last two sentences were, I think, not needed for the point you were making. They were also somewhere between gross generalizations and flat-out wrong, and felt like a gratuitous slam on a language that wasn't even the topic. I thought that deserved a response, even if it wasn't your main point.

3. I may assume that you know C. I don't assume that everyone reading this exchange knows C.


> "They were also somewhere between gross generalizations"

Pointless correction. Gross generalizations are not wrong in this particular situation, which is not meant to teach anyone C.

Let me try again.

Do you think that your correction was more important than helping the poster of this comment correct their conceptual mistake?

> "In what reasonable sense can Python be said to "enforce types at runtime" here?"


All true but all rather irrelevant to their point




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: