Nullary constructor
From Wikipedia, the free encyclopedia
It has been suggested that this article or section be merged into Default constructor. (Discuss) |
The introduction to this article provides insufficient context for those unfamiliar with the subject. Please help improve the article with a good introductory style. |
The term constructor has different meanings depending on the context.
Contents |
[edit] OO constructors
In object-oriented programming, a constructor is code that is run when an object is created. A constructor that takes no arguments is called a default constructor.
[edit] Java example
public class Example { /* '''nullary constructor''' */ public Example () { this(1); } /* non-nullary constructor */ public Example (int data) { this.data = data; } protected int data; }
[edit] Algebraic data types
In algebraic data types, a constructor is one of many tags that wrap data. If a constructor does not take any data arguments, it is nullary.
[edit] Haskell example
-- nullary type constructor with two nullary data constructors data Bool = False | True -- non-nullary type constructor with non-nullary data constructor data Point a = Point a a -- nun-nullary type constructor with... data Maybe a = Nothing -- ...nullary data constructor | Just a -- ...unary data constructor