Mercurial > vim
comparison src/vim9type.c @ 33678:7d9d2404a3d4 v9.0.2076
patch 9.0.2076: Vim9: No support for type aliases
Commit: https://github.com/vim/vim/commit/ec3cebbd2b6b7583d2f683f5e66345163ec122aa
Author: Yegappan Lakshmanan <yegappan@yahoo.com>
Date: Fri Oct 27 19:35:26 2023 +0200
patch 9.0.2076: Vim9: No support for type aliases
Problem: Vim9: No support for type aliases
Solution: Implement :type command
A type definition is giving a name to a type specification. This also known
type alias.
:type ListOfStrings = list<string>
The type alias can be used wherever a built-in type can be used. The type
alias name must start with an upper case character.
closes: #13407
Signed-off-by: Christian Brabandt <cb@256bit.org>
Signed-off-by: Yegappan Lakshmanan <yegappan@yahoo.com>
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Fri, 27 Oct 2023 19:45:05 +0200 |
parents | 2c66a4fe1513 |
children | f126ffc85f7c |
comparison
equal
deleted
inserted
replaced
33677:96debbc2abd9 | 33678:7d9d2404a3d4 |
---|---|
1352 } | 1352 } |
1353 break; | 1353 break; |
1354 } | 1354 } |
1355 | 1355 |
1356 // It can be a class or interface name, possibly imported. | 1356 // It can be a class or interface name, possibly imported. |
1357 typval_T tv; | 1357 int did_emsg_before = did_emsg; |
1358 typval_T tv; | |
1359 | |
1358 tv.v_type = VAR_UNKNOWN; | 1360 tv.v_type = VAR_UNKNOWN; |
1359 if (eval_variable_import(*arg, &tv) == OK) | 1361 if (eval_variable_import(*arg, &tv) == OK) |
1360 { | 1362 { |
1361 if (tv.v_type == VAR_CLASS && tv.vval.v_class != NULL) | 1363 if (tv.v_type == VAR_CLASS && tv.vval.v_class != NULL) |
1362 { | 1364 { |
1375 ++*arg; | 1377 ++*arg; |
1376 | 1378 |
1377 return type; | 1379 return type; |
1378 } | 1380 } |
1379 } | 1381 } |
1382 else if (tv.v_type == VAR_TYPEALIAS) | |
1383 { | |
1384 // user defined type | |
1385 type_T *type = copy_type(tv.vval.v_typealias->ta_type, type_gap); | |
1386 *arg += len; | |
1387 clear_tv(&tv); | |
1388 // Skip over ".TypeName". | |
1389 while (ASCII_ISALNUM(**arg) || **arg == '_' || **arg == '.') | |
1390 ++*arg; | |
1391 return type; | |
1392 } | |
1380 | 1393 |
1381 clear_tv(&tv); | 1394 clear_tv(&tv); |
1382 } | 1395 } |
1383 | 1396 |
1384 if (give_error) | 1397 if (give_error && (did_emsg == did_emsg_before)) |
1385 semsg(_(e_type_not_recognized_str), *arg); | 1398 semsg(_(e_type_not_recognized_str), *arg); |
1386 return NULL; | 1399 return NULL; |
1387 } | 1400 } |
1388 | 1401 |
1389 /* | 1402 /* |
1414 case VAR_JOB: | 1427 case VAR_JOB: |
1415 case VAR_CHANNEL: | 1428 case VAR_CHANNEL: |
1416 case VAR_INSTR: | 1429 case VAR_INSTR: |
1417 case VAR_CLASS: | 1430 case VAR_CLASS: |
1418 case VAR_OBJECT: | 1431 case VAR_OBJECT: |
1432 case VAR_TYPEALIAS: | |
1419 break; // not composite is always OK | 1433 break; // not composite is always OK |
1420 case VAR_LIST: | 1434 case VAR_LIST: |
1421 case VAR_DICT: | 1435 case VAR_DICT: |
1422 return equal_type(type1->tt_member, type2->tt_member, flags); | 1436 return equal_type(type1->tt_member, type2->tt_member, flags); |
1423 case VAR_FUNC: | 1437 case VAR_FUNC: |
1664 case VAR_LIST: return "list"; | 1678 case VAR_LIST: return "list"; |
1665 case VAR_DICT: return "dict"; | 1679 case VAR_DICT: return "dict"; |
1666 case VAR_INSTR: return "instr"; | 1680 case VAR_INSTR: return "instr"; |
1667 case VAR_CLASS: return "class"; | 1681 case VAR_CLASS: return "class"; |
1668 case VAR_OBJECT: return "object"; | 1682 case VAR_OBJECT: return "object"; |
1683 case VAR_TYPEALIAS: return "typealias"; | |
1669 | 1684 |
1670 case VAR_FUNC: | 1685 case VAR_FUNC: |
1671 case VAR_PARTIAL: return "func"; | 1686 case VAR_PARTIAL: return "func"; |
1672 } | 1687 } |
1673 return "unknown"; | 1688 return "unknown"; |
1793 char *tofree; | 1808 char *tofree; |
1794 char *name; | 1809 char *name; |
1795 | 1810 |
1796 rettv->v_type = VAR_STRING; | 1811 rettv->v_type = VAR_STRING; |
1797 ga_init2(&type_list, sizeof(type_T *), 10); | 1812 ga_init2(&type_list, sizeof(type_T *), 10); |
1798 type = typval2type(argvars, get_copyID(), &type_list, TVTT_DO_MEMBER); | 1813 if (argvars[0].v_type == VAR_TYPEALIAS) |
1814 type = argvars[0].vval.v_typealias->ta_type; | |
1815 else | |
1816 type = typval2type(argvars, get_copyID(), &type_list, TVTT_DO_MEMBER); | |
1799 name = type_name(type, &tofree); | 1817 name = type_name(type, &tofree); |
1800 if (tofree != NULL) | 1818 if (argvars[0].v_type == VAR_TYPEALIAS) |
1801 rettv->vval.v_string = (char_u *)tofree; | 1819 { |
1820 vim_snprintf((char *)IObuff, IOSIZE, "typealias<%s>", name); | |
1821 rettv->vval.v_string = vim_strsave((char_u *)IObuff); | |
1822 if (tofree != NULL) | |
1823 vim_free(tofree); | |
1824 } | |
1802 else | 1825 else |
1803 rettv->vval.v_string = vim_strsave((char_u *)name); | 1826 { |
1827 if (tofree != NULL) | |
1828 rettv->vval.v_string = (char_u *)tofree; | |
1829 else | |
1830 rettv->vval.v_string = vim_strsave((char_u *)name); | |
1831 } | |
1804 clear_type_list(&type_list); | 1832 clear_type_list(&type_list); |
1805 } | 1833 } |
1806 | 1834 |
1807 #endif // FEAT_EVAL | 1835 #endif // FEAT_EVAL |