What’s the proper term for a function inverse to a constructor – to unwrap a value from a data type?

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.

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật