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