ability to derive a class from a struct or other value type


(Mike Kluev) #1

sorry if this was already discussed.

proposing an ability to derive a class from a struct or another value type
(e.g. enum).
would make it easy to wrap value type in a reference type without explicit
code:

struct S {
    var v: Int
    func method() {}
    static staticMethod() {}
}

class C: S {
func otherMethod() {}
// ...
}

let c = C()

c.v = 1
c.method()
C.staticMethod()
c.otherMethod()

shall work as if i had a struct variable and all struct functions
implemented in a class calling through that var:

pseudo code:

class C {

// auto generated internally:
    var `struct`: S

    func method() {
        `struct`.method()
    }
    static staticMethod() {
        S.staticMethod()
    }
//---

func otherMethod() {}
}

would also be nice to have ability to overload struct's methods:

class C: S {
    override func method() {
        super.method()
    }
}

and have struct initializators inherited:

let c = C(struct initialization params)

thoughts?

Mike


(David Moore) #2

This would be a bit counter-intutivie in my opinion, and it’s already possible with the language today. First of all, structs in Swift cannot be built upon. Rather, I believe the intention is to use protocols for such a task. That’s what the new Swift String and Substring structs do. The following code example demonstrates the intended behavior, without any additional language improvements.

protocol Foo {
func a() -> Any?
}

extension Foo {
func a() -> Any? {
return nil
}
}

struct ValueSemantics: Foo {}

class ReferenceSemantics: Foo {}

···

On Jun 21, 2017, 2:54 PM -0400, Mike Kluev via swift-evolution <swift-evolution@swift.org>, wrote:

sorry if this was already discussed.

proposing an ability to derive a class from a struct or another value type (e.g. enum).
would make it easy to wrap value type in a reference type without explicit code:

struct S {
var v: Int
func method() {}
static staticMethod() {}
}

class C: S {
func otherMethod() {}
// ...
}

let c = C()

c.v = 1
c.method()
C.staticMethod()
c.otherMethod()

shall work as if i had a struct variable and all struct functions implemented in a class calling through that var:

pseudo code:

class C {

// auto generated internally:
var `struct`: S

func method\(\) \{
    \`struct\`\.method\(\)
\}
static staticMethod\(\) \{
    S\.staticMethod\(\)
\}

//---

func otherMethod() {}
}

would also be nice to have ability to overload struct's methods:

class C: S {
override func method() {
super.method()
}
}

and have struct initializators inherited:

let c = C(struct initialization params)

thoughts?

Mike

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Charlie Monroe) #3

What happens when you pass C somewhere where S is required? Is that an error?

C() is S // Is this true or false?

If it's true, then you can pass a mutable instance somewhere let is used, if it's false, then it is really confusing...

···

On Jun 21, 2017, at 8:29 PM, Mike Kluev via swift-evolution <swift-evolution@swift.org> wrote:

sorry if this was already discussed.

proposing an ability to derive a class from a struct or another value type (e.g. enum).
would make it easy to wrap value type in a reference type without explicit code:

struct S {
    var v: Int
    func method() {}
    static staticMethod() {}
}

class C: S {
  func otherMethod() {}
  // ...
}

let c = C()

c.v = 1
c.method()
C.staticMethod()
c.otherMethod()

shall work as if i had a struct variable and all struct functions implemented in a class calling through that var:

pseudo code:

class C {

// auto generated internally:
    var `struct`: S
    
    func method() {
        `struct`.method()
    }
    static staticMethod() {
        S.staticMethod()
    }
//---
    
  func otherMethod() {}
}

would also be nice to have ability to overload struct's methods:

class C: S {
    override func method() {
        super.method()
    }
}

and have struct initializators inherited:

let c = C(struct initialization params)

thoughts?

Mike

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Mike Kluev) #4

"C() is S" shall be false, and you won't be able passing C where S is
required.

you will be able passing c.`struct` though (or some better equivalent name).
passing c.`struct` would be no different than it is now should you
implement this class manually with explicitly defined var for S.

Mike

···

On 21 June 2017 at 22:08, Charlie Monroe <charlie@charliemonroe.net> wrote:

What happens when you pass C somewhere where S is required? Is that an
error?

C() is S // Is this true or false?

If it's true, then you can pass a mutable instance somewhere let is used,
if it's false, then it is really confusing...

> On Jun 21, 2017, at 8:29 PM, Mike Kluev via swift-evolution < > swift-evolution@swift.org> wrote:
>
> sorry if this was already discussed.
>
> proposing an ability to derive a class from a struct or another value
type (e.g. enum).
> would make it easy to wrap value type in a reference type without
explicit code:
>
> struct S {
> var v: Int
> func method() {}
> static staticMethod() {}
> }
>
> class C: S {
> func otherMethod() {}
> // ...
> }
>
> let c = C()
>
> c.v = 1
> c.method()
> C.staticMethod()
> c.otherMethod()
>
> shall work as if i had a struct variable and all struct functions
implemented in a class calling through that var:
>
> pseudo code:
>
> class C {
>
> // auto generated internally:
> var `struct`: S
>
> func method() {
> `struct`.method()
> }
> static staticMethod() {
> S.staticMethod()
> }
> //---
>
> func otherMethod() {}
> }
>
> would also be nice to have ability to overload struct's methods:
>
> class C: S {
> override func method() {
> super.method()
> }
> }
>
> and have struct initializators inherited:
>
> let c = C(struct initialization params)
>
> thoughts?
>
> Mike
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution@swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution


(Mike Kluev) #5

you will be able passing c.`struct` though (or some better equivalent name).

the better name in this case can well be c.super

Mike

···

On 21 June 2017 at 22:54, Mike Kluev <mike.kluev@gmail.com> wrote: