Respond to an unknown method call: Difference between revisions
Respond to an unknown method call (view source)
Revision as of 01:01, 10 December 2017
, 6 years ago→{{header|Kotlin}}: Changed code so that the object itself now captures an attempt to call an unknown method.
(→{{header|Object Pascal}}: Incorrect.) |
(→{{header|Kotlin}}: Changed code so that the object itself now captures an attempt to call an unknown method.) |
||
Line 591:
=={{header|Kotlin}}==
Kotlin JS does not currently target ECMAScript 2015 and so the Proxy object cannot be used for this task. The only way it can currently be accomplished is to use the Mozilla extension __noSuchMethod__ property which works with FireFox 43 but is no longer supported by more up to date versions:
<lang scala>// Kotlin JS version 1.
class C
// this method prevents a TypeError being thrown if an unknown method is called
fun __noSuchMethod__(id: String, args: Array<Any>) {
if (args.size > 0) println("which takes arguments: ${args.asList()}")
}▼
}
fun main(args: Array<String>) {
val c: dynamic = C() // 'dynamic' turns off compile time checks
▲ c.foo() // the compiler now allows this call even though foo() is undefined
▲ }
▲ println("Class C does not have a method called foo")
}</lang>
|