Custom Cast Operators

RemObjects Silver allows the implementation of implicit and explicit cast operators on types by declaring a static func named __implicit or __explicit.

Cast operators funcs must have one parameter and a result, and one of the two must be the type they are declared on.

public class Foo {
    public static func __implicit(_ other: Foo) -> Bar {
        return createBarFromFoo()
    }

    public static func __explicit(_ other: Foo) -> Bar {
        return createBarFromFoo()
    }

    public static func __implicit(_ other: Bar) -> Foo {
        return createFooFromBar()
    }

    public static func __explicit(_ other: Bar) -> Foo {
        return createFooFromBar()
    }
}

With an explicit cast operator declared on a class, explicit casts from one type to the other are allowed:

let bar = Bar()
let foo: Foo = f as? Foo

With an implicit cast operator declared on a class, automatic casts from one type to the other are allowed:

let bar = Bar()
let foo: Foo = f  // no explicit cast necessary