@@ -1586,6 +1586,36 @@ s.draw_borrowed();
1586
1586
(&@~s).draw_borrowed();
1587
1587
~~~
1588
1588
1589
+ Implementations may also define _ static_ methods,
1590
+ which don't have an explicit ` self ` argument.
1591
+ The ` static ` keyword distinguishes static methods from methods that have a ` self ` :
1592
+
1593
+ ~~~~ {.xfail-test}
1594
+ impl Circle {
1595
+ fn area(&self) -> float { ... }
1596
+ static fn new(area: float) -> Circle { ... }
1597
+ }
1598
+ ~~~~
1599
+
1600
+ > *** Note*** : In the future the ` static ` keyword will be removed and static methods
1601
+ > will be distinguished solely by the presence or absence of the ` self ` argument.
1602
+ > In the current langugage instance methods may also be declared without an explicit
1603
+ > ` self ` argument, in which case ` self ` is an implicit reference.
1604
+ > That form of method is deprecated.
1605
+
1606
+ Constructors are one common application for static methods, as in ` new ` above.
1607
+ To call a static method, you have to prefix it with the type name and a double colon:
1608
+
1609
+ ~~~~
1610
+ # use float::consts::pi;
1611
+ # use float::sqrt;
1612
+ struct Circle { radius: float }
1613
+ impl Circle {
1614
+ static fn new(area: float) -> Circle { Circle { radius: sqrt(area / pi) } }
1615
+ }
1616
+ let c = Circle::new(42.5);
1617
+ ~~~~
1618
+
1589
1619
We'll discuss implementations more in the context of [ traits and
1590
1620
generics] ( #generics ) .
1591
1621
@@ -2113,6 +2143,29 @@ second parameter of type `self`.
2113
2143
In contrast, in the ` impl ` , ` equals ` takes a second parameter of
2114
2144
type ` int ` , only using ` self ` as the name of the receiver.
2115
2145
2146
+ Traits can also define static methods which are called by prefixing
2147
+ the method name with the trait name.
2148
+ The compiler will use type inference to decide which implementation to call.
2149
+
2150
+ ~~~~
2151
+ # trait Shape { static fn new(area: float) -> self; }
2152
+ # use float::consts::pi;
2153
+ # use float::sqrt;
2154
+ struct Circle { radius: float }
2155
+ struct Square { length: float }
2156
+
2157
+ impl Circle: Shape {
2158
+ static fn new(area: float) -> Circle { Circle { radius: sqrt(area / pi) } }
2159
+ }
2160
+ impl Square: Shape {
2161
+ static fn new(area: float) -> Square { Square { length: sqrt(area) } }
2162
+ }
2163
+
2164
+ let area = 42.5;
2165
+ let c: Circle = Shape::new(area);
2166
+ let s: Square = Shape::new(area);
2167
+ ~~~~
2168
+
2116
2169
## Bounded type parameters and static method dispatch
2117
2170
2118
2171
Traits give us a language for defining predicates on types, or
@@ -2238,32 +2291,6 @@ select the method to call at runtime.
2238
2291
2239
2292
This usage of traits is similar to Java interfaces.
2240
2293
2241
- ## Static methods
2242
-
2243
- Traits can define _ static_ methods, which don't have an implicit ` self ` argument.
2244
- The ` static ` keyword distinguishes static methods from methods that have a ` self ` :
2245
-
2246
- ~~~~
2247
- trait Shape {
2248
- fn area(&self) -> float;
2249
- static fn new_shape(area: float) -> Shape;
2250
- }
2251
- ~~~~
2252
-
2253
- Constructors are one application for static methods, as in ` new_shape ` above.
2254
- To call a static method, you have to prefix it with the trait name and a double colon:
2255
-
2256
- ~~~~
2257
- # trait Shape { static fn new_shape(area: float) -> self; }
2258
- # use float::consts::pi;
2259
- # use float::sqrt;
2260
- struct Circle { radius: float }
2261
- impl Circle: Shape {
2262
- static fn new_shape(area: float) -> Circle { Circle { radius: sqrt(area / pi) } }
2263
- }
2264
- let s: Circle = Shape::new_shape(42.5);
2265
- ~~~~
2266
-
2267
2294
## Trait inheritance
2268
2295
2269
2296
We can write a trait declaration that _ inherits_ from other traits, called _ supertraits_ .
0 commit comments