4 ms·
> in Haskell a value is a value The same is true of C++. Values and references in C++ can't be null. Pointers can be null, but that's a lower-level feature tha
by TheCoelacanth 13y ago
> in Haskell a value is a value
The same is true of C++. Values and references in C++ can't be null. Pointers can be null, but that's a lower-level feature that doesn't have an equivalent in idiomatic Haskell.
- dllthomas 13y ago"Ptr a" is an equivalent, in many senses. It's quite true that use is far less in idiomatic Haskell, especially as you're restricted to IO when operating on them.
- swift 13y agoPtr is exactly the same thing as a C pointer. Critically, if you have a Ptr to some value, that Ptr could become invalid if the value gets moved by the garbage collector, just like a C pointer would. That's why you'd never use Ptr except in FFI code, to access memory that isn't managed by Haskell's GC. (If you need to pass a pointer to a Haskell value to code written in another language, you'd use a StablePtr, which pins the value in the Haskell heap.) In idiomatic Haskell, if you need something like a pointer you have several choices. If you're writing parallel code your best bet is STM. If you're writing code in IO, you can use an IORef. If you don't want that restriction, you can use an STRef. (I'm aware that you probably know all this, but other people reading the thread might not.)
- dllthomas 13y agoRight, it's "exactly the same thing" under the hood. It's different in the roles it serves. Your elaboration is appreciated!