Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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 newtypes) 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.

share|improve this question
3  
Destructor seems to be the most widely used term –  Zavior Oct 12 '12 at 12:25
3  
This seems specialized to Haskell (a "destructor" in Haskell isn't the same concept as in C++. The name similarity is misleading). If you want the general concept of "unwrapping a value", you probably shouldn't use Haskell as an example. In C++-like languages, such an "unwrapper" may be simply a getter! –  Andres F. Oct 12 '12 at 12:49
1  
I suggest you remove the word "destructor" from the question, use "unwrapper" instead, and ask whether there is a general name for this pattern across languages :) –  Andres F. Oct 12 '12 at 12:50
1  
.unapply, or "extractor" is a rough analog in scala, but maybe there's no name because in most cases you simply pattern match –  Gene T Nov 3 '12 at 21:27

5 Answers 5

up vote 4 down vote accepted

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.

share|improve this answer
1  
Interesting how the same thing gets a different term from one language to the next, like map turns into select in C#, haskell has fold lisp/java has reduce and ruby has inject, haskell has bind scala has flatmap C# has selectmany –  Jimmy Hoffa Oct 12 '12 at 17:05
    
It is indeed called deconstruction in the Haskell circles. If it sounds odd, keep in mind that Haskell has no objects - just values; So it goes without saying that the term has a different meaning. The great part is that 'deconstruction' is exactly what it does. On that note I personally think object deconstructors should be called something along the lines of object disposers. It is much, much more accurate. –  MasterMastic Oct 24 at 15:13

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.

share|improve this answer

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.

share|improve this answer
1  
Note that C++/C#/Java's usage of the term "destructor" doesn't match Haskell's. Same word, different concepts. –  Andres F. Oct 12 '12 at 12:46
1  
@AndresF. Yes, that's why I added the sentence : Maybe in Haskell this does not translate really well but I don't know the language. –  marco-fiset Oct 12 '12 at 12:54
1  
Fair enough, I removed my downvote. I think what's misguided is the actual question, which seems to be asking one thing, but is actually asking another! :) –  Andres F. Oct 12 '12 at 12:56
1  
Just a note, C# has finalizers but not destructors, which are different because finalizers have non-deterministic execution. So maybe edit your answer to not spread the misnomer of destructors in C#. –  Jimmy Hoffa Oct 12 '12 at 15:06
1  
@JimmyHoffa Thanks for pointing that out! Fixed. –  marco-fiset Oct 12 '12 at 15:07

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.

share|improve this answer
2  
Evidence of usage of the term "destructor" in Haskell communities: haskell.org/pipermail/beginners/2010-April/003946.html and mail-archive.com/haskell-cafe@haskell.org/msg26463.html . A "destructor" in Haskell lingo is an "unwrapper", a concept totally unrelated to memory management or object finalization. –  Andres F. Oct 12 '12 at 14:42
3  
My thesis director, who programs exclusively in Haskell, also uses the term "destructor" for an unwrapper. –  Andres F. Oct 12 '12 at 14:43
    
Well, I had never come across the term. You are absolutely right that, when used, it has absolutely nothing to do with the destructor concept in OOP. –  tdammers Oct 12 '12 at 15:01
    
@AndresF.: Edited to honor your remarks. –  tdammers Oct 12 '12 at 15:08
    
@AndresF. This is interesting as I also never heard it referred to as a destructor, though that aside, would it not also be accurate to just refer to it as a record field, and the technique I see called "destructor" there as just a pattern match? I understand it's called "destructuring" when your match is pulling a list apart, is this perhaps a related term but in reference to an ADT? After all destructuring a list is just acting on an ADT as that's what a list is.. I got the term destructuring from stackoverflow.com/questions/11677806/… –  Jimmy Hoffa Oct 12 '12 at 15:10

I think destructor is the broader term. Some languages like Java and VB.NET have a finalizer. http://stackoverflow.com/questions/171952/is-there-a-destructor-for-java

share|improve this answer
    
A destructor in C++-like languages performs cleanup actions before releasing allocated memory. A destructor in Haskell unwraps a value from its "outer layer" (you don't use it to cleanup anything). They aren't truly related concepts. –  Andres F. Oct 12 '12 at 12:54
2  
I guess I didn't realize this is a Haskel question. –  JeffO Oct 12 '12 at 15:33
    
It wasn't clear indeed. The question has now been retagged and reformulated :) –  Andres F. Oct 12 '12 at 16:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.