diff --git a/changelog/std-experimental-scripting.dd b/changelog/std-experimental-all.dd similarity index 74% rename from changelog/std-experimental-scripting.dd rename to changelog/std-experimental-all.dd index 372edd35b..0ccfe2ec3 100644 --- a/changelog/std-experimental-scripting.dd +++ b/changelog/std-experimental-all.dd @@ -1,10 +1,10 @@ -`import std.experimental.scripting` as a global convenience import +`import std.experimental.all` as a global convenience import -$(MREF std,experimental,scripting) allows convenient use of all Phobos modules +$(MREF std,experimental,all) allows convenient use of all Phobos modules with one import: --- -import std.experimental.scripting; +import std.experimental.all; void main() { 10.iota.map!log.sum.writeln; @@ -13,11 +13,11 @@ void main() For short scripts a lot of imports are often needed to get all the modules from the standard library. -With this release it's possible to use `import std.experimental.scripting` for importing the entire +With this release it's possible to use `import std.experimental.all` for importing the entire standard library at once. This can be used for fast prototyping or REPLs: --- -import std.experimental.scripting; +import std.experimental.all; void main() { 6.iota @@ -34,6 +34,6 @@ In this case, $(LINK2 $(ROOT)spec/module.html#static_imports, static imports) or $(LINK2 $(ROOT)spec/module.html#renamed_imports, renamed imports) can be used to uniquely select a specific symbol. -The baseline cost for `import std.experimental.scripting` +The baseline cost for `import std.experimental.all` is less than half a second (varying from system to system) and work is in progress to reduce this overhead even further. diff --git a/posix.mak b/posix.mak index 0f5c7e171..2a72e34dd 100644 --- a/posix.mak +++ b/posix.mak @@ -195,7 +195,7 @@ PACKAGE_std = array ascii base64 bigint bitmanip compiler complex concurrency \ outbuffer parallelism path process random signals socket stdint \ stdio string system traits typecons uni \ uri utf uuid variant xml zip zlib -PACKAGE_std_experimental = checkedint typecons scripting +PACKAGE_std_experimental = all checkedint typecons PACKAGE_std_algorithm = comparison iteration mutation package searching setops \ sorting PACKAGE_std_container = array binaryheap dlist package rbtree slist util diff --git a/std/exception.d b/std/exception.d index da50410ae..957e844d6 100644 --- a/std/exception.d +++ b/std/exception.d @@ -33,35 +33,6 @@ $(TR $(TD Other) $(TD )) ) - Synopsis of some of std.exception's functions: - -------------------- - string synopsis() - { - FILE* f = enforce(fopen("some/file")); - // f is not null from here on - FILE* g = enforce!WriteException(fopen("some/other/file", "w")); - // g is not null from here on - - Exception e = collectException(write(g, readln(f))); - if (e) - { - ... an exception occurred... - ... We have the exception to play around with... - } - - string msg = collectExceptionMsg(write(g, readln(f))); - if (msg) - { - ... an exception occurred... - ... We have the message from the exception but not the exception... - } - - char[] line; - enforce(readln(f, line)); - return assumeUnique(line); - } - -------------------- - Copyright: Copyright Andrei Alexandrescu 2008-, Jonathan M Davis 2011-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0) Authors: $(HTTP erdani.org, Andrei Alexandrescu) and Jonathan M Davis @@ -70,6 +41,73 @@ $(TR $(TD Other) $(TD +/ module std.exception; +/// Synopis +@system unittest +{ + import core.stdc.stdlib : malloc, free; + import std.algorithm.comparison : equal; + import std.algorithm.iteration : map, splitter; + import std.algorithm.searching : endsWith; + import std.conv : ConvException, to; + import std.range : front, retro; + + // use enforce like assert + int a = 3; + enforce(a > 2, "a needs to be higher than 2."); + + // enforce can throw a custom exception + enforce!ConvException(a > 2, "a needs to be higher than 2."); + + // enforce will return it's input + enum size = 42; + auto memory = enforce(malloc(size), "malloc failed")[0 .. size]; + scope(exit) free(memory.ptr); + + // collectException can be used to test for exceptions + Exception e = collectException("abc".to!int); + assert(e.file.endsWith("conv.d")); + + // and just for the exception message + string msg = collectExceptionMsg("abc".to!int); + assert(msg == "Unexpected 'a' when converting from type string to type int"); + + // assertThrown can be used to assert that an exception is thrown + assertThrown!ConvException("abc".to!int); + + // ifThrown can be used to provide a default value if an exception is thrown + assert("x".to!int().ifThrown(0) == 0); + + // handle is a more advanced version of ifThrown for ranges + auto r = "12,1337z32,54".splitter(',').map!(a => to!int(a)); + auto h = r.handle!(ConvException, RangePrimitive.front, (e, r) => 0); + assert(h.equal([12, 0, 54])); + assertThrown!ConvException(h.retro.equal([54, 0, 12])); + + // basicExceptionCtors avoids the boilerplate when creating custom exceptions + static class MeaCulpa : Exception + { + mixin basicExceptionCtors; + } + e = collectException((){throw new MeaCulpa("diagnostic message");}()); + assert(e.msg == "diagnostic message"); + assert(e.file == __FILE__); + assert(e.line == __LINE__ - 3); + + // assumeWontThrow can be used to cast throwing code into `nothrow` + void exceptionFreeCode() nothrow + { + // auto-decoding only throws if an invalid UTF char is given + assumeWontThrow("abc".front); + } + + // assumeUnique can be used to cast mutable instance to an `immutable` one + // use with care + char[] str = " mutable".dup; + str[0 .. 2] = "im"; + immutable res = assumeUnique(str); + assert(res == "immutable"); +} + import std.range.primitives; import std.traits; diff --git a/std/experimental/scripting.d b/std/experimental/all.d similarity index 94% rename from std/experimental/scripting.d rename to std/experimental/all.d index 8b666bae1..60da41607 100644 --- a/std/experimental/scripting.d +++ b/std/experimental/all.d @@ -1,12 +1,12 @@ /++ Convenience file that allows to import entire Phobos in one command. +/ -module std.experimental.scripting; +module std.experimental.all; /// @safe unittest { - import std.experimental.scripting; + import std.experimental.all; int len; const r = 6.iota @@ -23,7 +23,7 @@ module std.experimental.scripting; /// @safe unittest { - import std.experimental.scripting; + import std.experimental.all; assert(10.iota.map!(partial!(pow, 2)).sum == 1023); } diff --git a/win32.mak b/win32.mak index fa91b8b91..f6ef5e5d5 100644 --- a/win32.mak +++ b/win32.mak @@ -284,7 +284,7 @@ SRC_STD_INTERNAL_WINDOWS= \ std\internal\windows\advapi32.d SRC_STD_EXP= \ - std\experimental\checkedint.d std\experimental\typecons.d std\experimental\scripting.d + std\experimental\all.d std\experimental\checkedint.d std\experimental\typecons.d SRC_STD_EXP_ALLOC_BB= \ std\experimental\allocator\building_blocks\affix_allocator.d \ diff --git a/win64.mak b/win64.mak index 82e670cdb..29dfaa568 100644 --- a/win64.mak +++ b/win64.mak @@ -309,7 +309,7 @@ SRC_STD_INTERNAL_WINDOWS= \ std\internal\windows\advapi32.d SRC_STD_EXP= \ - std\experimental\checkedint.d std\experimental\typecons.d std\experimental\scripting.d + std\experimental\all.d std\experimental\checkedint.d std\experimental\typecons.d SRC_STD_EXP_ALLOC_BB= \ std\experimental\allocator\building_blocks\affix_allocator.d \