summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordkf <donal.k.fellows@manchester.ac.uk>2019-04-15 19:57:54 (GMT)
committerdkf <donal.k.fellows@manchester.ac.uk>2019-04-15 19:57:54 (GMT)
commitbcca980f06d421d332bb5bb34dac1a056681b86a (patch)
treeeb42c509a8fba1c0e668301fc52c27f56e856696
parent6ae823b6b7894f244a874659bfd93fb4914c6a3a (diff)
downloadtcl-bcca980f06d421d332bb5bb34dac1a056681b86a.zip
tcl-bcca980f06d421d332bb5bb34dac1a056681b86a.tar.gz
tcl-bcca980f06d421d332bb5bb34dac1a056681b86a.tar.bz2
Add [dict getdef] alias
-rw-r--r--doc/dict.n5
-rw-r--r--generic/tclDictObj.c18
-rw-r--r--tests/dict.test46
3 files changed, 50 insertions, 19 deletions
diff --git a/doc/dict.n b/doc/dict.n
index 12c9b1a..3475415 100644
--- a/doc/dict.n
+++ b/doc/dict.n
@@ -120,6 +120,8 @@ It is an error to attempt to retrieve a value for a key that is not
present in the dictionary.
.RE
.TP
+\fBdict getdef \fIdictionaryValue \fR?\fIkey ...\fR? \fIkey default\fR
+.TP
\fBdict getwithdefault \fIdictionaryValue \fR?\fIkey ...\fR? \fIkey default\fR
.VS "8.7, TIP342"
This behaves the same as \fBdict get\fR (with at least one \fIkey\fR
@@ -129,7 +131,8 @@ error because the \fIkey\fR (or one of the \fIkey\fRs on the key path)
is absent, it returns the \fIdefault\fR argument instead.
.RS
.PP
-Note that there must always be at least one \fIkey\fR provided.
+Note that there must always be at least one \fIkey\fR provided, and that
+\fBdict getdef\fR and \fBdict getwithdefault\fR are aliases for each other.
.RE
.VE "8.7, TIP342"
.TP
diff --git a/generic/tclDictObj.c b/generic/tclDictObj.c
index 75dcd09..fea4035 100644
--- a/generic/tclDictObj.c
+++ b/generic/tclDictObj.c
@@ -34,9 +34,8 @@ static int DictFilterCmd(ClientData dummy, Tcl_Interp *interp,
int objc, Tcl_Obj *const *objv);
static int DictGetCmd(ClientData dummy, Tcl_Interp *interp,
int objc, Tcl_Obj *const *objv);
-static int DictGetWithDefaultCmd(ClientData dummy,
- Tcl_Interp *interp, int objc,
- Tcl_Obj *const *objv);
+static int DictGetDefCmd(ClientData dummy, Tcl_Interp *interp,
+ int objc, Tcl_Obj *const *objv);
static int DictIncrCmd(ClientData dummy, Tcl_Interp *interp,
int objc, Tcl_Obj *const *objv);
static int DictInfoCmd(ClientData dummy, Tcl_Interp *interp,
@@ -92,7 +91,8 @@ static const EnsembleImplMap implementationMap[] = {
{"filter", DictFilterCmd, NULL, NULL, NULL, 0 },
{"for", NULL, TclCompileDictForCmd, DictForNRCmd, NULL, 0 },
{"get", DictGetCmd, TclCompileDictGetCmd, NULL, NULL, 0 },
- {"getwithdefault", DictGetWithDefaultCmd, NULL, NULL, NULL, 0 },
+ {"getdef", DictGetDefCmd, NULL, NULL, NULL, 0 },
+ {"getwithdefault", DictGetDefCmd, NULL, NULL, NULL, 0 },
{"incr", DictIncrCmd, TclCompileDictIncrCmd, NULL, NULL, 0 },
{"info", DictInfoCmd, TclCompileBasic1ArgCmd, NULL, NULL, 0 },
{"keys", DictKeysCmd, TclCompileBasic1Or2ArgCmd, NULL, NULL, 0 },
@@ -1631,11 +1631,11 @@ DictGetCmd(
/*
*----------------------------------------------------------------------
*
- * DictGetWithDefaultCmd --
+ * DictGetDefCmd --
*
- * This function implements the "dict getwithdefault" Tcl command. See
- * the user documentation for details on what it does, and TIP#342 for
- * the formal specification.
+ * This function implements the "dict getdef" and "dict getwithdefault"
+ * Tcl commands. See the user documentation for details on what it does,
+ * and TIP#342 for the formal specification.
*
* Results:
* A standard Tcl result.
@@ -1647,7 +1647,7 @@ DictGetCmd(
*/
static int
-DictGetWithDefaultCmd(
+DictGetDefCmd(
ClientData dummy,
Tcl_Interp *interp,
int objc,
diff --git a/tests/dict.test b/tests/dict.test
index 50e4db7..6d74b96 100644
--- a/tests/dict.test
+++ b/tests/dict.test
@@ -2048,31 +2048,59 @@ test dict-25.1 {compiled dict update with low-refcount values [Bug d553228d9f]}
}}
} {}
-test dict-26.1 {dict getwithdefault command} -body {
+test dict-26.1 {dict getdef command} -body {
+ dict getdef {a b} a c
+} -result b
+test dict-26.2 {dict getdef command} -body {
+ dict getdef {a b} b c
+} -result c
+test dict-26.3 {dict getdef command} -body {
+ dict getdef {a {b c}} a b d
+} -result c
+test dict-26.4 {dict getdef command} -body {
+ dict getdef {a {b c}} a c d
+} -result d
+test dict-26.5 {dict getdef command} -body {
+ dict getdef {a {b c}} b c d
+} -result d
+test dict-26.6 {dict getdef command} -returnCodes error -body {
+ dict getdef {a {b c d}} a b d
+} -result {missing value to go with key}
+test dict-26.7 {dict getdef command} -returnCodes error -body {
+ dict getdef
+} -result {wrong # args: should be "dict getdef dictionary ?key ...? key default"}
+test dict-26.8 {dict getdef command} -returnCodes error -body {
+ dict getdef {}
+} -result {wrong # args: should be "dict getdef dictionary ?key ...? key default"}
+test dict-26.9 {dict getdef command} -returnCodes error -body {
+ dict getdef {} {}
+} -result {wrong # args: should be "dict getdef dictionary ?key ...? key default"}
+
+test dict-27.1 {dict getwithdefault command} -body {
dict getwithdefault {a b} a c
} -result b
-test dict-26.2 {dict getwithdefault command} -body {
+test dict-27.2 {dict getwithdefault command} -body {
dict getwithdefault {a b} b c
} -result c
-test dict-26.3 {dict getwithdefault command} -body {
+test dict-27.3 {dict getwithdefault command} -body {
dict getwithdefault {a {b c}} a b d
} -result c
-test dict-26.4 {dict getwithdefault command} -body {
+test dict-27.4 {dict getwithdefault command} -body {
dict getwithdefault {a {b c}} a c d
} -result d
-test dict-26.5 {dict getwithdefault command} -body {
+test dict-27.5 {dict getwithdefault command} -body {
dict getwithdefault {a {b c}} b c d
} -result d
-test dict-26.6 {dict getwithdefault command} -returnCodes error -body {
+test dict-27.6 {dict getwithdefault command} -returnCodes error -body {
dict getwithdefault {a {b c d}} a b d
} -result {missing value to go with key}
-test dict-26.7 {dict getwithdefault command} -returnCodes error -body {
+test dict-27.7 {dict getwithdefault command} -returnCodes error -body {
dict getwithdefault
} -result {wrong # args: should be "dict getwithdefault dictionary ?key ...? key default"}
-test dict-26.8 {dict getwithdefault command} -returnCodes error -body {
+test dict-27.8 {dict getwithdefault command} -returnCodes error -body {
dict getwithdefault {}
} -result {wrong # args: should be "dict getwithdefault dictionary ?key ...? key default"}
-test dict-26.9 {dict getwithdefault command} -returnCodes error -body {
+test dict-27.9 {dict getwithdefault command} -returnCodes error -body {
dict getwithdefault {} {}
} -result {wrong # args: should be "dict getwithdefault dictionary ?key ...? key default"}