]> granicus.if.org Git - vim/commitdiff
patch 8.2.2339: cannot get the type of a value as a string v8.2.2339
authorBram Moolenaar <Bram@vim.org>
Tue, 12 Jan 2021 20:49:00 +0000 (21:49 +0100)
committerBram Moolenaar <Bram@vim.org>
Tue, 12 Jan 2021 20:49:00 +0000 (21:49 +0100)
Problem:    Cannot get the type of a value as a string.
Solution:   Add typename().

runtime/doc/eval.txt
runtime/doc/usr_41.txt
src/evalfunc.c
src/proto/vim9type.pro
src/testdir/test_vim9_builtin.vim
src/testdir/test_vimscript.vim
src/version.c
src/vim9type.c

index d31ba2d36231fdcc7223222ded2364d87f68316e..76c55485327e29d361c8f2cd854b49c50770766c 100644 (file)
@@ -3016,7 +3016,8 @@ tr({src}, {fromstr}, {tostr})     String  translate chars of {src} in {fromstr}
 trim({text} [, {mask} [, {dir}]])
                                String  trim characters in {mask} from {text}
 trunc({expr})                  Float   truncate Float {expr}
-type({name})                   Number  type of variable {name}
+type({expr})                   Number  type of value {expr}
+typename({expr})               String  representation of the type of {expr}
 undofile({name})               String  undo file name for {name}
 undotree()                     List    undo file tree
 uniq({list} [, {func} [, {dict}]])
@@ -11129,6 +11130,14 @@ type({expr})   The result is a Number representing the type of {expr}.
 <              Can also be used as a |method|: >
                        mylist->type()
 
+
+typename({expr})                                       *typename()*
+               Return a string representation of the type of {expr}.
+               Example: >
+                       echo typename([1, 2, 3])
+                       list<number>
+
+
 undofile({name})                                       *undofile()*
                Return the name of the undo file that would be used for a file
                with name {name} when writing.  This uses the 'undodir'
index 624bb934f54ac1116f805df4d1eaf515497f5835..5f9c8e1bdcd334e5ca0398b3ff853bf02fd41226 100644 (file)
@@ -720,7 +720,8 @@ Other computation:                                  *bitwise-function*
        srand()                 initialize seed used by rand()
 
 Variables:                                             *var-functions*
-       type()                  type of a variable
+       type()                  type of a variable as a number
+       typename()              type of a variable as text
        islocked()              check if a variable is locked
        funcref()               get a Funcref for a function reference
        function()              get a Funcref for a function name
index 65cb59f8d41fbaada08c2d42c89ae63a3a05fc5c..0e6d759c66460275b9ac2d36bed4504938aa76f4 100644 (file)
@@ -1742,6 +1742,8 @@ static funcentry_T global_functions[] =
                        ret_float,          FLOAT_FUNC(f_trunc)},
     {"type",           1, 1, FEARG_1,      NULL,
                        ret_number,         f_type},
+    {"typename",       1, 1, FEARG_1,      NULL,
+                       ret_string,         f_typename},
     {"undofile",       1, 1, FEARG_1,      NULL,
                        ret_string,         f_undofile},
     {"undotree",       0, 0, 0,            NULL,
index bc306687f846cfb636b7236d932d13c079fe75d0..316c65011597686808e18ddf710591ce0bd021a0 100644 (file)
@@ -24,4 +24,5 @@ void common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap
 type_T *get_member_type_from_stack(type_T **stack_top, int count, int skip, garray_T *type_gap);
 char *vartype_name(vartype_T type);
 char *type_name(type_T *type, char **tofree);
+void f_typename(typval_T *argvars, typval_T *rettv);
 /* vim: set ft=c : */
index 83b9931f24daf33c53fb4f5a2fcd04e92f96a18e..a84b7f6210482637241604687c369cf83b046a83 100644 (file)
@@ -318,8 +318,8 @@ def Test_job_info_return_type()
   if has('job')
     job_start(&shell)
     var jobs = job_info()
-    assert_equal(v:t_list, type(jobs))
-    assert_equal(v:t_dict, type(job_info(jobs[0])))
+    assert_equal('list<job>', typename(jobs))
+    assert_equal('dict<any>', typename(job_info(jobs[0])))
     job_stop(jobs[0])
   endif
 enddef
index dba8c73fc80cc4fd588550c7c5e4cad180b60e99..0ba933b7cbe84e8262c2fd6c1b00aaa1e8d9f948 100644 (file)
@@ -6600,6 +6600,14 @@ func Test_type()
     call ChangeYourMind()
 endfunc
 
+func Test_typename()
+  call assert_equal('number', typename(123))
+  call assert_equal('string', typename('x'))
+  call assert_equal('list<number>', typename([123]))
+  call assert_equal('dict<number>', typename(#{key: 123}))
+  call assert_equal('list<dict<number>>', typename([#{key: 123}]))
+endfunc
+
 "-------------------------------------------------------------------------------
 " Test 92:  skipping code                                          {{{1
 "-------------------------------------------------------------------------------
index 1b63a5e9c7f7273919caa85b32c915d8eff22352..fe41ec27a0a684da4345bca07c004399dd1cf58b 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    2339,
 /**/
     2338,
 /**/
index c65f747483cb0ead7a7a78c2dc26dc98ae7862a9..85f9bd8347f6afece54e2a2552c0b8705c9e8dd8 100644 (file)
@@ -1170,4 +1170,29 @@ type_name(type_T *type, char **tofree)
     return name;
 }
 
+/*
+ * "typename(expr)" function
+ */
+    void
+f_typename(typval_T *argvars, typval_T *rettv)
+{
+    garray_T   type_list;
+    type_T     *type;
+    char       *tofree;
+    char       *name;
+
+    rettv->v_type = VAR_STRING;
+    ga_init2(&type_list, sizeof(type_T *), 10);
+    type = typval2type(argvars, &type_list);
+    name = type_name(type, &tofree);
+    if (tofree != NULL)
+       rettv->vval.v_string = (char_u *)tofree;
+    else
+    {
+       rettv->vval.v_string = vim_strsave((char_u *)name);
+       vim_free(tofree);
+    }
+    clear_type_list(&type_list);
+}
+
 #endif // FEAT_EVAL