Last active
February 19, 2023 01:29
-
-
Save kubo39/4d2f3b42170c08c79819fd83c76cfcc5 to your computer and use it in GitHub Desktop.
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
import std.stdio; | |
void main() | |
{ | |
// 型情報の取得: : https://dlang.org/spec/type.html#typeof | |
alias rt = typeof(1.0); | |
// 型名: https://dlang.org/spec/property.html#stringof | |
writeln(rt.stringof); // double | |
// 型の種別? | |
import std.traits : isFloatingPoint; | |
static assert(isFloatingPoint!rt); | |
// 型の割り当てサイズ: https://dlang.org/spec/property.html#sizeof | |
writeln(rt.sizeof); // 8 | |
writeln("-- 配列(range?)の型情報?"); | |
// 型の重複はできないので連番で | |
alias rt2 = int[]; | |
// 要素の型情報 | |
import std.traits : ForeachType; | |
writeln(ForeachType!rt2.stringof); // int | |
writeln("-- 連想配列独自の型情報"); | |
alias rt3 = int[string]; | |
// 値の要素の型情報 | |
import std.traits : ValueType; | |
writeln(ValueType!rt3.stringof); // int | |
// キーの要素の型情報 | |
import std.traits : KeyType; | |
writeln(KeyType!rt3.stringof); // string | |
writeln("-- 構造体独自の型情報"); | |
// フィールド数 | |
import std.traits : Fields; | |
writeln(Fields!User.length); // 2 | |
// フィールドの型情報 | |
writeln(Fields!User.stringof); // (string, int) | |
// メソッド数、の代わりに非フィールドなもの(仮想関数ポインタや関数ポインタ)の数 | |
writeln(__traits(allMembers, User).length - Fields!User.length); // 1 | |
writeln("-- 関数の型情報"); | |
alias rt4 = typeof(hello); | |
// 引数の数 | |
import std.traits : arity; | |
writeln(arity!rt4); // 1 | |
// 引数の型情報 | |
import std.traits : Parameters; | |
writeln(Parameters!rt4.stringof); // (string) | |
// 返り値の型情報 | |
import std.traits : ReturnType; | |
writeln(ReturnType!rt4.stringof); // string | |
} | |
struct User | |
{ | |
string name; | |
int age; | |
string String() | |
{ | |
import std.format : format; | |
return format!"%s(%d)"(name, age); | |
} | |
} | |
string hello(string name) | |
{ | |
import std.format : format; | |
if (name == "invalid") | |
return ""; | |
return name.format!("Hello, %s"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment