Nullary constructor
≈ 1 min read
Names in other languages
No names in other languages are held for this entry yet.
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
In computer programming, a nullary constructor is a constructor that takes no arguments. Also known as a 0-argument constructor, no-argument constructor, parameterless constructor or default constructor.
In computer programming, a nullary constructor is a constructor that takes no arguments. Also known as a 0-argument constructor, no-argument constructor, parameterless constructor or default 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 MyInteger { private int data; // Nullary constructor public MyInteger() { this(0); } // Non-nullary constructor public MyInteger(int value) { this.data = value; } int getData() { return data; } void setData(int value) { data = value; } } ===C++ example=== class Integer { private: int data; public: // Default constructor with parameters // Leaving parameters unspecified defaults to the default value Integer(int value = 0): data{value} {} nodiscard int getData() const noexcept { return data; } void setData(int value) noexcept { data = value; } } ==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
Text derived from https://en.wikipedia.org/w/index.php?oldid=1360713875 under CC BY-SA 4.0; modifications have been made.