tech

[English] [Japanese]

Box edition

This is information under consideration. Not currently available.

Nilable type

As mentioned above, nilable cannot be specified for the actual type parameter of Generics.

This restriction causes the following new Test( val ) to fail.

// @lnsFront: skip
class Test<T> {
   let val:T;
}
let val:int! = 1;
let test = new Test( val ); // error

There are two patterns to avoid this error.

  • Use T!
  • Use the Nilable type

In the case of "using T!", the above processing can be written as follows.

// @lnsFront: skip
class Test<T> {
   let val:T!; // <--- T! とする
}
let val:int! = 1;
let test = new Test( val );

However, in many cases, even if non-nilable is fine, it has to be declared as nilable, so unwrap processing becomes necessary, which is troublesome.

That's why we use the Nilable type.

What is a nilable type?

A Nilable type is a type that wraps a nilable value. Think of it as the Integer class for int in Java.