logoalt Hacker News

ninkendo • yesterday at 11:01 PM • 1 reply • view on HN

There’s nothing special about primitives, they’re just types like any other. A method taking `&mut self` can always modify self, on any type. But you can only call such methods if the value is mutable to begin with, so it’s not really that interesting.

It’s only weird if you come from languages like Java where “primitives” are distinct from “objects”, which is a self-imposed limitation the language imposes.


Replies

Groxx • yesterday at 11:13 PM

Or Go (also treats primitives rather specially) or roughly any language without extension methods (because primitives are at a minimum a pre-defined type). Which is quite a lot of languages really.

Otherwise agreed, methods like this can be mutable in any language that doesn't heavily specialize primitives.