swift – Subclass a Struct或Similar

I understand how structures and classes (and protocols) work at a basic level. I have a fairly common situation:

I need to use operators They must be copied during assignment.
These types have complex structures, and I want to be able to specialize through subclassing, otherwise the code will be copied everywhere, which would be bad programming.
/p>

I have tried protocols and extensions but because the protocol is not universal I cannot define the (universal) operator I want.
If I use the course, I will not copy the assignment.

Today’s example is that I have Matrix and SquareMatrix, with specific square matrix functions. Some operators and matrices can be filled by anything that conforms to my ring protocol. I tried to use association types and extensions to define the protocol in Almost all functions.

Edit: I really want to know what I should code. In the matrix case, I need to be able to pass a square matrix like any other matrix, so is subclassing the only option ? Maybe I am wrong. The main problem is that when I have to write a function that discusses internal values, I have to know the generic type parameters to do anything useful. For example, when the definition is added, I have to create a new matrix and declare Its generic type, but when I only know a certain (non-generic) protocol, where do I get it-its real type is generic but even though the protocol has this associated type, I can’t get it out .

The solution thanks to alexander momchliov. Basically it takes more work to completely move the code into the protocol extension and use “Self” for all related types. In the extension, the compiler treats generics I am satisfied with the type of content.

The code is private, sorry, I cannot paste anything in this question. Thank you for your patience and help.

Structural inheritance/polymorphism is impossible for at least two reasons (I can think of).

> Structures are stored and moved by value. This requires the compiler to know the exact size of the structure at compile time so that it knows how many bytes to copy after the structure instance starts.

Assume there is a structure A and one inherited from A The structure of B. As long as the compiler sees a variable of type A, it cannot determine whether the runtime type is really A, or whether B is used. If B adds new storage attributes that A does not have, the size of B will be the same A is different (greater than). The compiler will not be able to determine the runtime type and the size of these structures. Polymorphism requires a function table. The function table will be stored as a static member of the structure type. But to access this static member, Each structure instance requires an instance member to encode the type of the instance. This is usually called an “isa” pointer (as in, this instance is of type A). For each instance, this will be 8 bytes of overhead (in 64 bits System). Considering that Int, Bool, Double and many other common types are implemented as structures, this would be an unacceptable amount of overhead. Imagine that Bool is a single-byte value and requires 8 bytes of overhead . That efficiency is 11%!

For these reasons, protocols play an important role in Swift because they allow you to introduce inheritance-like behavior without these problems.

< p>I understand how structures and classes (and protocols) work at a basic level. I have a fairly common situation:

I need to use operator generic value types, they must be in Copy when assigning.
These types have a complex structure, and I want to be able to specialize through subclassing, otherwise the code will be copied everywhere, which would be bad programming.

I have tried I have passed protocols and extensions, but because the protocol is not universal, I cannot define the (universal) operator I want.
If I use the course, I won’t copy the homework.

Today’s example is mine There are Matrix and SquareMatrix, which have specific square matrix functions. Some operators and matrices can be filled by anything that conforms to my ring protocol. I try to use association types and extensions to define almost all functions in the protocol.

< p>Edit: I really want to know what I should encode. In the matrix case, I need to be able to pass a square matrix like any other matrix, so is subclassing the only option? Maybe I am wrong. The main problem is that when I have to write a function that discusses internal values, I have to know the generic type parameters to do anything useful. For example, when the definition is added, I have to create a new matrix and declare Its generic type, but when I only know a certain (non-generic) protocol, where do I get it-its real type is generic but even though the protocol has this associated type, I can’t get it out .

The solution thanks to alexander momchliov. Basically it takes more work to completely move the code into the protocol extension and use “Self” for all related types. In the extension, the compiler treats generics I am satisfied with the type of content.

The code is private, sorry, I cannot paste anything in this question. Thank you for your patience and help.

< /p>

Structural inheritance/polymorphism is impossible for at least two reasons (I can think of).

>Structures are stored and moved by value. This requires the compiler to Know the exact size of the structure at compile time so that you know how many bytes to copy after the structure instance is started.

Suppose there is a structure A and a structure B inherited from A. As long as the compiler sees the type A Variable, it cannot determine whether the runtime type is really A, or whether B is used. If B adds new storage attributes that A does not have, the size of B will be different (greater than) from A. The compiler will not be able to determine whether to run Time type and the size of these structures. Polymorphism requires a function table. The function table will be stored as a static member of the structure type. But to access this static member, each structure instance needs an instance member to encode the instance The type. This is usually called an “isa” pointer (as in, this instance is of type A). For each instance, this will be 8 bytes of overhead (on a 64-bit system). Consider Int, Bool, Double, and Many other common types are implemented as structures, which would be an unacceptable amount of overhead. Imagine that Bool is a single-byte value and requires 8 bytes of overhead. That efficiency is 11%!

For these reasons, protocols play an important role in Swift because they allow you to introduce inheritance-like behavior without these problems.

p>

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 4102 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.