ios - How to use extension methods as arguments in Swift? -
i implement dsl similar can builders in kotlin: http://kotlinlang.org/docs/reference/type-safe-builders.html
the idea use extension methods function arguments, can use methods class being extended within closure give argument. allowing inject methods , variables scope of closure.
it seems almost possible in swift, maybe missing something. following code works, right until try call head() within closure:
// class method available within closure class html { func head() { print("head") } } // create type same signature extension method typealias ext_method = html -> () -> () func html(op: ext_method) { let html = html() op(html)() // call extension method } html { head() // ! use of unresolved identifier 'head' } have had luck doing similar, or have idea how possible?
i have no idea if looking for, but
html { $0.head } would compile , seems produce expected output.
the closure takes single parameter (here using shorthand parameter name $0) instance of html. returns instance method $0.head function of type () -> ().
Comments
Post a Comment