You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: sips/pending/_posts/2016-01-01-static-members.md
+9-1Lines changed: 9 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ title: SIP 25 - @static fields and methods in Scala objects(SI-4581)
4
4
disqus: true
5
5
---
6
6
7
-
__Dmitry Petrashkoand Sébastien Doeraene__
7
+
__Dmitry Petrashko, Sébastien Doeraene and Martin Odersky__
8
8
9
9
__first submitted TODO 2016__
10
10
@@ -30,6 +30,7 @@ There is no special syntax proposed to access these members, they are accessed a
30
30
31
31
For example:
32
32
33
+
```scala
33
34
{% highlight scala %}
34
35
classFoo
35
36
@@ -41,10 +42,12 @@ object Foo {
41
42
println(Foo.x)
42
43
println(Foo.bar(12))
43
44
{% endhighlight %}
45
+
```
44
46
45
47
Intuively, the presence of the `@static` annotation ensures that a field/method is declared as a static member of the companion class.
46
48
For the JVM, the above would therefore look to other Java code as if it had been declared with the following Java code:
47
49
50
+
```java
48
51
{% highlight java %}
49
52
classFoo {
50
53
publicstaticint x =5;
@@ -53,10 +56,12 @@ class Foo {
53
56
}
54
57
}
55
58
{% endhighlight %}
59
+
```
56
60
57
61
In Scala.js, the `@static` annotation has no semantic effect in Scala objects, as they are not visible from JavaScript anyway (it could be used for optimizations).
58
62
It has a semantic effect on Scala.js-defined JS classes, for example:
59
63
64
+
```scala
60
65
{% highlight scala %}
61
66
@ScalaJSDefined
62
67
classFooextends js.Object
@@ -67,9 +72,11 @@ object Foo extends js.Object {
67
72
@static defbar(y: Int):Int= x + y
68
73
}
69
74
{% endhighlight %}
75
+
```
70
76
71
77
would look to JavaScript code as if it had been declared with the following JavaScript code:
72
78
79
+
```javascript
73
80
{% highlight javascript %}
74
81
classFooextendsObject {
75
82
staticbar(y) {
@@ -78,6 +85,7 @@ class Foo extends Object {
78
85
}
79
86
Foo.x=5; // in ES6, there is no declarative syntax for static fields yet
0 commit comments