mirror of
https://github.com/dlang/phobos.git
synced 2025-04-26 05:00:35 +03:00
Add sumtype to Phobos (#7702)
Add sumtype to Phobos merged-on-behalf-of: unknown
This commit is contained in:
parent
cb3f5e29ab
commit
51a70ee267
9 changed files with 2558 additions and 7 deletions
|
@ -199,6 +199,7 @@ assert_without_msg="-etc.c.SQL_,\
|
|||
-std.socket,\
|
||||
-std.stdio,\
|
||||
-std.string,\
|
||||
-std.sumtype,\
|
||||
-std.traits,\
|
||||
-std.typecons,\
|
||||
-std.uni,\
|
||||
|
@ -211,7 +212,7 @@ assert_without_msg="-etc.c.SQL_,\
|
|||
; Checks for assignment to auto-ref function parameters
|
||||
auto_ref_assignment_check="-std.algorithm.mutation,-std.typecons"
|
||||
; Checks for variables that could be declared immutable
|
||||
could_be_immutable_check="-std.algorithm.comparison,-std.algorithm.iteration,-std.algorithm.mutation,-std.algorithm.searching,-std.algorithm.setops,-std.algorithm.sorting,-std.array,-std.base64,-std.bigint,-std.bitmanip,-std.complex,-std.concurrency,-std.container,-std.container.array,-std.container.binaryheap,-std.container.dlist,-std.container.rbtree,-std.container.slist,-std.container.util,-std.conv,-std.csv,-std.datetime,-std.datetime.date,-std.datetime.interval,-std.datetime.stopwatch,-std.datetime.systime,-std.datetime.timezone,-std.digest.crc,-std.digest,-std.digest.hmac,-std.digest.md,-std.digest.murmurhash,-std.digest.ripemd,-std.digest.sha,-std.encoding,-std.exception,-std.experimental.allocator,-std.experimental.allocator.building_blocks.affix_allocator,-std.experimental.allocator.building_blocks.allocator_list,-std.experimental.allocator.building_blocks.bitmapped_block,-std.experimental.allocator.building_blocks.bucketizer,-std.experimental.allocator.building_blocks.fallback_allocator,-std.experimental.allocator.building_blocks.free_list,-std.experimental.allocator.building_blocks.free_tree,-std.experimental.allocator.building_blocks.kernighan_ritchie,-std.experimental.allocator.building_blocks.region,-std.experimental.allocator.building_blocks.stats_collector,-std.experimental.allocator.gc_allocator,-std.experimental.allocator.mallocator,-std.experimental.allocator.typed,-std.experimental.checkedint,-std.experimental.logger.core,-std.experimental.logger.multilogger,-std.experimental.typecons,-std.file,-std.format,-std.functional,-std.getopt,-std.internal.cstring,-std.internal.digest.sha_SSSE3,-std.internal.math.biguintcore,-std.internal.math.biguintnoasm,-std.internal.math.biguintx86,-std.internal.math.errorfunction,-std.internal.math.gammafunction,-std.internal.scopebuffer,-std.internal.test.dummyrange,-std.json,-std.math,-std.mathspecial,-std.meta,-std.mmfile,-std.net.curl,-std.net.isemail,-std.numeric,-std.outbuffer,-std.parallelism,-std.path,-std.process,-std.random,-std.range,-std.range.interfaces,-std.range.primitives,-std.regex,-std.regex.internal.backtracking,-std.regex.internal.generator,-std.regex.internal.ir,-std.regex.internal.kickstart,-std.regex.internal.parser,-std.regex.internal.tests,-std.regex.internal.thompson,-std.signals,-std.socket,-std.stdio,-std.string,-std.traits,-std.typecons,-std.uni,-std.uri,-std.utf,-std.uuid,-std.variant,-std.windows.registry,-std.xml,-std.zip,-std.zlib"
|
||||
could_be_immutable_check="-std.algorithm.comparison,-std.algorithm.iteration,-std.algorithm.mutation,-std.algorithm.searching,-std.algorithm.setops,-std.algorithm.sorting,-std.array,-std.base64,-std.bigint,-std.bitmanip,-std.complex,-std.concurrency,-std.container,-std.container.array,-std.container.binaryheap,-std.container.dlist,-std.container.rbtree,-std.container.slist,-std.container.util,-std.conv,-std.csv,-std.datetime,-std.datetime.date,-std.datetime.interval,-std.datetime.stopwatch,-std.datetime.systime,-std.datetime.timezone,-std.digest.crc,-std.digest,-std.digest.hmac,-std.digest.md,-std.digest.murmurhash,-std.digest.ripemd,-std.digest.sha,-std.encoding,-std.exception,-std.experimental.allocator,-std.experimental.allocator.building_blocks.affix_allocator,-std.experimental.allocator.building_blocks.allocator_list,-std.experimental.allocator.building_blocks.bitmapped_block,-std.experimental.allocator.building_blocks.bucketizer,-std.experimental.allocator.building_blocks.fallback_allocator,-std.experimental.allocator.building_blocks.free_list,-std.experimental.allocator.building_blocks.free_tree,-std.experimental.allocator.building_blocks.kernighan_ritchie,-std.experimental.allocator.building_blocks.region,-std.experimental.allocator.building_blocks.stats_collector,-std.experimental.allocator.gc_allocator,-std.experimental.allocator.mallocator,-std.experimental.allocator.typed,-std.experimental.checkedint,-std.experimental.logger.core,-std.experimental.logger.multilogger,-std.experimental.typecons,-std.file,-std.format,-std.functional,-std.getopt,-std.internal.cstring,-std.internal.digest.sha_SSSE3,-std.internal.math.biguintcore,-std.internal.math.biguintnoasm,-std.internal.math.biguintx86,-std.internal.math.errorfunction,-std.internal.math.gammafunction,-std.internal.scopebuffer,-std.internal.test.dummyrange,-std.json,-std.math,-std.mathspecial,-std.meta,-std.mmfile,-std.net.curl,-std.net.isemail,-std.numeric,-std.outbuffer,-std.parallelism,-std.path,-std.process,-std.random,-std.range,-std.range.interfaces,-std.range.primitives,-std.regex,-std.regex.internal.backtracking,-std.regex.internal.generator,-std.regex.internal.ir,-std.regex.internal.kickstart,-std.regex.internal.parser,-std.regex.internal.tests,-std.regex.internal.thompson,-std.signals,-std.socket,-std.stdio,-std.string,-std.sumtype,-std.traits,-std.typecons,-std.uni,-std.uri,-std.utf,-std.uuid,-std.variant,-std.windows.registry,-std.xml,-std.zip,-std.zlib"
|
||||
; Check for poor exception handling practices
|
||||
exception_check="-std.concurrency,-std.net.curl,-std.parallelism,-std.range,-std.socket,-std.typecons"
|
||||
; Checks for poor placement of function attributes
|
||||
|
@ -266,6 +267,7 @@ has_public_example="-etc.c.curl,\
|
|||
-std.regex.internal.ir,\
|
||||
-std.socket,\
|
||||
-std.stdio,\
|
||||
-std.sumtype,\
|
||||
-std.uni,\
|
||||
-std.xml,\
|
||||
-std.zip,\
|
||||
|
@ -290,9 +292,9 @@ number_style_check="+disabled"
|
|||
;number_style_check="-std.algorithm.iteration,-std.algorithm.sorting,-std.array,-std.bigint,-std.bitmanip,-std.container.array,-std.conv,-std.datetime.date,-std.datetime.systime,-std.datetime.timezone,-std.digest.crc,-std.digest,-std.digest.md,-std.digest.ripemd,-std.digest.sha,-std.experimental.allocator.building_blocks.free_tree,-std.experimental.allocator.building_blocks.kernighan_ritchie,-std.experimental.checkedint,-std.file,-std.format,-std.functional,-std.internal.math.biguintcore,-std.internal.math.gammafunction,-std.json,-std.math,-std.outbuffer,-std.parallelism,-std.random,-std.range,-std.regex.internal.generator,-std.utf,-std.zip,-std.zlib"
|
||||
; Checks that opEquals, opCmp, toHash, and toString are either const, immutable
|
||||
; , or inout.
|
||||
object_const_check="-std.algorithm.searching,-std.array,-std.bitmanip,-std.concurrency,-std.container.rbtree,-std.conv,-std.datetime.interval,-std.encoding,-std.exception,-std.experimental.checkedint,-std.experimental.logger.core,-std.experimental.typecons,-std.format,-std.functional,-std.meta,-std.numeric,-std.range,-std.regex,-std.stdio,-std.typecons,-std.variant,-std.xml"
|
||||
object_const_check="-std.algorithm.searching,-std.array,-std.bitmanip,-std.concurrency,-std.container.rbtree,-std.conv,-std.datetime.interval,-std.encoding,-std.exception,-std.experimental.checkedint,-std.experimental.logger.core,-std.experimental.typecons,-std.format,-std.functional,-std.meta,-std.numeric,-std.range,-std.regex,-std.stdio,-std.sumtype,-std.typecons,-std.variant,-std.xml"
|
||||
; Checks that opEquals and toHash are both defined or neither are defined
|
||||
opequals_tohash_check="-std.complex,-std.container.array,-std.container.dlist,-std.container.rbtree,-std.container.slist,-std.datetime,-std.datetime.date,-std.experimental.checkedint,-std.internal.test.dummyrange,-std.json,-std.numeric,-std.random,-std.socket,-std.typecons,-std.uni"
|
||||
opequals_tohash_check="-std.complex,-std.container.array,-std.container.dlist,-std.container.rbtree,-std.container.slist,-std.datetime,-std.datetime.date,-std.experimental.checkedint,-std.internal.test.dummyrange,-std.json,-std.numeric,-std.random,-std.socket,-std.sumtype,-std.typecons,-std.uni"
|
||||
; Check for properly documented public functions (Returns, Params)
|
||||
; Note: DScanner doesn't understand documenting parameters of IFTI/eponymous templates.
|
||||
properly_documented_public_functions="-etc.c.odbc.sql,\
|
||||
|
@ -383,6 +385,7 @@ properly_documented_public_functions="-etc.c.odbc.sql,\
|
|||
-std.socket,\
|
||||
-std.stdio,\
|
||||
-std.string,\
|
||||
-std.sumtype,\
|
||||
-std.typecons,\
|
||||
-std.uni,\
|
||||
-std.uri,\
|
||||
|
|
|
@ -61,6 +61,7 @@ std/socket.d @CyberShadow @klickverbot
|
|||
# std/stdint.d
|
||||
std/stdio.d @CyberShadow @schveiguy
|
||||
std/string.d @burner @JackStouffer
|
||||
std/sumtype.d @pbackus
|
||||
# std/system.d
|
||||
std/traits.d @Biotronic @klickverbot @ZombineDev
|
||||
std/typecons.d @Biotronic @MetaLang @ZombineDev
|
||||
|
|
6
index.d
6
index.d
|
@ -444,12 +444,16 @@ $(BOOKTABLE ,
|
|||
)
|
||||
$(TR
|
||||
$(TDNW $(MREF std,variant))
|
||||
$(TD Discriminated unions and algebraic types.)
|
||||
$(TD Dynamically-typed variable that can hold a value of any type.)
|
||||
)
|
||||
$(TR
|
||||
$(TDNW $(MREF core,bitop))
|
||||
$(TD Low level bit manipulation.)
|
||||
)
|
||||
$(TR
|
||||
$(TDNW $(MREF std,sumtype))
|
||||
$(TD Type-safe discriminated union.)
|
||||
)
|
||||
$(LEADINGROW Vector programming)
|
||||
$(TR
|
||||
$(TDNW $(MREF core,simd))
|
||||
|
|
23
posix.mak
23
posix.mak
|
@ -216,7 +216,7 @@ PACKAGE_std = array ascii base64 bigint bitmanip compiler complex concurrency \
|
|||
conv csv demangle encoding exception file \
|
||||
functional getopt json math mathspecial meta mmfile numeric \
|
||||
outbuffer package parallelism path process random signals socket stdint \
|
||||
stdio string system traits typecons \
|
||||
stdio string sumtype system traits typecons \
|
||||
uri utf uuid variant xml zip zlib
|
||||
PACKAGE_std_experimental = checkedint typecons
|
||||
PACKAGE_std_algorithm = comparison iteration mutation package searching setops \
|
||||
|
@ -670,6 +670,27 @@ betterc: betterc-phobos-tests
|
|||
--inputdir $< --outputdir $(BETTERCTESTS_DIR)
|
||||
$(DMD) $(DFLAGS) $(NODEFAULTLIB) -betterC -unittest -run $(BETTERCTESTS_DIR)/$(subst /,_,$<)
|
||||
|
||||
|
||||
################################################################################
|
||||
# Full-module BetterC tests
|
||||
# -------------------------
|
||||
#
|
||||
# Test full modules with -betterC. Edit BETTERC_MODULES and
|
||||
# test/betterc_module_tests.d to add new modules to the list.
|
||||
#
|
||||
# make -f posix.mak betterc-module-tests
|
||||
################################################################################
|
||||
|
||||
BETTERC_MODULES=std/sumtype
|
||||
|
||||
betterc: betterc-module-tests
|
||||
|
||||
betterc-module-tests: $(ROOT)/betterctests/betterc_module_tests
|
||||
$(ROOT)/betterctests/betterc_module_tests
|
||||
|
||||
$(ROOT)/betterctests/betterc_module_tests: test/betterc_module_tests.d $(addsuffix .d,$(BETTERC_MODULES))
|
||||
$(DMD) $(DFLAGS) $(NODEFAULTLIB) -of=$(ROOT)/betterctests/betterc_module_tests -betterC -unittest test/betterc_module_tests.d $(addsuffix .d,$(BETTERC_MODULES))
|
||||
|
||||
################################################################################
|
||||
|
||||
.PHONY : auto-tester-build
|
||||
|
|
2474
std/sumtype.d
Normal file
2474
std/sumtype.d
Normal file
File diff suppressed because it is too large
Load diff
|
@ -23,6 +23,9 @@ type constructor. Unlike `Variant`, `Algebraic` only allows a finite set of
|
|||
types, which are specified in the instantiation (e.g. $(D Algebraic!(int,
|
||||
string)) may only hold an `int` or a `string`).
|
||||
|
||||
$(RED Warning: $(LREF Algebraic) is outdated and not recommended for use in new
|
||||
code. Instead, use $(REF SumType, std,sumtype).)
|
||||
|
||||
Credits: Reviewed by Brad Roberts. Daniel Keep provided a detailed code review
|
||||
prompting the following improvements: (1) better support for arrays; (2) support
|
||||
for associative arrays; (3) friendlier behavior towards the garbage collector.
|
||||
|
@ -1588,6 +1591,8 @@ useful when it is desirable to restrict what a discriminated type
|
|||
could hold to the end of defining simpler and more efficient
|
||||
manipulation.
|
||||
|
||||
$(RED Warning: $(LREF Algebraic) is outdated and not recommended for use in new
|
||||
code. Instead, use $(REF SumType, std,sumtype).)
|
||||
*/
|
||||
template Algebraic(T...)
|
||||
{
|
||||
|
|
30
test/betterc_module_tests.d
Normal file
30
test/betterc_module_tests.d
Normal file
|
@ -0,0 +1,30 @@
|
|||
static immutable bettercModules = [
|
||||
"std.sumtype"
|
||||
];
|
||||
|
||||
template from(string modname)
|
||||
{
|
||||
mixin("import from = ", modname, ";");
|
||||
}
|
||||
|
||||
void testModule(string modname)()
|
||||
{
|
||||
import core.stdc.stdio : printf;
|
||||
|
||||
printf("Running BetterC tests for %.*s\n", cast(int) modname.length, modname.ptr);
|
||||
|
||||
static foreach (test; __traits(getUnitTests, from!modname))
|
||||
{
|
||||
test();
|
||||
}
|
||||
}
|
||||
|
||||
extern(C) int main()
|
||||
{
|
||||
static foreach (modname; bettercModules)
|
||||
{
|
||||
testModule!modname;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -155,6 +155,9 @@ SRC_STD_7= \
|
|||
std\process.d \
|
||||
std\package.d
|
||||
|
||||
SRC_STD_7a= \
|
||||
std\sumtype.d
|
||||
|
||||
SRC_STD= \
|
||||
$(SRC_STD_1) \
|
||||
$(SRC_STD_2a) \
|
||||
|
@ -162,7 +165,8 @@ SRC_STD= \
|
|||
$(SRC_STD_3a) \
|
||||
$(SRC_STD_4) \
|
||||
$(SRC_STD_6) \
|
||||
$(SRC_STD_7)
|
||||
$(SRC_STD_7) \
|
||||
$(SRC_STD_7a)
|
||||
|
||||
SRC_STD_ALGO= \
|
||||
std\algorithm\package.d \
|
||||
|
@ -389,6 +393,7 @@ UNITTEST_OBJS= \
|
|||
unittest6a.obj \
|
||||
unittest6b.obj \
|
||||
unittest7.obj \
|
||||
unittest7a.obj \
|
||||
unittest8a.obj \
|
||||
unittest8b.obj \
|
||||
unittest8c.obj \
|
||||
|
@ -410,6 +415,7 @@ unittest : $(LIB)
|
|||
$(DMD) $(UDFLAGS) -L/co -c -ofunittest6a.obj $(SRC_STD_EXP_ALLOC)
|
||||
$(DMD) $(UDFLAGS) -L/co -c -ofunittest6b.obj $(SRC_STD_EXP_LOGGER)
|
||||
$(DMD) $(UDFLAGS) -L/co -c -ofunittest7.obj $(SRC_STD_7)
|
||||
$(DMD) $(UDFLAGS) -L/co -c -ofunittest7a.obj $(SRC_STD_7a)
|
||||
$(DMD) $(UDFLAGS) -L/co -c -ofunittest8a.obj $(SRC_STD_REGEX)
|
||||
$(DMD) $(UDFLAGS) -L/co -c -ofunittest8b.obj $(SRC_STD_NET)
|
||||
$(DMD) $(UDFLAGS) -L/co -c -ofunittest8c.obj $(SRC_STD_C) $(SRC_STD_WIN) $(SRC_STD_C_WIN)
|
||||
|
@ -513,6 +519,7 @@ cov : $(SRC_TO_COMPILE) $(LIB)
|
|||
$(DMD) -conf= -cov=ctfe -cov=92 $(UDFLAGS) -main -run std\internal\math\errorfunction.d
|
||||
$(DMD) -conf= -cov=ctfe -cov=31 $(UDFLAGS) -main -run std\internal\windows\advapi32.d
|
||||
$(DMD) -conf= -cov=ctfe -cov=58 $(UDFLAGS) -main -run etc\c\zlib.d
|
||||
$(DMD) -conf= -cov=ctfe -cov=95 $(UDFLAGS) -main -run std\sumtype.d
|
||||
|
||||
######################################################
|
||||
|
||||
|
|
|
@ -164,6 +164,9 @@ SRC_STD_7= \
|
|||
std\process.d \
|
||||
std\package.d
|
||||
|
||||
SRC_STD_7a= \
|
||||
std\sumtype.d
|
||||
|
||||
SRC_STD= \
|
||||
$(SRC_STD_1) \
|
||||
$(SRC_STD_2a) \
|
||||
|
@ -178,7 +181,8 @@ SRC_STD= \
|
|||
$(SRC_STD_6e) \
|
||||
$(SRC_STD_6h) \
|
||||
$(SRC_STD_6i) \
|
||||
$(SRC_STD_7)
|
||||
$(SRC_STD_7) \
|
||||
$(SRC_STD_7a)
|
||||
|
||||
SRC_STD_ALGO_1= \
|
||||
std\algorithm\package.d \
|
||||
|
@ -421,6 +425,7 @@ UNITTEST_OBJS= \
|
|||
unittest6h.obj \
|
||||
unittest6i.obj \
|
||||
unittest7.obj \
|
||||
unittest7a.obj \
|
||||
unittest8a.obj \
|
||||
unittest8b.obj \
|
||||
unittest8c.obj \
|
||||
|
@ -450,6 +455,7 @@ unittest : $(LIB)
|
|||
"$(DMD)" $(UDFLAGS) -c -ofunittest6h.obj $(SRC_STD_6h)
|
||||
"$(DMD)" $(UDFLAGS) -c -ofunittest6i.obj $(SRC_STD_6i)
|
||||
"$(DMD)" $(UDFLAGS) -c -ofunittest7.obj $(SRC_STD_7) $(SRC_STD_EXP_LOGGER)
|
||||
"$(DMD)" $(UDFLAGS) -c -ofunittest7a.obj $(SRC_STD_7a)
|
||||
"$(DMD)" $(UDFLAGS) -c -ofunittest8a.obj $(SRC_STD_REGEX)
|
||||
"$(DMD)" $(UDFLAGS) -c -ofunittest8b.obj $(SRC_STD_NET)
|
||||
"$(DMD)" $(UDFLAGS) -c -ofunittest8c.obj $(SRC_STD_C) $(SRC_STD_WIN) $(SRC_STD_C_WIN)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue