Hacker News new | ask | show | jobs
by Groxx 20 days ago
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.

2 comments

Eiffel or Smalltalk are two languages, among others I could chose from, where primitives and others types are differently implemented, yet unless you dive into the nitty gritty details, they look exactly the same to the developer as the compiler handles them exactly the same, and there are no extension methods to be seen.
However, it would be impossible to implement a #toggle method in Smalltalk that behaves like this, because you're calling it on the singleton True (or False) object; Smalltalk local variables, like Lisp variables, are references to objects rather than objects themselves.

(You obviously know this, but it might be a surprise to many people reading this thread.)

Yes, but that is orthogonal how primitives and reference types get exposed to the developer.

Something like toggle is also impossible in languages that favour immutability and forward progress requires creating new variables, like several functional and logic languages.

Agreed.
Go is fine with pointer methods (comparable to Rust &mut self) on types that are structurally "primitives". (Go predeclared types do not have methods.)

    package main
    
    import "fmt"
    
    type T int32
    
    func (t *T) inc() { *t = *t + 1 }
    
    func main() {
     var t T = 42
     t.inc()
     fmt.Println(t)
    }