Nullary constructor

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

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 
{
    protected int data;

    /* Nullary constructor */
    public Example()
    {
        this(0);
    }

    /* Non-nullary constructor */
    public Example(final int data)
    {
        this.data = 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
gollark: Oookay.
gollark: Wait, how would you know about stupidly expensive FTL travel for thousands of years before you had even figured out what the speed of light was?
gollark: There may also be different stuff produced in each system, or at least some stuff produced more cheaply in some.
gollark: I don't think anyone knows the answer to that.
gollark: … that would do it, why did I never think of that... but you still need room for the ship component stuff.
This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.