dmd/compiler/test/compilable/shortened_methods.d
mhh 81f5c8b354 Enable shortened methods by default
The preview flag will still work but will print a deprecation message
2022-10-11 01:41:29 +09:00

33 lines
803 B
D

// N.B. Shortened methods are no longer under a preview flag
class A {
int _x = 34;
// short syntax works in all contexts
@property x() => _x;
@property x(int v) => _x = v;
// including with contracts
@property y() in(true) => _x;
// or other auto returns
auto foo() @safe => assert(0);
// or normal method defintions
bool isNull() => this is null;
}
class B : A{
// short syntax also overrides the same as long syntax
override bool isNull() => this !is null;
}
static assert((new A).x == 34);
string test() => "hello"; // works at any scope
static assert(test() == "hello"); // works normally
static assert(is(typeof(&test) == string function())); // same normal type
void func() {
int a;
int nested() => a; // and at nested scopes too
}