Skip to content

Commit 7554b46

Browse files
committed
---
yaml --- r: 36845 b: refs/heads/try2 c: af5cd34 h: refs/heads/master i: 36843: 4367c05 v: v3
1 parent 141244f commit 7554b46

File tree

2 files changed

+54
-27
lines changed

2 files changed

+54
-27
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: eb8fd119c65c67f3b1b8268cc7341c22d39b7b61
55
refs/heads/try: d324a424d8f84b1eb049b12cf34182bda91b0024
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 42ab33e5725eea7bfbfd9bc579c3c63a261b684f
8+
refs/heads/try2: af5cd341d3c6719c02cd4869dc27a7d47aa2c0d5
99
refs/heads/incoming: d9317a174e434d4c99fc1a37fd7dc0d2f5328d37
1010
refs/heads/dist-snap: 22efa39382d41b084fde1719df7ae8ce5697d8c9
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596

branches/try2/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)