Swift has an interesting take on composition via protocols (note, it also 
has the concept of single inheritance):

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html

it avoids copying anything into the child class, but rather sets up both 
the variables and methods that a child class should support. How the 
variable is actually implemented in the class is up to the class (which I 
think is the nice part). Thus you can do:

protocol Foo {
  var x:Int { get set } // x must evaluate to an Int, and can be both 'get' 
and 'set'
}

class Bar: Foo {
  var x:Int // treat it as a simple Int
}

class Baz: Foo {
  var x_latent:Int

  // x is a 'functional' variable where we explicitly define how it is 
get'd and set'd
  var x:Int {
    get {
      return x_latent/2
    }
    set {
      x_latent = 2*x
   }
}

While it does involve rewriting code (which I don't love), it importantly 
protects the developer from making mistakes (i.e. it will complain that `x` 
is not defined, and will force `x` to be of type Int).

Swift also has the concept of generics. You can make Protocols generic, so 
in theory you could make X of any type. Unfortunately, there appears to be 
a gap in the language (like Julia, it is still an evolving language), and 
making variables in the protocol appears to create issues down the line.

Reply via email to