←back to thread

451 points birdculture | 3 comments | | HN request time: 0.734s | source
Show context
GenshoTikamura ◴[] No.43982864[source]
> Stop resisting. That’s the most important lesson

> Accept that learning Rust requires...

> Leave your hubris at home

> Declare defeat

> Resistance is futile. The longer you refuse to learn, the longer you will suffer

> Forget what you think you knew...

Now it finally clicked to me that Orwell's telescreen OS was written in Rust

replies(1): >>43983002 #
atoav ◴[] No.43983002[source]
But it is true. My own biggest mistake when learning Rust was that I tried to torce Object Oriented paradigms on it. That went.. poorly. As soon as I went "fuck it, I just do it like you want" things went smoothly.
replies(2): >>43984144 #>>43987271 #
rikafurude21 ◴[] No.43984144[source]
Sounds like an abusive relationship if im being honest. Your programming language shouldnt constrict you in those ways.
replies(5): >>43984486 #>>43984523 #>>43984615 #>>43984649 #>>43993404 #
hbn ◴[] No.43984615[source]
Every programming language has constrictions by the nature of having syntax.

In JavaScript you can declare a variable, set it to 5 (number), and then set it to the "hello" (string), but that's not allowed in e.g. C. Is C constricting me too much because I have to do it in C's way?

replies(1): >>43986014 #
dgfitz ◴[] No.43986014[source]
I believe you can do that in C pretty easily with a void pointer, someone correct me if I'm mistaken.

Should you? Different question entirely.

replies(2): >>43988260 #>>44003293 #
int_19h ◴[] No.44003293[source]
Pedantically speaking, you can't. You can have a variable that points to an integer 5, and then make it point to a character array "hello\0". But the value of the variable is a pointer in both cases.

Curiously enough, this is also true of Python - just less obvious because it doesn't have any variables that aren't pointers, and most operators perform an implicit dereference.

replies(1): >>44013551 #
1. boomlinde ◴[] No.44013551[source]
> Pedantically speaking, you can't. You can have a variable that points to an integer 5, and then make it point to a character array "hello\0". But the value of the variable is a pointer in both cases.

Unions may be the better analog here.

> Curiously enough, this is also true of Python - just less obvious because it doesn't have any variables that aren't pointers, and most operators perform an implicit dereference.

That's more like an implementation detail than a quality of the language itself. A JavaScript implementation might do the same. For example, values in V8 are all either pointers to the heap except in the case of small (31-bit) integers, and a less optimized implementation might not even make that distinction and allocate everything on the heap. Similarly, a Python implementation might store SMIs directly where the pointer would be, like V8. PyPy uses both tagged pointers/SMIs and may even allocate registers for values.

replies(1): >>44018547 #
2. int_19h ◴[] No.44018547[source]
> Unions may be the better analog here.

Tagged unions would be, except they aren't first class in C.

The best analogy here would probably be OCaml polymorphic variants (https://dev.realworldocaml.org/variants.html)

> That's more like an implementation detail than a quality of the language itself.

It is not, though, because - unlike JavaScript - the fact that everything is a reference to object, and each object has a unique identity, is explicitly a part of Python semantics, and this is very visible in many cases. It's easy to observe it for "primitive" types as well simply by inheriting from them.

(OTOH the fact that a given Python implementation might still implement this by using tagged pointers etc is an implementation detail, because it is still required to behave as-if everything was an object).

replies(1): >>44020135 #
3. boomlinde ◴[] No.44020135[source]
> Tagged unions would be, except they aren't first class in C.

For the originally stated example, 'declare a variable, set it to 5 (number), and then set it to the "hello" (string)', a plain union is just fine.

> The best analogy here would probably be OCaml polymorphic variants

It would be, except OCaml is not C.

> It is not, though, because - unlike JavaScript - the fact that everything is a reference to object

Evidently, references and pointers are not the same thing, hence it is an implementation detail whether references correspond to pointers.

> and each object has a unique identity

Which doesn't necessarily have anything to do with their address. It's an opaque value guaranteed to be unique for an object for the duration of its lifetime.

Meanwhile, C pointer values aren't necessarily unique for different objects. For example, a pointer to a struct with one or more fields shares value with a pointer to its first field. It's only once you factor in type that pointers uniquely identify a particular object, so they don't exactly correspond to Python's object identity.