c6347389d2
This moves pattern variable assignments of fully matched expressions into the case body. This will help backends (dart2js in particular) to reason about the code flow. Closes #54115 Change-Id: I598a384a829f16e91ab2d6a309499cf20b9cc121 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/339122 Commit-Queue: Johnni Winther <johnniwinther@google.com> Reviewed-by: Jens Johansen <jensj@google.com>
67 lines
2.4 KiB
Plaintext
67 lines
2.4 KiB
Plaintext
library;
|
|
import self as self;
|
|
import "dart:core" as core;
|
|
|
|
import "dart:math" as math;
|
|
|
|
abstract sealed class Shape extends core::Object {
|
|
synthetic constructor •() → self::Shape
|
|
: super core::Object::•()
|
|
;
|
|
abstract method calculateArea() → core::double;
|
|
}
|
|
class Square extends core::Object implements self::Shape {
|
|
final field core::double length;
|
|
constructor •(core::double length) → self::Square
|
|
: self::Square::length = length, super core::Object::•()
|
|
;
|
|
method calculateArea() → core::double
|
|
return this.{self::Square::length}{core::double}.{core::double::*}(this.{self::Square::length}{core::double}){(core::num) → core::double};
|
|
}
|
|
class Circle extends core::Object implements self::Shape {
|
|
final field core::double radius;
|
|
constructor •(core::double radius) → self::Circle
|
|
: self::Circle::radius = radius, super core::Object::•()
|
|
;
|
|
method calculateArea() → core::double
|
|
return #C1.{core::double::*}(this.{self::Circle::radius}{core::double}){(core::num) → core::double}.{core::double::*}(this.{self::Circle::radius}{core::double}){(core::num) → core::double};
|
|
}
|
|
static method calculateArea(self::Shape shape) → core::double
|
|
return block {
|
|
core::double #t1;
|
|
final synthesized self::Shape #0#0 = shape;
|
|
#L1:
|
|
{
|
|
{
|
|
hoisted core::double l;
|
|
if(#0#0 is self::Square) {
|
|
l = #0#0{self::Square}.{self::Square::length}{core::double};
|
|
#t1 = l.{core::double::*}(l){(core::num) → core::double};
|
|
break #L1;
|
|
}
|
|
}
|
|
{
|
|
hoisted core::double r;
|
|
if(#0#0 is self::Circle) {
|
|
r = #0#0{self::Circle}.{self::Circle::radius}{core::double};
|
|
#t1 = #C1.{core::double::*}(r){(core::num) → core::double}.{core::double::*}(r){(core::num) → core::double};
|
|
break #L1;
|
|
}
|
|
}
|
|
}
|
|
} =>#t1;
|
|
static method main() → dynamic {
|
|
self::Square s1 = new self::Square::•(2.0);
|
|
self::expect(s1.{self::Square::calculateArea}(){() → core::double}, self::calculateArea(s1));
|
|
self::Circle s2 = new self::Circle::•(3.0);
|
|
self::expect(s2.{self::Circle::calculateArea}(){() → core::double}, self::calculateArea(s2));
|
|
}
|
|
static method expect(dynamic expected, dynamic actual) → dynamic {
|
|
if(!(expected =={core::Object::==}{(core::Object) → core::bool} actual))
|
|
throw "Expected ${expected}, actual ${actual}";
|
|
}
|
|
|
|
constants {
|
|
#C1 = 3.141592653589793
|
|
}
|