Scala 类型列表采用类型参数(编译错误在起作用)

Scala 类型列表采用类型参数(编译错误在起作用),scala,playframework,Scala,Playframework,我目前正在学习RESTfulAPI的基础知识,并遇到了一些问题:我在学习一些长期的教程,认为我在正确的scala语法方面失败了!需要帮助,谢谢 你的代码有很多问题。您正在定义位置,同时调用定义右侧的位置本身 此外,您的命名也很混乱 试试这个: final case class Place(id: Int, name: String) object Place { implicit val placeWrites = Json.writes[Place] } class PlacesCo

我目前正在学习RESTfulAPI的基础知识,并遇到了一些问题:我在学习一些长期的教程,认为我在正确的scala语法方面失败了!需要帮助,谢谢


你的代码有很多问题。您正在定义位置,同时调用定义右侧的位置本身

此外,您的命名也很混乱

试试这个:

final case class Place(id: Int, name: String)

object Place {
  implicit val placeWrites = Json.writes[Place]
}

class PlacesController ... {

  val thePlaces: List[Place] = List(
    Place(1, "newyork"),
    Place(2, "chicago"),
    Place(3, "capetown")
  )

  def listPlaces = Action {
    val json = Json.toJson(thePlaces)
    Ok(json)
  }
}

终于得到了答案,希望这能对以后的其他人有所帮助

class PlacesController @Inject()(cc: ControllerComponents)(implicit assetsFinder: AssetsFinder, ec: ExecutionContext, configuration: Configuration)
  extends AbstractController(cc) {


  case class PlacesController(id: Int, name: String)

  val thePlaces: List[(Int, String)] = List(
    (1, "newyork"),
    (2, "chicago"),
    (3, "capetown")
  )

  implicit val thePlacesWrites = Json.writes[PlacesController]

  def listPlaces = Action {
    val json = Json.toJson(thePlaces)
    Ok(json)
  }


}

valtheplaces:List
(+shadowing
thePlaces
name)。强烈建议首先阅读和一些Scala教程。此外,当您遇到编译错误时,您应该在问题中包含错误。已链接错误屏幕快照粘贴错误,无错误屏幕快照
class PlacesController @Inject()(cc: ControllerComponents)(implicit assetsFinder: AssetsFinder, ec: ExecutionContext, configuration: Configuration)
  extends AbstractController(cc) {


  case class PlacesController(id: Int, name: String)

  val thePlaces: List[(Int, String)] = List(
    (1, "newyork"),
    (2, "chicago"),
    (3, "capetown")
  )

  implicit val thePlacesWrites = Json.writes[PlacesController]

  def listPlaces = Action {
    val json = Json.toJson(thePlaces)
    Ok(json)
  }


}