8000 Update to prefix types SIP, according to feedback on the PR. · soronpo/scala.github.com@173b2fb · GitHub
[go: up one dir, main page]

Skip to content

Commit 173b2fb

Browse files
committed
Update to prefix types SIP, according to feedback on the PR.
1 parent aa8603f commit 173b2fb

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

_sips/sips/2017-10-25-adding-prefix-types.md

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ permalink: /sips/:title.html
1414
| Date | Version |
1515
| ------------- | ---------------------------------------- |
1616
| Oct 25th 2017 | Split prefix types from [SIP33](http://docs.scala-lang.org/sips/priority-based-infix-type-precedence.html), and emphasize motivation |
17+
| Nov 29th 2017 | Updated SIP according to feedback in the PR, and recent update to SIP23 |
1718

1819

1920
Your feedback is welcome! If you're interested in discussing this proposal, head over to [this](https://contributors.scala-lang.org/t/sip-nn-make-infix-type-alias-precedence-like-expression-operator-precedence/471) Scala Contributors thread and let me know what you think.
@@ -54,24 +55,14 @@ object NonExistingPrefixTypes {
5455

5556
---
5657

57-
## Proposal
58-
Add support for prefix types, which is equivalent to the prefix operations for expressions.
59-
60-
```
61-
PrefixType ::= [`-' | `+' | `~' | `!'] SimpleType
62-
CompoundType ::= PrefixType
63-
| AnnotType {with AnnotType} [Refinement]
64-
| Refinement
65-
```
66-
67-
---
68-
6958
## Motivation
70-
Developers expect terms and types to be expressed the same for mathematical and logical operations.
59+
It is easier to reason about the language when mathematical and logical operations for both terms and types are expressed the same. The proposal is relevant solely for projects which utilize numeric literal type operations (supported by SIP23, which was not yet accepted into Lightbend Scala). However, the SIP is implementation is very small and should have minor effect on compiler performance.
7160

7261
### Motivating examples
7362

74-
The [singleton-ops library](https://github.com/fthomas/singleton-ops) with [Typelevel Scala](https://github.com/typelevel/scala) (which implemented [SIP-23](http://docs.scala-lang.org/sips/pending/42.type.html)) enable developers to express literal type operations more intuitively. For example:
63+
The [singleton-ops library](https://github.com/fthomas/singleton-ops) with [Typelevel Scala](https://github.com/typelevel/scala) (which implemented [SIP-23](http://docs.scala-lang.org/sips/pending/42.type.html)) enable developers to express literal type operations more intuitively.
64+
65+
Consider the following example, where `foo` has two equivalent implementations, one using types, while the other uses terms:
7566

7667
```scala
7768
import singleton.ops._
@@ -81,8 +72,10 @@ object PrefixExample {
8172
We would much rather write the following to acheive more clarity and shorter code:
8273
type Foo[Cond1, Cond2, Num] = ITE[Cond1 && !Cond2, -Num, Num]
8374
*/
84-
type Foo[Cond1, Cond2, Num] = ITE[Cond1 && ![Cond2], Negate[Num], Num]
75+
type Foo[Cond1, Cond2, Num] = ITE[Cond1 && ![Cond2], Negate[Num], Num]
76+
//foo executes typelevel operations by using singleton-ops
8577
def foo[Cond1, Cond2, Num](implicit f : Foo[Cond1, Cond2, Num]) : f.Out = f.value
78+
//foo executes term operations
8679
def foo(cond1 : Boolean, cond2 : Boolean, num : Int) : Int =
8780
if (cond1 && !cond2) -num else num
8881
}
@@ -97,6 +90,19 @@ Note: `type ![A]` is possible to define, but `type -[A]` is not due to collision
9790

9891
---
9992

93+
## Proposal
94+
95+
Add support for prefix types, which is equivalent to the prefix operations for expressions.
96+
97+
```
98+
PrefixType ::= [`-' | `+' | `~' | `!'] SimpleType
99+
CompoundType ::= PrefixType
100+
| AnnotType {with AnnotType} [Refinement]
101+
| Refinement
102+
```
103+
104+
------
105+
100106
## Implementation
101107

102108
A PR for this SIP is available at: [https://github.com/scala/scala/pull/6148](https://github.com/scala/scala/pull/6148)
@@ -143,18 +149,15 @@ This means that if unary prefix types are added, then `+42` will be a type expan
143149

144150
**Related Issues**
145151
* [Dotty Issue #2783](https://github.com/lampepfl/dotty/issues/2783)
146-
* [Typelevel Scala Issue #157](https://github.com/typelevel/scala/issues/157)
152+
* [Typelevel Scala Issue #157](https://github.com/typelevel/scala/issues/157) (Resolved in recent update to SIP23)
147153

148-
Both SIP23 implementation and Dotty's implementation of literal types currently fail compilation when infix types interact with a negative literal type.
154+
Dotty's implementation of literal types currently fail compilation when infix types interact with a negative literal type.
149155
```scala
150156
type ~~[A, B]
151157
type good = 2 ~~ 2
152158
type bad = 2 ~~ -2 //Error:(9, 20) ';' expected but integer literal found.
153-
type work_around = 2 ~~ (-2) //works for Typelevel scala, but fails in Dotty
159+
type work_around = 2 ~~ (-2) //Error in Dotty
154160
```
155-
It is not yet clear if this is an implementation issue, or if the spec should be changed to allow this as well.
156-
If this is a spec change, then the committee should approve it also.
157-
158161
----
159162

160163
### Bibliography

0 commit comments

Comments
 (0)
0