diff --git a/code-snippets/.scalafmt.conf b/code-snippets/.scalafmt.conf new file mode 100644 index 000000000..932d50b0b --- /dev/null +++ b/code-snippets/.scalafmt.conf @@ -0,0 +1,51 @@ +version = 3.7.2 +runner.dialect = scala3 + +style = defaultWithAlign +indentOperator.preset = akka +maxColumn = 120 +rewrite.rules = [RedundantParens, AvoidInfix] +align.tokens = [{code = "=>", owner = "Case"}] +align.openParenDefnSite = false +align.openParenCallSite = false +optIn.breakChainOnFirstMethodDot = false +optIn.configStyleArguments = false +danglingParentheses.defnSite = false +danglingParentheses.callSite = false +rewrite.neverInfix.excludeFilters = [ + and + min + max + until + to + by + eq + ne + "should.*" + "contain.*" + "must.*" + in + ignore + be + taggedAs + thrownBy + synchronized + have + when + size + only + noneOf + oneElementOf + noElementsOf + atLeastOneElementOf + atMostOneElementOf + allElementsOf + inOrderElementsOf + theSameElementsAs + message +] +rewriteTokens = { + "⇒": "=>" + "→": "->" + "←": "<-" +} diff --git a/code-snippets/build.sbt b/code-snippets/build.sbt index 1d470b7e5..4246cc3b9 100644 --- a/code-snippets/build.sbt +++ b/code-snippets/build.sbt @@ -1,23 +1,19 @@ - Global / onChangedBuildSource := ReloadOnSourceChanges lazy val `scala-3-snippets` = project .in(file(".")) - .settings(ThisBuild / scalaVersion := "3.2.2") + .settings(ThisBuild / scalaVersion := "3.3.0") .settings( name := "dotty-simple", version := "0.1.0", - - libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % "test" - ) + libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % "test") .aggregate( `new-control-structure-syntax`, `contextual-abstractions`, `intersection-and-union-types`, `enumerations`, `export-clause`, - `top-level-definitions`, - ) + `top-level-definitions`) lazy val `new-control-structure-syntax` = project.in(file("new-control-structure-syntax")) @@ -27,7 +23,7 @@ lazy val `enumerations` = project.in(file("enumerations")) lazy val `export-clause` = project.in(file("export-clause")) -lazy val `intersection-and-union-types` = project.in(file("intersection-and-union-types")) +lazy val `intersection-and-union-types` = + project.in(file("intersection-and-union-types")).settings(scalacOptions ++= Seq("-source", "future-migration")) lazy val `top-level-definitions` = project.in(file("top-level-definitions")) - diff --git a/code-snippets/contextual-abstractions/build.sbt b/code-snippets/contextual-abstractions/build.sbt index 476ceeeab..7f6508d60 100644 --- a/code-snippets/contextual-abstractions/build.sbt +++ b/code-snippets/contextual-abstractions/build.sbt @@ -1,3 +1 @@ -scalacOptions ++= Seq( - "-source:3.0", -) +scalacOptions ++= Seq("-source:3.0") diff --git a/code-snippets/contextual-abstractions/src/main/scala/org/lunatech/dotty/multiversalequality/BadRepository.scala b/code-snippets/contextual-abstractions/src/main/scala/org/lunatech/dotty/multiversalequality/BadRepository.scala index 520801863..c7a34d61d 100644 --- a/code-snippets/contextual-abstractions/src/main/scala/org/lunatech/dotty/multiversalequality/BadRepository.scala +++ b/code-snippets/contextual-abstractions/src/main/scala/org/lunatech/dotty/multiversalequality/BadRepository.scala @@ -2,12 +2,10 @@ package org.lunatech.dotty.multiversalequality import java.util.UUID -/** - * In this example we imagine that we refactored 'Item' so that now we use - * UUID instead of our custom-made 'Id' value class to identify items. - * But in the refactor we forgot to change the Repository.findById. - * This will type-check but we will not find the repository - */ +/** In this example we imagine that we refactored 'Item' so that now we use UUID instead of our custom-made 'Id' value + * class to identify items. But in the refactor we forgot to change the Repository.findById. This will type-check but + * we will not find the repository + */ object Bad1: final case class Id(value: Long) extends AnyVal final case class Item(id: UUID) @@ -17,20 +15,18 @@ object Bad1: items.find(_.id == id) } -/** - * Same as above but now we import strictEquality in the scope. This will now - * fail to compile because we don't have any Eql typeclass instances for - * comparing 'Id' with UUID - */ +/** Same as above but now we import strictEquality in the scope. This will now fail to compile because we don't have any + * Eql typeclass instances for comparing 'Id' with UUID + */ object Bad2: final case class Id(value: Long) extends AnyVal final case class Item(id: UUID) import scala.language.strictEquality - /** COMMENTED OUT. DOES NOT COMPILE. - * For illustration purposes - */ - // class Repository(items: Seq[Item]): + + /** COMMENTED OUT. DOES NOT COMPILE. For illustration purposes + */ + // class Repository(items: Seq[Item]): // def findById(id: Id): Option[Item] = // items.find(_.id == id) // Thus futile comparison is flagged as an error diff --git a/code-snippets/contextual-abstractions/src/main/scala/org/lunatech/dotty/opaquetypes/Units.scala b/code-snippets/contextual-abstractions/src/main/scala/org/lunatech/dotty/opaquetypes/Units.scala index e006b0cad..8903d051b 100644 --- a/code-snippets/contextual-abstractions/src/main/scala/org/lunatech/dotty/opaquetypes/Units.scala +++ b/code-snippets/contextual-abstractions/src/main/scala/org/lunatech/dotty/opaquetypes/Units.scala @@ -3,9 +3,7 @@ package org.lunatech.dotty.opaquetypes object Scala2TypeAliases: type Kilometres = Double class Rocket(distanceTravelled: Kilometres): - def advance(distanceToAdvance: Kilometres): Rocket = new Rocket( - distanceTravelled + distanceToAdvance - ) + def advance(distanceToAdvance: Kilometres): Rocket = new Rocket(distanceTravelled + distanceToAdvance) type Miles = Double class Booster(): @@ -18,11 +16,11 @@ object Scala2ClassWrappers: case class Kilometres(value: Double) class Rocket(distanceTravelled: Kilometres): def advance(distanceToAdvance: Kilometres): Rocket = new Rocket( - Kilometres(distanceTravelled.value + distanceToAdvance.value) - ) + Kilometres(distanceTravelled.value + distanceToAdvance.value)) case class Miles(value: Double) class Booster() { + /** COMMENTED OUT. DOES NOT COMPILE. */ // def advanceRocket(rocket: Rocket, distanceToAdvance: Miles): Rocket = { @@ -30,7 +28,6 @@ object Scala2ClassWrappers: // rocket.advance(distanceToAdvance) // } - } end Scala2ClassWrappers @@ -137,7 +134,7 @@ object Scala3OpaqueTypeAliasesDefinitions: def toMiles: Miles = a / 1.6 extension (a: Miles) - @targetName("plusMiles") + @targetName("plusMiles") def +(b: Miles): Miles = a + b def toKm: Kilometres = a * 1.6 @@ -148,9 +145,7 @@ object Scala3OpaqueTypeAliasesTypeSafety: import Scala3OpaqueTypeAliasesDefinitions._ class Rocket(distanceTravelled: Kilometres): - def advance(distanceToAdvance: Kilometres): Rocket = new Rocket( - distanceTravelled + distanceToAdvance - ) + def advance(distanceToAdvance: Kilometres): Rocket = new Rocket(distanceTravelled + distanceToAdvance) class Booster(): def advanceRocket(rocket: Rocket, distanceToAdvance: Kilometres): Rocket = { @@ -169,9 +164,7 @@ object Scala3OpaqueTypeAliasesNoAllocations1: export Scala3OpaqueTypeAliasesDefinitions._ class Rocket(val distanceTravelled: Kilometres): - def advance(distanceToAdvance: Kilometres): Rocket = new Rocket( - distanceTravelled + distanceToAdvance - ) + def advance(distanceToAdvance: Kilometres): Rocket = new Rocket(distanceTravelled + distanceToAdvance) type Distance = Kilometres | Miles class Booster(): @@ -179,7 +172,7 @@ object Scala3OpaqueTypeAliasesNoAllocations1: // SO WE HAVE A BUG. Any 'Kilometres' passed to this method will be multiplied by 1.6 def advanceRocket(rocket: Rocket, distanceToAdvance: Distance): Rocket = { val distanceInKm = distanceToAdvance match { - case miles: Miles => miles.toKm + case miles: Miles => miles.toKm case km: Kilometres => km } rocket.advance(distanceInKm) @@ -202,9 +195,7 @@ object Scala3OpaqueTypeAliasesNoAllocations2: import Scala3OpaqueTypeAliasesDefinitions._ class Rocket(val distanceTravelled: Kilometres) { - def advance(distanceToAdvance: Kilometres): Rocket = new Rocket( - distanceTravelled + distanceToAdvance - ) + def advance(distanceToAdvance: Kilometres): Rocket = new Rocket(distanceTravelled + distanceToAdvance) } type Conversion[A] = A => Kilometres @@ -239,6 +230,6 @@ object Scala3OpaqueTypeAliasesNoAllocations3: * public static double[] distances() { * return Scala3OpaqueTypeAliasesNoAllocations3$.MODULE$.distances(); * } - * + * */ val distances: Array[Kilometres] = Array(Kilometres(10)) // No allocation of Kilometres object diff --git a/code-snippets/contextual-abstractions/src/main/scala/org/lunatech/dotty/using/ContextBounds.scala b/code-snippets/contextual-abstractions/src/main/scala/org/lunatech/dotty/using/ContextBounds.scala index 4c5890c83..cb450be07 100644 --- a/code-snippets/contextual-abstractions/src/main/scala/org/lunatech/dotty/using/ContextBounds.scala +++ b/code-snippets/contextual-abstractions/src/main/scala/org/lunatech/dotty/using/ContextBounds.scala @@ -3,8 +3,8 @@ package org.lunatech.dotty.using trait Ord[T] { def compare(x: T, y: T): Int extension (x: T) - def < (y: T) = compare(x, y) < 0 - def > (y: T) = compare(x, y) > 0 + def <(y: T) = compare(x, y) < 0 + def >(y: T) = compare(x, y) > 0 } given ordInt: Ord[Int] with { @@ -16,10 +16,10 @@ object ContextBounds { if (x > y) x else y // The following will still be allowed in Scala 3.0, but disallowed in 3.1 - //@main def contextBoundsMain = println(max(1,3)(ordInt)) + // @main def contextBoundsMain = println(max(1,3)(ordInt)) } // If you want to explicitly pass an instance of Ord[Int], do it as follows -@main def contextBoundsMain(): Unit = - println(ContextBounds.max(1,3)(using ordInt)) \ No newline at end of file +@main def contextBoundsMain(): Unit = + println(ContextBounds.max(1, 3)(using ordInt)) diff --git a/code-snippets/contextual-abstractions/src/main/scala/org/lunatech/dotty/using/Engine.scala b/code-snippets/contextual-abstractions/src/main/scala/org/lunatech/dotty/using/Engine.scala index 54833340b..6198bcbbb 100644 --- a/code-snippets/contextual-abstractions/src/main/scala/org/lunatech/dotty/using/Engine.scala +++ b/code-snippets/contextual-abstractions/src/main/scala/org/lunatech/dotty/using/Engine.scala @@ -1,3 +1,3 @@ package org.lunatech.dotty.using -final case class Engine(name: String) \ No newline at end of file +final case class Engine(name: String) diff --git a/code-snippets/contextual-abstractions/src/main/scala/org/lunatech/dotty/using/EngineConfig.scala b/code-snippets/contextual-abstractions/src/main/scala/org/lunatech/dotty/using/EngineConfig.scala index 7f166697c..707475719 100644 --- a/code-snippets/contextual-abstractions/src/main/scala/org/lunatech/dotty/using/EngineConfig.scala +++ b/code-snippets/contextual-abstractions/src/main/scala/org/lunatech/dotty/using/EngineConfig.scala @@ -1,3 +1,3 @@ package org.lunatech.dotty.using -final case class EngineConfig(initialSpeed: Int, maxSpeed: Int, label: String) \ No newline at end of file +final case class EngineConfig(initialSpeed: Int, maxSpeed: Int, label: String) diff --git a/code-snippets/contextual-abstractions/src/main/scala/org/lunatech/dotty/using/MultipleUsingClauses.scala b/code-snippets/contextual-abstractions/src/main/scala/org/lunatech/dotty/using/MultipleUsingClauses.scala index dd1bd14a6..f6dc0d437 100644 --- a/code-snippets/contextual-abstractions/src/main/scala/org/lunatech/dotty/using/MultipleUsingClauses.scala +++ b/code-snippets/contextual-abstractions/src/main/scala/org/lunatech/dotty/using/MultipleUsingClauses.scala @@ -4,10 +4,7 @@ final case class RecordDev(recordType: String) final case class PlayerDev(playerType: String) object MultipleUsingClauses { - def recordAndMonitor(recordGain: Int) - (using recordDev: RecordDev) - (volume: Int = 3) - (using player: PlayerDev) = { + def recordAndMonitor(recordGain: Int)(using recordDev: RecordDev)(volume: Int = 3)(using player: PlayerDev) = { println(s"Recording with gain $recordGain from $recordDev to $player with volume $volume") } @@ -27,8 +24,8 @@ object MultipleUsingClauses2 { opaque type Mul = Int opaque type Add = Int - object Mul { def apply(n: Int): Mul = n} - object Add { def apply(n: Int): Add = n} + object Mul { def apply(n: Int): Mul = n } + object Add { def apply(n: Int): Add = n } import scala.annotation.targetName @@ -39,7 +36,7 @@ object MultipleUsingClauses2 { extension (a: Add) @targetName("addAsInt") def asInt: Int = a - + @main def MUC_1: Unit = { val r = 5 println(r) @@ -57,4 +54,4 @@ object Use { val r = 10 * summon[Mul].asInt + summon[Add].asInt println(r) } -} \ No newline at end of file +} diff --git a/code-snippets/contextual-abstractions/src/main/scala/org/lunatech/dotty/using/UsingBaseSamples.scala b/code-snippets/contextual-abstractions/src/main/scala/org/lunatech/dotty/using/UsingBaseSamples.scala index 6ef739cf2..0f6353dea 100644 --- a/code-snippets/contextual-abstractions/src/main/scala/org/lunatech/dotty/using/UsingBaseSamples.scala +++ b/code-snippets/contextual-abstractions/src/main/scala/org/lunatech/dotty/using/UsingBaseSamples.scala @@ -12,7 +12,7 @@ object Scala2Example { @main def CA_0: Unit = { startEngine(Engine("AC-35-B/002")) } - + } object MovingToDotty_1 { @@ -25,7 +25,7 @@ object MovingToDotty_1 { @main def CA_1: Unit = { startEngine(Engine("AC-35-B/002")) } - + } object MovingToDotty_2 { @@ -38,5 +38,5 @@ object MovingToDotty_2 { @main def CA_2: Unit = { startEngine(Engine("AC-35-B/002")) } - -} \ No newline at end of file + +} diff --git a/code-snippets/enumerations/src/main/scala/org/lunatech/dotty/enumeration/EnumForProtocol.scala b/code-snippets/enumerations/src/main/scala/org/lunatech/dotty/enumeration/EnumForProtocol.scala index 46c06c26c..f439d7cc1 100644 --- a/code-snippets/enumerations/src/main/scala/org/lunatech/dotty/enumeration/EnumForProtocol.scala +++ b/code-snippets/enumerations/src/main/scala/org/lunatech/dotty/enumeration/EnumForProtocol.scala @@ -10,7 +10,7 @@ object SomeActor { } export Command._ - //def apply(): Behavior[Command] = ??? + // def apply(): Behavior[Command] = ??? } -// someActor ! SomeActor.Command.Execute(5) \ No newline at end of file +// someActor ! SomeActor.Command.Execute(5) diff --git a/code-snippets/enumerations/src/main/scala/org/lunatech/dotty/enumeration/EnumMemberVisibility.scala b/code-snippets/enumerations/src/main/scala/org/lunatech/dotty/enumeration/EnumMemberVisibility.scala index e64e3709c..6ddecafb4 100644 --- a/code-snippets/enumerations/src/main/scala/org/lunatech/dotty/enumeration/EnumMemberVisibility.scala +++ b/code-snippets/enumerations/src/main/scala/org/lunatech/dotty/enumeration/EnumMemberVisibility.scala @@ -1,39 +1,39 @@ package org.lunatech.dotty.enumeration @main def enumMemberVisibilityDefault = { - + enum Command { case Reset case IncrementBy(inc: Int) case Configure(init: Int, inc: Int) } - val reset = Command.Reset // `reset` is a singleton case mapped to a val definition - val inc = Command.IncrementBy(2) // `inc` is NOT a case class + val reset = Command.Reset // `reset` is a singleton case mapped to a val definition + val inc = Command.IncrementBy(2) // `inc` is NOT a case class val r1 = Command.Reset val r2 = Command.Reset - println(r1 eq r2) // will print `true` + println(r1 eq r2) // will print `true` println(s"reset.ordinal = ${reset.ordinal}") // will print reset.ordinal = 0 println(s"inc.ordinal = ${inc.ordinal}") } @main def enumMemberVisibility = { - + enum Command { case Reset case IncrementBy(inc: Int) case Configure(init: Int, inc: Int) } - val inc = new Command.IncrementBy(2) // `inc` IS a case class - val conf = new Command.Configure(0, 5) // `conf` IS a case class + val inc = new Command.IncrementBy(2) // `inc` IS a case class + val conf = new Command.Configure(0, 5) // `conf` IS a case class println(s"inc.ordinal = ${inc.ordinal}") - println(s"conf = $conf") // will print Configure(0, 5) + println(s"conf = $conf") // will print Configure(0, 5) val conf1 = conf.copy(inc = 7) - println(s"conf1 = $conf1") // will print Configure(0, 7) - conf1 // which members are defined on this enum instance? + println(s"conf1 = $conf1") // will print Configure(0, 7) + conf1 // which members are defined on this enum instance? } diff --git a/code-snippets/enumerations/src/main/scala/org/lunatech/dotty/enumeration/EnumerationDefinitions.scala b/code-snippets/enumerations/src/main/scala/org/lunatech/dotty/enumeration/EnumerationDefinitions.scala index d201fb095..b1ae12f44 100644 --- a/code-snippets/enumerations/src/main/scala/org/lunatech/dotty/enumeration/EnumerationDefinitions.scala +++ b/code-snippets/enumerations/src/main/scala/org/lunatech/dotty/enumeration/EnumerationDefinitions.scala @@ -9,7 +9,7 @@ object Enum { @main def printInfo(): Unit = { val red = Color.Red println(s"""Ordinal of Red = ${red.ordinal}""") - + // Get all values in enumeration val enums: Array[Color] = Color.values println(s"Values in enumeration Color: ${enums.mkString("Array(", ", ", ")")}") @@ -24,24 +24,24 @@ object Enum { object ParametrizedEnum { enum Color(val rgb: Int) { - case Red extends Color(0xFF0000) - case Green extends Color(0x00FF00) - case Blue extends Color(0x0000FF) + case Red extends Color(0xff0000) + case Green extends Color(0x00ff00) + case Blue extends Color(0x0000ff) } } enum Planet(mass: Double, radius: Double) { - private final val G = 6.67300E-11 + private final val G = 6.67300e-11 def surfaceGravity = G * mass / (radius * radius) - def surfaceWeight(otherMass: Double) = otherMass * surfaceGravity + def surfaceWeight(otherMass: Double) = otherMass * surfaceGravity case Mercury extends Planet(3.303e+23, 2.4397e6) - case Venus extends Planet(4.869e+24, 6.0518e6) - case Earth extends Planet(5.976e+24, 6.37814e6) - case Mars extends Planet(6.421e+23, 3.3972e6) - case Jupiter extends Planet(1.9e+27, 7.1492e7) - case Saturn extends Planet(5.688e+26, 6.0268e7) - case Uranus extends Planet(8.686e+25, 2.5559e7) + case Venus extends Planet(4.869e+24, 6.0518e6) + case Earth extends Planet(5.976e+24, 6.37814e6) + case Mars extends Planet(6.421e+23, 3.3972e6) + case Jupiter extends Planet(1.9e+27, 7.1492e7) + case Saturn extends Planet(5.688e+26, 6.0268e7) + case Uranus extends Planet(8.686e+25, 2.5559e7) case Neptune extends Planet(1.024e+26, 2.4746e7) } diff --git a/code-snippets/export-clause/src/main/scala/org/lunatech/dotty/exportclause/A.scala b/code-snippets/export-clause/src/main/scala/org/lunatech/dotty/exportclause/A.scala index df602517f..398517d07 100644 --- a/code-snippets/export-clause/src/main/scala/org/lunatech/dotty/exportclause/A.scala +++ b/code-snippets/export-clause/src/main/scala/org/lunatech/dotty/exportclause/A.scala @@ -8,3 +8,7 @@ object A { } export B.C.x } + +@main def test: Unit = + import A._ + println(x) diff --git a/code-snippets/export-clause/src/main/scala/org/lunatech/dotty/exportclause/Ovens.scala b/code-snippets/export-clause/src/main/scala/org/lunatech/dotty/exportclause/Ovens.scala index dbe4685d0..026576037 100644 --- a/code-snippets/export-clause/src/main/scala/org/lunatech/dotty/exportclause/Ovens.scala +++ b/code-snippets/export-clause/src/main/scala/org/lunatech/dotty/exportclause/Ovens.scala @@ -4,7 +4,7 @@ object Ovens { private class HotAirOven { var on = false var tempSetting = 180 - def setTemp(temp: Int): Unit = {tempSetting = temp; println(s"Setting temperature to $temp")} + def setTemp(temp: Int): Unit = { tempSetting = temp; println(s"Setting temperature to $temp") } def turnOn: Unit = on = true def turnOff: Unit = on = false def status: String = s"Hot-air oven is ${if (on) "on" else "off"} - Temperature setting at $tempSetting" @@ -13,7 +13,7 @@ object Ovens { private class MicrowaveOven { var on = false var powerSetting = 600 - def setPower(watts: Int): Unit = {powerSetting = watts; println(s"Setting power to $powerSetting")} + def setPower(watts: Int): Unit = { powerSetting = watts; println(s"Setting power to $powerSetting") } def turnOn: Unit = on = true def turnOff: Unit = on = false def status: String = s"Microwave oven is ${if (on) "on" else "off"} - Power setting at $powerSetting Watt" @@ -28,3 +28,19 @@ object Ovens { export microwaveOven.{setPower, turnOn => microwaveOn, turnOff => microwaveOff} } } + +@main def overRunner(): Unit = + import Ovens.CombiOven + + val combiOven = CombiOven() + + println(combiOven.status) + combiOven.microwaveOn + println(combiOven.status) + combiOven.setPower(350) + combiOven.hotAirOn + println(combiOven.status) + combiOven.hotAirOff + combiOven.microwaveOff + combiOven.setTemp(250) + println(combiOven.status) diff --git a/code-snippets/intersection-and-union-types/src/main/scala/org/lunatech/dotty/intersectionanduniontypes/IntersectionTypes.scala b/code-snippets/intersection-and-union-types/src/main/scala/org/lunatech/dotty/intersectionanduniontypes/IntersectionTypes.scala index 2e311666d..151501d82 100644 --- a/code-snippets/intersection-and-union-types/src/main/scala/org/lunatech/dotty/intersectionanduniontypes/IntersectionTypes.scala +++ b/code-snippets/intersection-and-union-types/src/main/scala/org/lunatech/dotty/intersectionanduniontypes/IntersectionTypes.scala @@ -1,8 +1,8 @@ package org.lunatech.dotty.intersectionanduniontypes -type S // Some type S -type T // Some type T -type ST = S & T // Intersection Type ST which has all the properties of both S & T +type S // Some type S +type T // Some type T +type ST = S & T // Intersection Type ST which has all the properties of both S & T @main def inter() = { trait Growable { @@ -20,8 +20,8 @@ type ST = S & T // Intersection Type ST which has all the properties of both S } def resizeAndPaint(obj: Growable & Paintable): Unit = { - obj.growBy(20).paint(0x10FF00).growBy(40).paint(0x0010FF) + obj.growBy(20).paint(0x10ff00).growBy(40).paint(0x0010ff) } resizeAndPaint(new Growable with Paintable) -} \ No newline at end of file +} diff --git a/code-snippets/intersection-and-union-types/src/main/scala/org/lunatech/dotty/intersectionanduniontypes/Matchable.scala b/code-snippets/intersection-and-union-types/src/main/scala/org/lunatech/dotty/intersectionanduniontypes/Matchable.scala new file mode 100644 index 000000000..13a14ea31 --- /dev/null +++ b/code-snippets/intersection-and-union-types/src/main/scala/org/lunatech/dotty/intersectionanduniontypes/Matchable.scala @@ -0,0 +1,12 @@ +package org.lunatech.dotty.intersectionanduniontypes + +object Matchable { + val iar: IArray[Int] = IArray(1, 2) + + iar.mkString("IArray(", ",", ")") + + iar match + case a: Array[Int] => a(0) = 7 + + iar.mkString("IArray(", ",", ")") +} diff --git a/code-snippets/intersection-and-union-types/src/main/scala/org/lunatech/dotty/intersectionanduniontypes/UnionTypes.scala b/code-snippets/intersection-and-union-types/src/main/scala/org/lunatech/dotty/intersectionanduniontypes/UnionTypes.scala index b9361aa69..cb1dd09e4 100644 --- a/code-snippets/intersection-and-union-types/src/main/scala/org/lunatech/dotty/intersectionanduniontypes/UnionTypes.scala +++ b/code-snippets/intersection-and-union-types/src/main/scala/org/lunatech/dotty/intersectionanduniontypes/UnionTypes.scala @@ -18,19 +18,19 @@ object UnionTypesExample { @main def unionTypes() = { // implicitly[Behavior[CommandAndResponse] <:< Behavior[Command]] - val internalBehavior: Behavior[CommandAndResponse] = new Behavior[CommandAndResponse]{} - val externalBehavior: Behavior[Command] = internalBehavior // Contravariance at work + val internalBehavior: Behavior[CommandAndResponse] = new Behavior[CommandAndResponse] {} + val externalBehavior: Behavior[Command] = internalBehavior // Contravariance at work // Handling of Command and Responses by internal behavior internalBehavior.treatMsg(Command.Reset) internalBehavior.treatMsg(Command.Run(5)) internalBehavior.treatMsg(Response.RunFailed("Too much to do")) internalBehavior.treatMsg(Response.RunFinished) - + externalBehavior.treatMsg(Command.Reset) externalBehavior.treatMsg(Command.Run(110)) // The following doesn't compile - //externalBehavior.treatMsg(Response.RunFailed("Too much to do")) + // externalBehavior.treatMsg(Response.RunFailed("Too much to do")) + // externalBehavior.treatMsg(Response.RunFinished) } } - diff --git a/code-snippets/intersection-and-union-types/src/main/scala/org/lunatech/dotty/intersectionanduniontypes/UnionTypesSimple.scala b/code-snippets/intersection-and-union-types/src/main/scala/org/lunatech/dotty/intersectionanduniontypes/UnionTypesSimple.scala index 1a3910b11..558b763fd 100644 --- a/code-snippets/intersection-and-union-types/src/main/scala/org/lunatech/dotty/intersectionanduniontypes/UnionTypesSimple.scala +++ b/code-snippets/intersection-and-union-types/src/main/scala/org/lunatech/dotty/intersectionanduniontypes/UnionTypesSimple.scala @@ -8,9 +8,10 @@ enum ToolSupplies { case Nail(size: Int) case Screw(size: Int) } -def printIt(t: Tools | ToolSupplies): Unit = { t match - case tool: Tools => println(s"Got a tool: $tool") - case supply: ToolSupplies => println(s"Got a supply: $supply") +def printIt(t: Tools | ToolSupplies): Unit = { + t match + case tool: Tools => println(s"Got a tool: $tool") + case supply: ToolSupplies => println(s"Got a supply: $supply") } @main def simpleUnionTypes: Unit = { @@ -19,4 +20,4 @@ def printIt(t: Tools | ToolSupplies): Unit = { t match printIt(Hammer(6)) printIt(Nail(9)) -} \ No newline at end of file +} diff --git a/code-snippets/new-control-structure-syntax/build.sbt b/code-snippets/new-control-structure-syntax/build.sbt deleted file mode 100644 index 942186565..000000000 --- a/code-snippets/new-control-structure-syntax/build.sbt +++ /dev/null @@ -1,5 +0,0 @@ -scalacOptions ++= - Seq( - "-old-syntax", - "-rewrite" - ) diff --git a/code-snippets/new-control-structure-syntax/src/main/scala/org/lunatech/dotty/NewControlStructures.scala b/code-snippets/new-control-structure-syntax/src/main/scala/org/lunatech/dotty/NewControlStructures.scala index 7ed60db4f..63fa1b4f3 100644 --- a/code-snippets/new-control-structure-syntax/src/main/scala/org/lunatech/dotty/NewControlStructures.scala +++ b/code-snippets/new-control-structure-syntax/src/main/scala/org/lunatech/dotty/NewControlStructures.scala @@ -1,27 +1,31 @@ package org.lunatech.dotty object NewControlStructures { - var x = 5 - def f(x: Int) = x * x + 5 - val xs = List.range(1,4) - val ys = Vector.range(3,7) + var x = -6 + def f(x: Int): Int = x + 1 + val xs = Seq(1, 2, 3) + val ys = Seq(3, 4, 5) - if (x < 0) "negative" - else if (x == 0) - "zero" - else - "positive" + def body: Int = 6 - if (x < 0) -x else x + if x < 0 then "negative" + else if x == 0 then "zero" + else "positive" - while (x >= 0) x = f(x) + if x < 0 then -x else x - for (x <- xs if x > 0) - yield x * x + while x <= 0 do x = f(x) - for { + println(x) + + for x <- xs if x > 0 + yield x * x + + for x <- xs y <- ys - } println(x + y) - + do println(x + y) + + // try body + // catch case ex: IOException => handle } diff --git a/code-snippets/new-control-structure-syntax/src/main/scala/org/lunatech/dotty/NewControlStructures.worksheet.sc b/code-snippets/new-control-structure-syntax/src/main/scala/org/lunatech/dotty/NewControlStructures.worksheet.sc new file mode 100644 index 000000000..4018bcf49 --- /dev/null +++ b/code-snippets/new-control-structure-syntax/src/main/scala/org/lunatech/dotty/NewControlStructures.worksheet.sc @@ -0,0 +1,28 @@ +var x = -6 +def f(x: Int): Int = x + 1 +val xs = Seq(1,2,3) +val ys = Seq(3,4,5) + +def body: Int = 6 + +if x < 0 then + "negative" +else if x == 0 then + "zero" +else + "positive" + +if x < 0 then -x else x + +while x <= 0 do x = f(x) + +println(x) + +for x <- xs if x > 0 +yield x * x + +for + x <- xs + y <- ys +do + println(x + y) diff --git a/code-snippets/project/plugins.sbt b/code-snippets/project/plugins.sbt new file mode 100644 index 000000000..e7cf946d4 --- /dev/null +++ b/code-snippets/project/plugins.sbt @@ -0,0 +1 @@ +addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.0") diff --git a/code-snippets/src/main/scala/org/lunatech/dotty/toplevel/TemperatureConverter.scala b/code-snippets/src/main/scala/org/lunatech/dotty/toplevel/TemperatureConverter.scala index 113e297c1..9106137fd 100644 --- a/code-snippets/src/main/scala/org/lunatech/dotty/toplevel/TemperatureConverter.scala +++ b/code-snippets/src/main/scala/org/lunatech/dotty/toplevel/TemperatureConverter.scala @@ -5,7 +5,7 @@ object TemperatureConverter extends App { val bodyTempCelcius = Temperature(37, Celsius) val bodyTempFaren = convertToFarenheit(bodyTempCelcius) - + println(bodyTempCelcius) println(bodyTempFaren) -} \ No newline at end of file +} diff --git a/code-snippets/src/main/scala/org/lunatech/dotty/toplevel/TemperatureOps.scala b/code-snippets/src/main/scala/org/lunatech/dotty/toplevel/TemperatureOps.scala index 1447a1452..f333ad2c9 100644 --- a/code-snippets/src/main/scala/org/lunatech/dotty/toplevel/TemperatureOps.scala +++ b/code-snippets/src/main/scala/org/lunatech/dotty/toplevel/TemperatureOps.scala @@ -18,15 +18,9 @@ case class Temperature(value: Double, scale: TemperatureScale) { // A private toplevel definition is always visible from everywhere in the enclosing package. private def convertToFarenheit(temperature: Temperature): Temperature = { - Temperature( - (temperature.value * 9/5) + 32, - Farenheit - ) + Temperature((temperature.value * 9 / 5) + 32, Farenheit) } def convertToCelcius(temperature: Temperature): Temperature = { - Temperature( - (temperature.value - 32) * 5/9, - Celsius - ) -} \ No newline at end of file + Temperature((temperature.value - 32) * 5 / 9, Celsius) +}