DIVEX

Constructors as functions

To support OOP, DIVEX supports treating constructors as functions. Assuming that we have the following class defined:

public class Greeter
{
    private readonly string greeting;
    private readonly bool includeExclamation;

    public Greeter(string greeting, bool includeExclamation)
    {
        this.greeting = greeting;
        this.includeExclamation = includeExclamation;
    }

    public void Greet(string personName)
    {
        var message =  greeting + " " + personName + (includeExclamation ? "!" : "");

        Console.WriteLine(message);
    }
}

we can do the following to treat the constructor of Greeter as a function:

using System;
using DIVEX.Core;
using static DIVEX.Core.DivexUtils;

namespace SampleApplication
{
    [DivexCompose]
    public partial class Program
    {
        static void Main(string[] args)
        {
            var createGreeter = CtorOf<Greeter>();

            var createGreeterWithExclamation =
                createGreeter.Apply(includeExclamation: true);
        }
    }
}

CtorOf allows us to use a constructor as a function. It is a static method defined in the DIVEX.Core.DivexUtils class. And in the above code, I am using the using static directive to allow me to use the CtorOf method without having to type the name of the DivexUtils class (DivexUtils.CtorOf).