我想實現類似,他們可以在科特林與建築工地做什麼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