Initialize associated Types


(James Campbell) #1

I would love to be able to initilize another type like this:

extension Unit where Self.UnitType: SignedIntegerType {

    init(_ value: Double) { //Ideally this could be generic too.

        self.init()

        self.value = Self.UnitType(value)

    }

}

···

--
 Wizard
james@supmenow.com
+44 7523 279 698


(Guillaume Lessard) #2

You can do that if your constraining protocol defines an initializer with the desired signature.
I tried with both a struct extension and a protocol extension, no problem.

Is this about modifying the interface of SignedIntegerType, then?

Guillaume Lessard


(James Campbell) #3

Yeah I want a protocol (In this case Unit) expression that says if the
class implementing this protocol has an associated type which is a
SignedIntegerType and it is initialized with a SignedIntegerType.

Then it should set the property value (Which should be the same type as the
assosiated type) with that SignedIntegerType

···

On Thu, Jan 7, 2016 at 4:33 PM, Guillaume Lessard < glessard@tffenterprises.com> wrote:

You can do that if your constraining protocol defines an initializer with
the desired signature.
I tried with both a struct extension and a protocol extension, no problem.

Is this about modifying the interface of SignedIntegerType, then?

Guillaume Lessard

--
 Wizard
james@supmenow.com
+44 7523 279 698