Created
June 20, 2018 07:10
-
-
Save run-dlang/004dd297254ae3b99544e3d70d2b3ff4 to your computer and use it in GitHub Desktop.
Code shared from run.dlang.io.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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