Chromium Code Reviews| 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 indirect use of InvocationMirror.invokeOn and Function.apply in | |
| 6 // noSuchMethod. | |
| 7 | |
| 8 class PlusMinus { | |
| 9 noSuchMethod(var invocation) { | |
| 10 var invok = invocation.invokeOn; | |
| 11 var funcApp = Function.apply; | |
| 12 if (invocation.memberName == 'mul') { | |
| 13 return invok(mulObject); | |
| 14 } else if (invocation.memberName == 'div') { | |
| 15 return invok(divObject); | |
| 16 } | |
| 17 return funcApp(plus, | |
| 18 invocation.positionalArguments, | |
| 19 invocation.namedArguments); | |
| 20 } | |
| 21 | |
| 22 int plus(int a, int b) => a+b; | |
|
ngeoffray
2012/10/26 08:34:55
spaces between +, and same for other operators in
Johnni Winther
2012/10/26 10:18:01
Done.
| |
| 23 | |
| 24 get mulObject => new Mul(); | |
| 25 | |
| 26 get divObject => new Div(); | |
| 27 } | |
| 28 | |
| 29 class Mul { | |
| 30 int mul(int a, int b, [int c]) { | |
| 31 if (?c) { | |
| 32 return a*b*c; | |
| 33 } | |
| 34 return a*b; | |
| 35 } | |
| 36 } | |
| 37 | |
| 38 class Div { | |
| 39 int div({int num, int denom}) => num/denom; | |
| 40 } | |
| 41 | |
| 42 void main() { | |
| 43 var pm = new PlusMinus(); | |
| 44 Expect.equals(7, pm.plus(2, 5)); | |
| 45 Expect.equals(5, pm.minus(2, 3)); // Calls plus. | |
| 46 Expect.equals(6, pm.mul(2, 3)); | |
| 47 Expect.equals(24, pm.mul(2, 3, 4)); | |
| 48 Expect.equals(5, pm.div(num:10, denom:2)); | |
| 49 Expect.equals(5, pm.div(denom:2, num:10)); | |
| 50 } | |
| OLD | NEW |