[swift-evolution] ability to derive a class from a struct or other value type
Tony Allevato
tony.allevato at gmail.com
Fri Jun 23 10:31:12 CDT 2017
On Thu, Jun 22, 2017 at 5:15 PM Mike Kluev <mike.kluev at gmail.com> wrote:
> On 23 June 2017 at 02:43, Tony Allevato <tony.allevato at gmail.com
> <tony.allevato at gmail.com>> wrote:
>
>> There's been talk about "protocol forwarding" that I think would achieve
>> a lot of these goals, without the sharp edges involved trying to define
>> inheritance between a class and a struct. The proposed feature would
>> eliminate the boilerplate you're talking about.
>>
>> I'm on my phone so I can't pull up links to old discussions, but Matthew
>> Johnson is who was pushing it so you may want to look back at what he's
>> said on the topic.
>>
>
> thanks, i skimmed through it now and will have a closer look later.
> does it address (2) though? (preexisting value types we can't change)
>
> as to the sharp edges: i do not see a single one in the "manual"
> implementation of this approach and the idea of this proposal is that it is
> equivalent to the manual implementation...
>
Imagine something like this:
struct BaseStruct { ... }
class ExtendsStruct: BaseStruct { ... }
func foo(x: BaseStruct) { ... }
func bar(x: inout BaseStruct) { ... }
var x = ExtendsStruct(...)
foo(x)
bar(&x)
What is the behavior in each of these cases? Do foo and bar get a
value-type slice of x? Is that slice the same memory as is occupied by x,
or is it a copy? When bar mutates its argument, does it modify the same
memory occupied by x?
Supporting better composition (such as through protocol forwarding) avoids
all of these issues.
Let's go back to step 1: what's your use case for wanting inheritance
between a class and a struct vs. something like protocol forwarding? You've
proposed a feature in the abstract but it would be more helpful to know
what problem you're trying to solve. Do you just want to create a new type
that contains the same data and methods as a value type, plus others, but
as a reference type? Is it important for your use case that there is an
"is-a" relationship between the two types, or do you just want to reĆ¼se the
implementation of the value type? If you do want an "is-a" relationship,
how do you get around the fact that the *identity* of the reference type
would be lost if you used it polymorphically through the base struct type?
>
> Mike
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170623/87c37de5/attachment.html>
More information about the swift-evolution
mailing list