I'm making a unit framework in Swift, which has measurement superclass and subclasses for different units, such as Mass and Volume. A feature is to allow the framework to correctly guess what unit it is created with and returning the correct class. example code:
class Measurement {
var unitString : String
init(unknownUnit: String) {
// checks if the unit is either Volume or Mass, and returns an instance of that class
}
}
class Volume : Measurement {
init(unitString: String) {
}
}
class Mass : Measurement {
init(unitString: String) {
}
}
let mass = Mass("kg") // class: Mass
let volume = Volume("ml") // class: Volume
let shouldBeVolume = Measurement("ml") // class: Volume
let shouldBeMass = Measurement("kg") // class: Mass
Is it possible to have a inherited class create an object of a specific subclass when initializing it?
Library is named Indus Valley and open source on GitHub