In computer programming, a nullary constructor is a constructor that takes no arguments.[1] Also known as a 0-argument constructor, no-argument constructor,[2] parameterless constructor or default constructor.[3]

Object-oriented constructors

edit

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

Java example

edit
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

edit
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

edit

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

edit
-- 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

See also

edit

References

edit
  1. ^ "Default Constructor in Java – Class Constructor Example". freeCodeCamp.org. 2022-01-13. Retrieved 2022-03-23.
  2. ^ "No-argument Constructor". chortle.ccsu.edu. Retrieved 2022-03-23.
  3. ^ "Default constructors - cppreference.com". en.cppreference.com. Retrieved 2023-04-12.
  4. ^ Ottinger, Joseph B.; Linwood, Jeff; Minter, Dave (2022), Ottinger, Joseph B.; Linwood, Jeff; Minter, Dave (eds.), "An Introduction to Hibernate 6", Beginning Hibernate 6: Java Persistence from Beginner to Pro, Berkeley, CA: Apress, pp. 1–25, doi:10.1007/978-1-4842-7337-1_1, ISBN 978-1-4842-7337-1, retrieved 2022-03-23{{citation}}: CS1 maint: work parameter with ISBN (link)


📚 Artikel Terkait di Wikipedia

Constructor (object-oriented programming)

In Java, a "default constructor" refer to a nullary constructor that is automatically generated by the compiler if no constructors have been defined for

Default constructor

constructors (e.g. in Java), and is usually a nullary constructor. In other languages (e.g. in C++) it is a constructor that can be called without having to provide

Type constructor

type constructor is a feature of a typed formal language that builds new types from old ones. Basic types are considered to be built using nullary type

Kind (type theory)

polymorphic data types to be type constructors, thus non-polymorphic types to be nullary type constructors. But all nullary constructors, thus all monomorphic types

Option type

particular case of a tagged union, where the Nothing is taken as (nullary constructor for a) singleton type. Tagged unions can generally be implemented

Tagged union

degenerate case: a tagged union of unit types. It corresponds to a set of nullary constructors and may be implemented as a simple tag variable, since it holds no

Enumerated type

ML (SML), OCaml, and Haskell), an algebraic data type with only nullary constructors can be used to implement an enumerated type. For example (in the

Comparison of programming languages (basic instructions)

^e Enumerations in this language are algebraic types with only nullary constructors ^f The value of n is provided by the SELECTED_INT_KIND intrinsic