Skip to content

Instantly share code, notes, and snippets.

@tjw
Created November 5, 2014 05:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjw/d9e658e10e0bbea97139 to your computer and use it in GitHub Desktop.
Save tjw/d9e658e10e0bbea97139 to your computer and use it in GitHub Desktop.
Example for Radar 18877135
/*
Subclassing a FloatLiteralConvertible conforming class and then using the convertible bit uses the superclass initializer, even when the subclass type is specified explicitly.
% xcrun -sdk macosx swiftc ./subclass-float-literal.swift
% ./subclass-float-literal
b1 className = B
b1 = B
b2 className = A
assertion failed: Must be subclassed: file ./subclass-float-literal.swift, line 23
*/
public class A: FloatLiteralConvertible, Printable {
let value: Double;
public required init(floatLiteral value: Double) {
self.value = value
}
public var className: String { get { return "A" } }
public var name: String {
get {
assert(false, "Must be subclassed")
}
}
public var description: String {
get {
return self.name
}
}
}
public class B: A {
public override var className: String { get { return "B" } }
public override var name: String { get { return "B" } }
}
// Explicitly specifying the type and calling the subclass constructor works.
let b1:B = B(floatLiteral:1.0)
println("b1 className = \(b1.className)")
println("b1 = \(b1)")
// Explicitly specifying the type and assuming the float conversion path will pick it up instead gets the superclass (presumably) and then hits the assert()
let b2:B = 2.0
println("b2 className = \(b2.className)")
println("b2 = \(b2)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment