我想实现类似,他们可以在科特林与建筑工地做什么DSL:http://kotlinlang.org/docs/reference/type-safe-builders.html如何在Swift中使用扩展方法作为参数?
的想法是使用扩展方法作为函数的参数,这样就可以使用来自类的方法关闭之内被延长你给出一个论点。基本上允许你将方法和变量注入闭包的范围。
它似乎是差不多可能在Swift中,但也许我错过了一些东西。下面的代码工作,直到正确我试图关闭内部调用head()
:
// Class with method to be available within closure
class HTML {
func head() {
print("head")
}
}
// Create a type with same signature as an extension method
typealias ext_method = HTML ->() ->()
func html(op: ext_method) {
let html = HTML()
op(html)() // call the extension method
}
html {
head() // ! Use of unresolved identifier 'head'
}
有任何人任何运气做类似的东西,或有一个想法,怎么会是可能的?
您可能会提到这也发布在Apple Developer Forum上:https://forums.developer.apple.com/thread/20481。 –
请原谅我的无知,但在这种情况下,DSL *是什么? –
@MartinR域特定语言https://en.wikipedia.org/wiki/Domain-specific_language – matt