Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Can we introduce syntax desugaring for type-apply update? #21571

WojciechMazur started this conversation in Feature Requests
Discussion options

Here's example of compile-time or runtime-checked arrays with bounds. Scala allows to definition a desugaring for def update(idx: Int, v: T): Unit allowing to use x(idx) = v. However, the limitation of this feature is the fact that we need to pass arguments always in the apply arguments. When we compare it with type-apply apply method.

I wonder if the compiler could also accept the additional desugaring for type-apply based calls which would allow to implement a symetrical apply/update methods using type apply. The idea is to allow to switch easily from runtime-checked to compile-time checked bounds by only changing type of brackets.

 import scala.compiletime.ops.int.{>=, <}
 import scala.compiletime.ops.boolean.&&
 import scala.compiletime.constValue
 final abstract class Array[T, Size <: Int]:
 // bounds checked at runtime
 def apply(n: Int): T = ??? 
 def update(n: Int, value: T): Unit = ???
 // bounds checked at compile time
 def apply[N <: Int](using ((N >= 0) && (N < Size)) =:= true): T = ???
 def update[N <: Int](value: T)(using ((N >= 0) && (N < Size)) =:= true): Unit = ???
 def test = 
 val arr: Array[Array[Int, 5], 5] = ???
 arr(2)(5) == 42
 val accessRT = arr(2)(4)
 val accessCT = arr[2][4]
 val setMixed = arr[2](4) = -1
 val setCT = arr[2][4] = -1 // error
 // proposal, should desugar to arr.apply[2].update[4](-1)
You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /