Skip to content

Instantly share code, notes, and snippets.

@run-dlang
Created June 20, 2018 07:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save run-dlang/004dd297254ae3b99544e3d70d2b3ff4 to your computer and use it in GitHub Desktop.
Save run-dlang/004dd297254ae3b99544e3d70d2b3ff4 to your computer and use it in GitHub Desktop.
Code shared from run.dlang.io.
void main()
{
// Define an array of numbers, double[].
// Compiler recognizes the common
// type of all initializers.
auto arr = [ 1, 2, 3.14, 5.1, 6 ];
// Dictionary that maps string to int,
// type is spelled int[string]
auto dictionary = [ "one" : 1, "two" : 2,
"three" : 3 ];
// Calls the min function defined below
auto x = min(arr[0], dictionary["two"]);
}
// Type deduction works for function results.
// This is important for generic functions,
// such as min below, which works correctly
// for all comparable types.
auto min(T1, T2)(T1 lhs, T2 rhs)
{
return rhs < lhs ? rhs : lhs;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment