What are traits in D?

Overview

In D, we use traits to get internal compiler information, which helps us use functions and variables differently and makes it more beneficial for programming.

Syntax

writeln(__traits(isFloating, float));
Syntax
  • Here, we use __traits to call the traits functionality. After that, we use isFloating to check if the argument is a float type. It returns true or false.

Example

import std.stdio;
void main() {
int i;
float t;
// Used the traits
writeln(__traits(isFloating, i));
writeln(__traits(isFloating, t));
}

Explanation

  • Lines 7 and 8: We use the traits method in which we use isFloating as the keyword. It identifies if the passed parameter is of the float type. It returns the result as true if the value is float. Otherwise, it returns false.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved