unions

Typescript has unions, so are enums redundant?

浪尽此生 提交于 2019-11-29 06:02:17
问题 Ever since TypeScript introduced unions types, I wonder if there is any reason to declare an enum type. Consider the following enum type declaration: enum X { A, B, C } var x:X = X.A; and a similar union type declaration: type X: "A" | "B" | "C" var x:X = "A"; If they basically serve the same purpose, and unions are more powerful and expressive, then why are enums necessary? 回答1: As far as I see they are not redundant, due to the very simple reason that union types are purely a compile time

What's the major difference between “union” and “struct” in C.? [duplicate]

为君一笑 提交于 2019-11-29 05:56:14
问题 Possible Duplicate: Difference between a Structure and a Union in C I could understand what a struct means. But, i am bit confused with the difference between union and struct. Union is like a share of memory. What exactly it means.? 回答1: With a union, all members share the same memory. With a struct, they do not share memory, so a different space in memory is allocated to each member of the struct. For example: union foo { int x; int y; }; foo f; f.x = 10; printf("%d\n", f.y); Here, we

union of structs sharing same first members

泪湿孤枕 提交于 2019-11-29 04:12:58
I have been looking into an un-traditional way of achieving struct "polymorphism" in pre-C11 C. Let's say we have 2 structs: struct s1 { int var1; char var2; long var3; }; struct s2 { int var1; char var2; long var3; char var4; int var5; }; On most compilers, we could safely cast between pointers to the two and then access the common first members if no padding takes place. However, this is not standartized behaviour. Now, I found the following line in the C standard as far as C89: One special guarantee is made in order to simplify the use of unions: If a union contains several structures that

Padding in union is present or not

五迷三道 提交于 2019-11-29 02:06:27
Hello all, I want to know whether union uses padding? since the size of union is the largest data member size, can there be padding at the end? since the size of union is the largest data member size That need not be true. Consider union Pad { char arr[sizeof (double) + 1]; double d; }; The largest member of that union is arr . But usually, a double will be aligned on a multiple of four or eight bytes (depends on architecture and size of double ). On some architectures, that is even necessary since they don't support unaligned reads at all. So sizeof (union Pad) is usually larger than sizeof

Appending/concatenating two IEnumerable sequences

怎甘沉沦 提交于 2019-11-29 00:54:19
I have two sets of datarows. They are each IEnumerable. I want to append/concatenate these two lists into one list. I'm sure this is doable. I don't want to do a for loop and noticed that there is a Union method and a Join method on the two Lists. Any ideas? Assuming your objects are of the same type, you can use either Union or Concat . Note that, like the SQL UNION keyword, the Union operation will ensure that duplicates are eliminated, whereas Concat (like UNION ALL ) will simply add the second list to the end of the first. IEnumerable<T> first = ...; IEnumerable<T> second = ...;

Why do unions have a deleted default constructor if just one of its members doesn't have one?

空扰寡人 提交于 2019-11-28 23:32:59
N3797::9.5/2 [class.union] says: If any non-static data member of a union has a non-trivial default constructor (12.1), copy constructor (12.8), move constructor (12.8), copy assignment operator (12.8), move assignment operator (12.8), or destructor (12.4), the corresponding member function of the union must be user-provided or it will be implicitly deleted (8.4.3) for the union I was trying to understand that note by example: #include <iostream> #include <limits> struct A { A(const A&){ std::cout << "~A()" << std::endl; } //A has no default constructor }; union U { A a; }; U u; //error: call

C: Where is union practically used?

两盒软妹~` 提交于 2019-11-28 16:42:36
I have a example with me where in which the alignment of a type is guaranteed, union max_align . I am looking for a even simpler example in which union is used practically, to explain my friend. Yousf I usually use unions when parsing text. I use something like this: typedef enum DataType { INTEGER, FLOAT_POINT, STRING } DataType ; typedef union DataValue { int v_int; float v_float; char* v_string; }DataValue; typedef struct DataNode { DataType type; DataValue value; }DataNode; void myfunct() { long long temp; DataNode inputData; inputData.type= read_some_input(&temp); switch(inputData.type) {

Do union types actually exist in python?

时光怂恿深爱的人放手 提交于 2019-11-28 11:13:31
Since python is dynamically typed, of course we can do something like this: def f(x): return 2 if x else "s" But is the way python was actually intended to be used? or in other words, do union types exist in the sense they do in racket for example? Or do we only use them like this: def f(x): if x: return "x" where the only "union" we need is with None? Union typing is only needed when you have a statically typed language, as you need to declare that an object can return one of multiple types (in your case an int or str , or in the other example str or NoneType ). Python deals in objects only,

Questions regarding C++ non-POD unions

久未见 提交于 2019-11-28 11:01:18
C++11 gave us to possibility to use non-POD types within unions, say I have the following piece of code; union { T one; V two; } uny; Somewhere within my class, only one member will be active at a time, now my questions are rather simple. What is the default value of uny? - undefined? Whenever my class is destructed, which members (within the union), if any will be destructed? Suppose I have to std::typeinfo to keep track of which is the active member, should I then call the destructor explicitly on that member in the destructor? Does anyone have a link to the language proposal, which changed

Unions within unions

随声附和 提交于 2019-11-28 07:54:24
问题 In C, is it possible to define a union within another union? If no, why is it not possible? Or if yes, where can it be used? 回答1: Suppose you want to define: union myun { int x; sometype y; }; where sometype is a typedef defined by a library you're using. If the library happened to implement it as a union type, then this would be a union within a union, and it would make sense because you can't (from a good design standpoint) violate the encapsulation of the library's type. 回答2: Yes, it's