Nullary constructor

In computer programming, a nullary constructor is a constructor that takes no arguments. Also known as a 0-argument constructor.

Object-oriented constructors

In object-oriented programming, a constructor is code that is run when an object is created. Default constructors of objects are usually nullary.

Java example

 public class Example 
 {
    /* nullary constructor */
    public Example ()
    {
       this(0);
    }
 
    /* non-nullary constructor */
    public Example (int data)
    {
       this.data = data;
    }
 
    protected int data;
 }

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.

Haskell example

 -- nullary type constructor with two nullary data constructors
 data Bool = False
           | True

 -- non-nullary type constructor with one non-nullary data constructor
 data Point a = Point a a

 -- non-nullary type constructor with...
 data Maybe a = Nothing -- ...nullary data constructor
              | Just a  -- ...unary data constructor


This article is issued from Wikipedia - version of the 4/5/2014. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.