Constructors

Constructors are a way to create instances of objects. They are declared like a method, with the difference being the new keyword instead of a method name. The following class in Java:
class MyClass
{
public MyClass()
{
// ...
}
}
Looks like this in Dyvil:
class MyClass
{
public new()
{
// ...
}
}
This allows you to construct an instance of the MyClass class:
let myInstance = new MyClass
print myInstance // prints '[email protected]'
Constructors may also define parameters, just like methods:
class MyIntContainer
{
var value: int
public new(value: int)
{
this.value = value
}
func getValue() -> int = this.value
}
As with methods, you have to pass arguments for these parameters when invoking the constructor:
let container = new MyIntContainer(10)
print container.getValue // prints '10'
As shown above, empty parenthesis may be omitted from constructor calls.
Dyvil also provides a way to use constructors with generics. The following example shows just that:
class MyContainer<T>
{
var value: T
public new(value: T)
{
this.value = value
}
func getValue() -> T = this.value
}
let container = new MyContainer<String>("abc")
// inferred type MyContainer<String>
println container.getValue // prints 'abc'
In the above example, you don't have to explicitly declare the type argument. The compiler can also infer it from the type of the argument "abc" (which is String).
let container = new MyContainer("abc")
// inferred type MyContainer<String>
let value = container.getValue
// inferred type String