Skip to content

Commit af5cd34

Browse files
committed
Move mast static method to the section on methods
/cc: #4217
1 parent 42ab33e commit af5cd34

File tree

1 file changed

+53
-26
lines changed

1 file changed

+53
-26
lines changed

doc/tutorial.md

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1586,6 +1586,36 @@ s.draw_borrowed();
15861586
(&@~s).draw_borrowed();
15871587
~~~
15881588

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+
15891619
We'll discuss implementations more in the context of [traits and
15901620
generics](#generics).
15911621

@@ -2113,6 +2143,29 @@ second parameter of type `self`.
21132143
In contrast, in the `impl`, `equals` takes a second parameter of
21142144
type `int`, only using `self` as the name of the receiver.
21152145

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+
21162169
## Bounded type parameters and static method dispatch
21172170

21182171
Traits give us a language for defining predicates on types, or
@@ -2238,32 +2291,6 @@ select the method to call at runtime.
22382291

22392292
This usage of traits is similar to Java interfaces.
22402293

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-
22672294
## Trait inheritance
22682295

22692296
We can write a trait declaration that _inherits_ from other traits, called _supertraits_.

0 commit comments

Comments
 (0)