Jump to content

Nullary constructor

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by SlamDiego (talk | contribs) at 14:09, 28 August 2008 (Remove inappropriate tag. (1) Lede linked to foundational article. (2) Reader without idea of constructor quite unlikely to come to article. (3) Lede supposed to be short.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In computer programming, a nullary constructor is a constructor that takes no arguments.

Object-oriented constructors

In object-oriented programming, a constructor is code that is run when an object is created. A constructor that takes no arguments is usually called a default constructor.

Java example

public class Example 
{
   /* nullary constructor */
   public Example ()
   {
      this(1);
   }

   /* 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 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