dmd/compiler/test/compilable/shortened_methods.d
mhh e3eccfe9a5 Fix Issue 24088 - Nested functions using the shortened syntax were not recognized correctly as declarations.
The fix was simply to amend `isDeclarator` to look for TOK.goesTo (i.e. `=>`)
2023-08-17 12:33:04 +02:00

38 lines
914 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
struct S(T) {}
void func() {
int a;
int nested() => a; // and at nested scopes too
// Issue 24088 - https://issues.dlang.org/show_bug.cgi?id=24088
S!int f() => S!int();
}