Knowledge (XXG)

Unit type

Source 📝

695:, i.e. in a struct or a class in C/C++. In contrast, the unit type can be stored in records in functional programming languages, i.e. it can appear as the type of a field; the above implementation of the unit type in C++ can also be stored. While this may seem a useless feature, it does allow one for instance to elegantly implement a 422:' is used in a manner that simulates some, but not all, of the properties of the unit type, as detailed below. Like most imperative languages, C allows functions that do not return a value; these are specified as having the void return type. Such functions are called procedures in other imperative languages like 434:
The first notable difference between a true unit type and the void type is that the unit type may always be the type of the argument to a function, but the void type cannot be the type of an argument in C, despite the fact that it may appear as the sole argument in the list. This problem is best
541:
must be treated differently from other types. In C++ however, empty classes are allowed, so it is possible to implement a real unit type; the above example becomes compilable as:
124:
that is used in some imperative programming languages serves some of its functions, but because its carrier set is empty, it has some limitations (as detailed below).
414:
is used to designate a function that does not return anything useful, or a function that accepts no arguments. The unit type in C is conceptually similar to an empty
1171: 1542: 1176: 703:
to the unit type; in the absence of a unit type, one can still implement a set this way by storing some dummy value of another type for each key.
1166: 1161: 1149: 1050: 940:(where a particular class has only one instance, but narrowly-typed non-nullable references to it are usually not held by other classes) 1007: 58:
that allows only one value (and thus can hold no information). The carrier (underlying set) associated with a unit type can be any
1300: 994: 1417: 1222: 1154: 1116: 397: 184: 149: 141: 423: 338: 240: 1317: 1247: 1095: 259: 199: 1572: 353: 278: 153: 137: 375:
unit type was added in C++17. Before that, it is possible to define a custom unit type using an empty struct such as
1327: 1195: 157: 418:, but a struct without members is not allowed in the C language specification (this is allowed in C++). Instead, ' 1505: 1457: 1369: 1347: 1342: 1270: 1136: 692: 293: 172: 75: 723:(like all other reference types), so it acts as a unit type. In practice, any other non-instantiable type, e.g. 1379: 1043: 426:, where a syntactic distinction, instead of type-system distinction, is made between functions and procedures. 401: 389: 1532: 1447: 1275: 1131: 1090: 1085: 869:
Statically typed languages give a type to every possible expression. They need to associate a type to the
696: 532: 117: 1265: 1240: 32: 1598: 1067: 133: 28: 20: 1593: 1537: 1515: 1442: 1295: 1287: 1207: 1036: 528: 70:
unit type and ignore the details of its value. One may also regard the unit type as the type of 0-
1520: 1500: 1452: 1427: 1212: 1181: 989: 39: 1407: 1337: 1312: 1126: 1121: 1003: 937: 700: 680: 676: 106: 1552: 1437: 1235: 691:
The second notable difference is that the void type is special and can never be stored in a
537: 410: 43: 1557: 1422: 1374: 1307: 86: 82: 1510: 1332: 1322: 1230: 102: 1587: 1432: 59: 1389: 1364: 527:
type carries no information, it is useless to pass it anyway; but it may arise in
1567: 1562: 1412: 1359: 1186: 233: 214: 180: 63: 47: 24: 959: 523:
This issue does not arise in most programming practice in C, because since the
1472: 1467: 1384: 1352: 1257: 1200: 315: 94: 727:, can also be used for this purpose, since they also have exactly one value, 1547: 1525: 1482: 1144: 1100: 1059: 999: 711:
In Java Generics, type parameters must be reference types. The wrapper type
251:. In Python's optional static type annotations, this type is represented as 121: 55: 1016: 880:
For example in D, it's possible to declare functions that may only return
435:
illustrated by the following program, which is a compile-time error in C:
1462: 144:, and the argument type of a function that does not require arguments. 393: 368: 176: 71: 715:
is often used when a unit type parameter is needed. Although the
89:
of types and typed functions. It should not be confused with the
1080: 1020: 311:, the unit type is called null, which only value is NULL itself. 1032: 1075: 405: 308: 1028: 671:(For brevity, we're not worried in the above example whether 66:
between any two such sets, so it is customary to talk about
719:
type can never have any instances, it does have one value,
221:
is a unit type which has one value, namely the symbol
136:
provide a unit type to specify the result type of a
1491: 1400: 1286: 1256: 1221: 1109: 1066: 1044: 8: 1051: 1037: 1029: 19:This article is about the notion used in 873:expression. A type will be defined for 345:is a singleton with only one value: the 274:, reflecting the 0-tuple interpretation. 168:, reflecting the 0-tuple interpretation. 950: 225:. This should not be confused with the 27:. For types of measurement units, see 958:van Rossum, Guido; Levkivskyi, Ivan. 116:The unit type is implemented in most 7: 140:with the sole purpose of causing a 877:and it will only have this value. 14: 247:which allows the single value of 206:and its only value is written as 105:in this category. Similarly, the 430:Difference in calling convention 995:Types and Programming Languages 16:Type that allows only one value 1: 1117:Arbitrary-precision or bignum 966:. Python Software Foundation 964:Python Enhancement Proposals 683:for details on that issue.) 360:is the only instance of the 191:but the value is written as 296:, the unit type is written 270:and its only value is also 164:and its only value is also 1617: 506:// compile-time error here 334:) are built-in unit types. 281:, the unit type is called 262:, the unit type is called 202:, the unit type is called 160:, the unit type is called 18: 1458:Strongly typed identifier 928:, a unit type, can have. 243:, there is a type called 886: 733: 543: 437: 128:In programming languages 1533:Parametric polymorphism 924:is the only value that 175:descendants (including 960:"PEP 483 - Using None" 901:returnThatSpecialThing 384:Void type as unit type 285:and its only value is 187:), the type is called 118:functional programming 31:. For other uses, see 687:Difference in storage 134:programming languages 81:The unit type is the 33:Unit (disambiguation) 1002:. pp. 118–119. 29:Units of measurement 21:computer programming 1538:Primitive data type 1443:Recursive data type 1296:Algebraic data type 1172:Quadruple precision 990:Pierce, Benjamin C. 529:generic programming 330:(its only value is 322:(its only value is 1501:Abstract data type 1182:Extended precision 1141:Reduced precision 101:values and is the 40:mathematical logic 1581: 1580: 1313:Associative array 1177:Octuple precision 938:Singleton pattern 681:singleton pattern 300:and its value is 132:Several computer 109:is the type with 1606: 1553:Type constructor 1438:Opaque data type 1370:Record or Struct 1167:Double precision 1162:Single precision 1053: 1046: 1039: 1030: 1013: 976: 975: 973: 971: 955: 917: 914: 911: 908: 905: 902: 899: 896: 893: 890: 860: 857: 854: 851: 848: 845: 842: 839: 836: 833: 830: 827: 824: 821: 818: 815: 812: 809: 806: 803: 800: 797: 794: 791: 788: 785: 782: 779: 776: 773: 770: 767: 764: 761: 758: 755: 752: 749: 746: 743: 740: 737: 730: 726: 722: 718: 714: 674: 667: 664: 661: 658: 655: 652: 649: 646: 643: 640: 637: 634: 631: 628: 625: 622: 619: 616: 613: 610: 607: 604: 601: 598: 595: 592: 589: 586: 583: 580: 577: 574: 571: 568: 565: 562: 559: 556: 553: 550: 547: 540: 526: 519: 516: 513: 510: 507: 504: 501: 498: 495: 492: 489: 486: 483: 480: 477: 474: 471: 468: 465: 462: 459: 456: 453: 450: 447: 444: 441: 421: 417: 413: 378: 374: 363: 359: 348: 344: 333: 329: 325: 321: 303: 299: 288: 284: 273: 269: 265: 254: 250: 246: 228: 224: 220: 209: 205: 194: 190: 167: 163: 44:computer science 1616: 1615: 1609: 1608: 1607: 1605: 1604: 1603: 1584: 1583: 1582: 1577: 1558:Type conversion 1493: 1487: 1423:Enumerated type 1396: 1282: 1276:null-terminated 1252: 1217: 1105: 1062: 1057: 1010: 988: 985: 980: 979: 969: 967: 957: 956: 952: 947: 934: 919: 918: 915: 912: 909: 906: 903: 900: 897: 894: 891: 888: 867: 862: 861: 858: 855: 852: 849: 846: 843: 840: 837: 834: 831: 828: 825: 822: 819: 816: 813: 810: 807: 804: 801: 798: 795: 792: 789: 786: 783: 780: 777: 774: 771: 768: 765: 762: 759: 756: 753: 750: 747: 744: 741: 738: 735: 728: 724: 720: 716: 712: 709: 689: 672: 669: 668: 665: 662: 659: 656: 653: 650: 647: 644: 641: 638: 635: 632: 629: 626: 623: 620: 617: 614: 611: 608: 605: 602: 599: 596: 593: 590: 587: 584: 581: 578: 575: 572: 569: 566: 563: 560: 557: 554: 551: 548: 545: 536: 524: 521: 520: 517: 514: 511: 508: 505: 502: 499: 496: 493: 490: 487: 484: 481: 478: 475: 472: 469: 466: 463: 460: 457: 454: 451: 448: 445: 442: 439: 432: 419: 415: 409: 386: 376: 372: 361: 357: 346: 342: 331: 327: 323: 319: 301: 297: 286: 282: 271: 267: 263: 252: 248: 244: 232:, which is the 226: 222: 218: 217:the type named 207: 203: 192: 188: 165: 161: 130: 120:languages. The 97:, which allows 83:terminal object 38:In the area of 36: 17: 12: 11: 5: 1614: 1613: 1610: 1602: 1601: 1596: 1586: 1585: 1579: 1578: 1576: 1575: 1570: 1565: 1560: 1555: 1550: 1545: 1540: 1535: 1530: 1529: 1528: 1518: 1513: 1511:Data structure 1508: 1503: 1497: 1495: 1489: 1488: 1486: 1485: 1480: 1475: 1470: 1465: 1460: 1455: 1450: 1445: 1440: 1435: 1430: 1425: 1420: 1415: 1410: 1404: 1402: 1398: 1397: 1395: 1394: 1393: 1392: 1382: 1377: 1372: 1367: 1362: 1357: 1356: 1355: 1345: 1340: 1335: 1330: 1325: 1320: 1315: 1310: 1305: 1304: 1303: 1292: 1290: 1284: 1283: 1281: 1280: 1279: 1278: 1268: 1262: 1260: 1254: 1253: 1251: 1250: 1245: 1244: 1243: 1238: 1227: 1225: 1219: 1218: 1216: 1215: 1210: 1205: 1204: 1203: 1193: 1192: 1191: 1190: 1189: 1179: 1174: 1169: 1164: 1159: 1158: 1157: 1152: 1150:Half precision 1147: 1137:Floating point 1134: 1129: 1124: 1119: 1113: 1111: 1107: 1106: 1104: 1103: 1098: 1093: 1088: 1083: 1078: 1072: 1070: 1064: 1063: 1058: 1056: 1055: 1048: 1041: 1033: 1027: 1026: 1014: 1008: 984: 981: 978: 977: 949: 948: 946: 943: 942: 941: 933: 930: 887: 866: 863: 734: 708: 705: 688: 685: 544: 531:, such as C++ 438: 431: 428: 385: 382: 381: 380: 377:struct empty{} 373:std::monostate 365: 350: 335: 312: 305: 290: 275: 256: 237: 211: 196: 169: 129: 126: 103:initial object 62:. There is an 15: 13: 10: 9: 6: 4: 3: 2: 1612: 1611: 1600: 1597: 1595: 1592: 1591: 1589: 1574: 1571: 1569: 1566: 1564: 1561: 1559: 1556: 1554: 1551: 1549: 1546: 1544: 1541: 1539: 1536: 1534: 1531: 1527: 1524: 1523: 1522: 1519: 1517: 1514: 1512: 1509: 1507: 1504: 1502: 1499: 1498: 1496: 1490: 1484: 1481: 1479: 1476: 1474: 1471: 1469: 1466: 1464: 1461: 1459: 1456: 1454: 1451: 1449: 1446: 1444: 1441: 1439: 1436: 1434: 1433:Function type 1431: 1429: 1426: 1424: 1421: 1419: 1416: 1414: 1411: 1409: 1406: 1405: 1403: 1399: 1391: 1388: 1387: 1386: 1383: 1381: 1378: 1376: 1373: 1371: 1368: 1366: 1363: 1361: 1358: 1354: 1351: 1350: 1349: 1346: 1344: 1341: 1339: 1336: 1334: 1331: 1329: 1326: 1324: 1321: 1319: 1316: 1314: 1311: 1309: 1306: 1302: 1299: 1298: 1297: 1294: 1293: 1291: 1289: 1285: 1277: 1274: 1273: 1272: 1269: 1267: 1264: 1263: 1261: 1259: 1255: 1249: 1246: 1242: 1239: 1237: 1234: 1233: 1232: 1229: 1228: 1226: 1224: 1220: 1214: 1211: 1209: 1206: 1202: 1199: 1198: 1197: 1194: 1188: 1185: 1184: 1183: 1180: 1178: 1175: 1173: 1170: 1168: 1165: 1163: 1160: 1156: 1153: 1151: 1148: 1146: 1143: 1142: 1140: 1139: 1138: 1135: 1133: 1130: 1128: 1125: 1123: 1120: 1118: 1115: 1114: 1112: 1108: 1102: 1099: 1097: 1094: 1092: 1089: 1087: 1084: 1082: 1079: 1077: 1074: 1073: 1071: 1069: 1068:Uninterpreted 1065: 1061: 1054: 1049: 1047: 1042: 1040: 1035: 1034: 1031: 1025: 1023: 1018: 1015: 1011: 1009:0-262-16209-1 1005: 1001: 997: 996: 991: 987: 986: 982: 965: 961: 954: 951: 944: 939: 936: 935: 931: 929: 927: 923: 885: 883: 878: 876: 872: 864: 732: 706: 704: 702: 698: 694: 686: 684: 682: 678: 542: 539: 534: 530: 436: 429: 427: 425: 412: 407: 403: 399: 395: 391: 383: 370: 366: 355: 351: 340: 336: 317: 313: 310: 306: 295: 291: 280: 276: 261: 257: 242: 238: 235: 231: 216: 212: 201: 197: 186: 182: 178: 174: 170: 159: 155: 151: 147: 146: 145: 143: 139: 135: 127: 125: 123: 119: 114: 112: 108: 104: 100: 96: 92: 88: 84: 79: 78:of no types. 77: 73: 69: 65: 61: 60:singleton set 57: 53: 49: 45: 41: 34: 30: 26: 22: 1477: 1338:Intersection 1021: 993: 968:. Retrieved 963: 953: 926:typeof(null) 925: 921: 920: 881: 879: 874: 870: 868: 710: 690: 675:is really a 670: 522: 433: 387: 229: 131: 115: 110: 98: 90: 80: 67: 51: 37: 1599:Type theory 1568:Type theory 1563:Type system 1413:Bottom type 1360:Option type 1301:generalized 1187:Long double 1132:Fixed point 707:In Generics 693:record type 234:bottom type 215:Common Lisp 181:Standard ML 142:side effect 74:, i.e. the 64:isomorphism 48:type theory 25:type theory 1594:Data types 1588:Categories 1473:Empty type 1468:Type class 1418:Collection 1375:Refinement 1353:metaobject 1201:signedness 1060:Data types 983:References 316:JavaScript 302:struct{}{} 95:empty type 1548:Subtyping 1543:Interface 1526:metaclass 1478:Unit type 1448:Semaphore 1428:Exception 1333:Inductive 1323:Dependent 1288:Composite 1266:Character 1248:Reference 1145:Minifloat 1101:Bit array 1017:unit type 1000:MIT Press 865:Null type 677:singleton 606:unit_type 597:unit_type 576:unit_type 567:unit_type 558:unit_type 549:unit_type 533:templates 332:undefined 328:Undefined 122:void type 52:unit type 46:known as 1573:Variable 1463:Top type 1328:Equality 1236:physical 1213:Rational 1208:Interval 1155:bfloat16 992:(2002). 932:See also 673:the_unit 651:the_unit 618:the_unit 588:the_unit 561:the_unit 535:, where 362:NilClass 298:struct{} 245:NoneType 138:function 113:values. 87:category 1516:Generic 1492:Related 1408:Boolean 1365:Product 1241:virtual 1231:Address 1223:Pointer 1196:Integer 1127:Decimal 1122:Complex 1110:Numeric 1019:at the 970:2 March 349:object. 318:, both 150:Haskell 107:Boolean 85:in the 76:product 1506:Boxing 1494:topics 1453:Stream 1390:tagged 1348:Object 1271:String 1006:  907:return 889:typeof 829:String 817:static 814:public 802:return 778:static 775:public 763:return 739:static 736:public 679:; see 657:return 615:return 585:return 509:return 424:Pascal 416:struct 404:, and 371:, the 364:class. 339:Kotlin 326:) and 241:Python 183:, and 156:, and 72:tuples 1401:Other 1385:Union 1318:Class 1308:Array 1091:Tryte 945:Notes 699:as a 555:const 546:class 260:Swift 200:Scala 177:OCaml 54:is a 1521:Kind 1483:Void 1343:List 1258:Text 1096:Word 1086:Trit 1081:Byte 1004:ISBN 972:2024 922:null 910:null 895:null 882:null 875:null 871:null 853:null 832:args 823:main 820:void 805:null 790:Void 781:Void 766:null 751:Void 742:Void 729:null 725:Math 721:null 717:Void 713:Void 630:main 538:void 525:void 503:()); 485:void 479:main 467:void 458:void 449:void 440:void 420:void 411:void 354:Ruby 347:Unit 343:Unit 324:null 320:Null 287:null 283:Void 279:Java 264:Void 253:None 249:None 230:type 219:NULL 204:Unit 189:unit 154:Rust 91:zero 56:type 50:, a 42:and 23:and 1380:Set 1076:Bit 1024:Lab 904:(){ 856:)); 701:map 697:set 654:)); 627:int 552:{}; 476:int 406:PHP 394:C++ 388:In 369:C++ 367:In 358:nil 352:In 337:in 314:In 309:PHP 307:In 292:In 277:In 266:or 258:In 239:In 227:NIL 223:NIL 213:In 198:In 171:In 158:Elm 148:In 111:two 93:or 68:the 1590:: 998:. 962:. 884:: 731:. 633:() 473:{} 455:{} 408:, 400:, 398:C# 396:, 392:, 356:, 341:, 294:Go 272:() 268:() 208:() 193:() 185:F# 179:, 173:ML 166:() 162:() 152:, 99:no 1052:e 1045:t 1038:v 1022:n 1012:. 974:. 916:} 913:; 898:) 892:( 859:} 850:( 847:g 844:( 841:f 838:{ 835:) 826:( 811:} 808:; 799:{ 796:) 793:x 787:( 784:g 772:} 769:; 760:{ 757:) 754:x 748:( 745:f 666:} 663:; 660:0 648:( 645:g 642:( 639:f 636:{ 624:} 621:; 612:{ 609:) 603:( 600:g 594:} 591:; 582:{ 579:) 573:( 570:f 564:; 518:} 515:; 512:0 500:g 497:( 494:f 491:{ 488:) 482:( 470:) 464:( 461:g 452:) 446:( 443:f 402:D 390:C 379:. 304:. 289:. 255:. 236:. 210:. 195:. 35:.

Index

computer programming
type theory
Units of measurement
Unit (disambiguation)
mathematical logic
computer science
type theory
type
singleton set
isomorphism
tuples
product
terminal object
category
empty type
initial object
Boolean
functional programming
void type
programming languages
function
side effect
Haskell
Rust
Elm
ML
OCaml
Standard ML
F#
Scala

Text is available under the Creative Commons Attribution-ShareAlike License. Additional terms may apply.