OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 // Test direct use of InvocationMirror.invokeOn and Function.apply in |
| 6 // noSuchMethod. |
| 7 |
| 8 class PlusMinus { |
| 9 noSuchMethod(InvocationMirror invocation) { |
| 10 if (invocation.memberName == 'mul') return invocation.invokeOn(mulObject); |
| 11 if (invocation.memberName == 'div') return invocation.invokeOn(divObject); |
| 12 return Function.apply(plus, |
| 13 invocation.positionalArguments, |
| 14 invocation.namedArguments); |
| 15 } |
| 16 |
| 17 int plus(int a, int b) => a + b; |
| 18 |
| 19 Mul get mulObject => new Mul(); |
| 20 |
| 21 Div get divObject => new Div(); |
| 22 } |
| 23 |
| 24 class Mul { |
| 25 int mul(int a, int b, [int c]) { |
| 26 if (?c) { |
| 27 return a * b * c; |
| 28 } |
| 29 return a * b; |
| 30 } |
| 31 } |
| 32 |
| 33 class Div { |
| 34 int div({int num, int denom}) => num/denom; |
| 35 } |
| 36 |
| 37 void main() { |
| 38 var pm = new PlusMinus(); |
| 39 Expect.equals(7, pm.plus(2, 5)); |
| 40 Expect.equals(5, pm.minus(2, 3)); // Calls plus. |
| 41 Expect.equals(6, pm.mul(2, 3)); |
| 42 Expect.equals(24, pm.mul(2, 3, 4)); |
| 43 Expect.equals(5, pm.div(num:10, denom:2)); |
| 44 Expect.equals(5, pm.div(denom:2, num:10)); |
| 45 } |
OLD | NEW |