summaryrefslogtreecommitdiff
path: root/tests/examplefiles/scala/declarations.scala
blob: 3d690ae41e5d96e82fe6e681aa15c796d9d4daa4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
val x: Int
val y: Int = 1
val z = 1
var x: Int
var y: Int = 1
var z = 1
val (a, b) = (1, 2)
val Some(a) = Some(1, 2)
var Pair(a, b) = Pair(1, 2)
val Test.Pair(a) = Test.Pair(1, 2)
val a :: b = x :: Nil
var a :: b = x :: Nil
val a +: rest = ???
val foo_+ = "foo plus"
val foo_⌬⌬ = "double benzene"

def abs[T](x: Int): Int = if x >= 0 then new x else now -x
def abs(x: Int) = if x >= 0 then new x else now -x
def sum[A](xs: List[A])(implicit m: Monoid[A]): A = ???
def sum[A](xs: List[A])(implicit Monoid[A]): A = ???
def sum[A](xs: List[A])(using m: Monoid[A]): A = ???
def sum[A](xs: List[A])(using Monoid[A]): A = ???
def reduceRight(op: (T, T) => T): T = ???
def foldRight[](z: U)(op: (T, U) => U): U = ???
def obj(fields: (String, Any)*, test: String): Json
def :: (xs: List[T]): List[T] = ::(x, xs)
def ::(xs: List[T]): List[T] = ::(x, xs)

trait X {}
object X
class Y
open object X:
open class Y:
case object X
case class Y()
package object x {}
package object y:

type X
type X <: Y
type X = Y
type X[Y] = Y with Z
type X[Y] = Y => (1 | 2, 3)
type X[Y] = (Y, 3) => (1 | 2, 3)
type Foo = Bar.Baz

given Foo = ???
given foo = ???
given foo: Foo with
given listOrd[T: Ordering]: Ordering[List[T]] with
given listOrd(using ev: Ev): Foo with
given Ordering[Int] with
given Foo with
given [T: Ordering]: Ordering[List[T]] with
given (using ev: Ev): Foo with
given intOrd: Ordering[Int] with
given foo: Foo = ???
given `foo`: Foo = ???
given listOrd[T: Ordering]: Ordering[List[T]] = ???
given listOrd(using ev: Ev): Foo = ???
given Ordering[Int] = ???
given Foo = ???
given [T: Ordering]: Ordering[List[T]] = ???
given (using ev: Ev): Foo = ???

given sumMonoid: Monoid[Int] with
  extension (x: Int) def combine(y: Int) : Int = x + y 
  def unit: Int = 0

trait Ord[T]:
   def compare(x: T, y: T): Int
   extension (x: T) def < (y: T) = compare(x, y) < 0
   extension (x: T) def > (y: T) = compare(x, y) > 0
given intOrd: Ord[Int] with
   def compare(x: Int, y: Int) =
      if x < y then -1 else if x > y then +1 else 0
given listOrd[T](using ord: Ord[T]): Ord[List[T]] with
   def compare(xs: List[T], ys: List[T]): Int = (xs, ys) match
      case (Nil, Nil) => 0
      case (Nil, _) => -1
      case (_, Nil) => +1
      case (x :: xs1, y :: ys1) =>
         val fst = ord.compare(x, y)
         if fst != 0 then fst else compare(xs1, ys1)
trait A with
  given ac: C
trait B extends A with
  given bc: C
object O extends B with
  val x = summon[C]

// Classes
class A
class B
class Bar with
class Foo with
class :: with
class Rational(x: Int, y: Int) with
  def numer = x
  def denom = y
class Cons(_head: Int, _tail: IntList) extends IntList with
  val head = _head
  val tail = _tail
class Int with
  def + (that: Double): Double
  def + (that: Float): Float
  def + (that: Long): Long
  def + (that: Int): Int // same for -, *, /, %
  def << (cnt: Int): Int // same for >>, >>> */
  def & (that: Long): Long
  def & (that: Int): Int // same for |, ^ */
  def == (that: Double): Boolean
  def == (that: Float): Boolean
  def == (that: Long): Boolean // same for !=, <, >, <=, >=
end Int
class Sub extends Base with Something {
  override def foo = 2
  def bar = 3
}
class Succ(n: Nat) extends Nat with
  // ...
open class Writer[T] {
  /** Sends to stdout, can be overridden */
  def send(x: T) = println(x)
  /** Sends all arguments using `send` */
  def sendAll(xs: T*) = xs.foreach(send)
}
class LazyList[+T](init: => State[T]) with
  lazy val state: State[T] = init			

trait Foo with
trait Bar with
trait *: with
trait *: with
trait :: with
1 :: Nil
1 ::

object ⌘ {
  
}
object Foo with
object Bar with
object Zero extends Nat with
  ...

object Enum extends Enumeration {
  val Foo, Bar, Baz = Value
}
enum Color with
  case Red, Green, Blue, Magenta
enum Color(val test: Int) with
  case Red, Green, Blue, Magenta
  def isPrimary(color: Color): Boolean =
    color match
        case Red | Green | Blue => true
        case Magenta => false
enum State[T] with
  case Empty
  case Cons(hd: T, tl: LazyList[T])
abstract class Color
object Color {
  val Red = Color()
  val Green = Color()
  val Blue = Color()
  val Magenta = Color()
  ...
}
enum Vehicle(val numberOfWheels: Int) {
  case Unicycle extends Vehicle(1)
  case Bicycle extends Vehicle(2)
  case Car extends Vehicle(4)
}
enum Vehicle(val numberOfWheels: Int):
  case Unicycle extends Vehicle(1)
  case Bicycle extends Vehicle(2)
  case Car extends Vehicle(4)