Debian Codegraph @

gcc/testsuite/gdc.test/compilable/shortened_methods.d

raw
1 // N.B. Shortened methods are no longer under a preview flag
2 class A {
3 int _x = 34;
4 // short syntax works in all contexts
5 @property x() => _x;
6 @property x(int v) => _x = v;
7
8 // including with contracts
9 @property y() in(true) => _x;
10
11 // or other auto returns
12 auto foo() @safe => assert(0);
13
14 // or normal method defintions
15 bool isNull() => this is null;
16
17 this() {}
18 this(int x) { _x = x; }
19 this(float y) => this(cast(int) y);
20 }
21
22 class B : A{
23 // short syntax also overrides the same as long syntax
24 override bool isNull() => this !is null;
25 }
26
27 static assert((new A).x == 34);
28
29 string test() => "hello"; // works at any scope
30
31 static assert(test() == "hello"); // works normally
32 static assert(is(typeof(&test) == string function())); // same normal type
33
34 struct S(T) {}
35
36 void func() {
37 int a;
38 int nested() => a; // and at nested scopes too
39
40 // Issue 24088 - https://issues.dlang.org/show_bug.cgi?id=24088
41 S!int f() => S!int();
42 }
43
44 struct T
45 {
46 void inc() {}
47 this(this) => inc();
48
49 void free() {}
50 ~this() => free();
51 }