Edit: I’m rephrasing the question a bit. Apparently I caused some confusion because I didn’t realize that the term destructor is used in OOP for something quite different – it’s a function invoked when an object is being destroyed. In functional programming we (try to) avoid mutable state so there is no such equivalent to it. (I added the proper tag to the question.)
Instead, I’ve seen that the record field for unwrapping a value (especially for single-valued data types such as newtype
s) is sometimes called destructor or perhaps deconstructor. For example, let’s have (in Haskell):
newtype Wrap = Wrap { unwrap :: Int }
Here Wrap
is the constructor and unwrap
is what?
The questions are:
- How do we call
unwrap
in functional programming? Deconstructor? Destructor? Or by some other term? - And to clarify, is this/other terminology applicable to other functional languages, or is it used just in the Haskell?
- Perhaps also, is there any terminology for this in general, in non-functional languages?
I’ve seen both terms, for example:
… Most often, one supplies smart constructors and destructors for these to ease working with them. …
at Haskell wiki, or
… The general theme here is to fuse constructor – deconstructor pairs like …
at Haskell wikibook (here it’s probably meant in a bit more general sense), or
newtype DList a = DL { unDL :: [a] -> [a] }
The unDL function is our deconstructor, which removes the DL constructor. …
in The Real World Haskell.
4
There are several terms for the concept. Deconstructing is what I believe is common in Haskell circles, it is what Real World Haskell calls it. I believe the term destructuring(or destructuring bind) is common in Lisp circles.
2
Destructor is the term used by C++ and maybe other languages that I don’t know of. They are used to release ressources created by the constructor so they do the exact opposite of it. Maybe the concept does not translate literally to Haskell, but the term seems to be used at some places.
EDIT: Considering your edit to the question, I would call it an unwrapper then… I was able to answer the original question but now it has driven away from my knowledge so don’t take this edit too seriously.
5
In OOP, a constructor is a function or language construct that creates and initializes a new object (‘constructs’ the object), and a destructor is its dual, a function or language construct that cleans up after the object (by releasing any resources it holds) and deletes it.
However, since Haskell (unlike, say, C++) has garbage collection and doesn’t support mutable state or other side effects (at least not directly), there is absolutely no reason for a destructor notion in the OOP sense. Also, Haskell constructors, unlike OOP constructors, have more applications than just object creation; they are also heavily used in pattern matching (see below for an example).
In your code example, ‘unwrap’ is a record field, and depending on how it is used, I might refer to it as an accessor, or maybe even a getter (although the latter is also used in a lens context, so it might actually be a bit confusing).
I have never heard the term ‘destructor’ (or ‘deconstructor’) used in a Haskell context myself; as Andres F. notes, however, it is sometimes used to refer to a function that undoes the wrapping introduced by a constructor. To my understanding, a record getter may serve as a destructor, but so could regular functions, provided that they get a value back out of a more complex data type. Examples include maybe
and either
from the Prelude.
Note that unwrap
, in your example, can be several things:
- a function (just like any other): you can, for example, do
map unwrap [ Wrap 23, Wrap 42 ]
; this usage is roughly equivalent to a getter method in OOP. - a record field specifier in a record construction:
let w = Wrap { unwrap = 23 }
- a record field specifier in a record update:
let w' = w { unwrap = 23 }
; this is similar to setter methods in OOP (if you squeeze a lot). - a record field specifier in pattern matching:
f Wrap { unwrap = a } = a
It’s probably best to think of Haskell constructors as something altogether different from OOP constructors. I suggest you (re-)read a good book on the Haskell programming language for a better understanding – “Real World Haskell” is really good, and I’ve heard good things about “Learn You A Haskell”. Both should have good explanations about constructors and record syntax in particular.
7
Since there doesn’t seem to be any cross-language convention, I usually call them extractors, because that’s the term that Scala uses for that general notion, and because it’s not likely to be confused with any existing usage.
I think destructor is the broader term. Some languages like Java and VB.NET have a finalizer. https://stackoverflow.com/questions/171952/is-there-a-destructor-for-java
3