Created
March 26, 2023 19:40
-
-
Save WtzLAS/f15aaea67a4ff074d05b2b37f086bcd9 to your computer and use it in GitHub Desktop.
Scala 3 Dependent Vec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.compiletime.ops.* | |
import scala.compiletime.ops.int.* | |
import scala.reflect.ClassTag | |
import java.util.Scanner | |
class Vec[N <: Int, A: ClassTag](using ValueOf[N])(val arr: Array[A]): | |
def size() = valueOf[N] | |
def print() = for i <- 0 until valueOf[N] do println(arr(i)) | |
inline infix def ++[M <: Int](other: Vec[M, A]): Vec[N + M, A] = | |
given ValueOf[N + M] = ValueOf((valueOf[N] + valueOf[M]).asInstanceOf) | |
Vec[N + M, A](this.arr ++ other.arr) | |
object Vec: | |
inline def fill[N <: Int, A: ClassTag](n: N, a: => A)(using ValueOf[N]) = | |
Vec[N, A](Array.fill(n)(a)) | |
@main def main() = { | |
val scn = Scanner(System.in) | |
val n1 = scn.nextInt() | |
val n2 = scn.nextInt() | |
val v1 = Vec.fill[n1.type, Int](n1, 0) | |
val v2 = Vec.fill[n2.type, Int](n2, 0) | |
val v3 = v1 ++ v2 | |
println(v3.size()) | |
v3.print() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment