Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
8
Help us understand the problem. What is going on with this article?
@bububu

Scalaの小ネタ: 「これ知ってるのはあまりいないでしょ」っていうimportの書き方見つけた

More than 5 years have passed since last update.

結論から言うと、importに変数が使えるだけのこと。
型名に変数が使えるのは知ってたけど、これはついさっき知った。

val list = List(1, 2, 3)
import list._
foreach(println)
val x = map(_ * 2)
mkString("-")

知ったきっかけは、PlayでSeleniumを使ったテストを書いてたとき。

// コードはPlayのsamplesから拝借
running(...) { browser =>
  browser.$("#name").text("Bob")
  browser.$("#submit").click()

  browser.$("dl.error").size must equalTo(1)
  browser.$("dl#repeat_field dd.error").first.getText must equalTo("Numeric value expected")
  browser.$("#name").first.getValue must equalTo("Bob")

  browser.$("#repeat").text("xxx")
  browser.$("#submit").click()
}

いちいちbrowserと打つのが面倒臭かったので、メソッド名だけで書けないかと思い、変数名でimportしてみたら通ってむっちゃ驚いた。これでテストコードは、jQueryライクの素敵なコードへと変身を遂げた。

running(...) { browser =>
  import browser._
  $("#name").text("Bob")
  $("#submit").click()

  $("dl.error").size must equalTo(1)
  $("dl#repeat_field dd.error").first.getText must equalTo("Numeric value expected")
  $("#name").first.getValue must equalTo("Bob")

  $("#repeat").text("xxx")
  $("#submit").click()
}
8
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
bububu

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
8
Help us understand the problem. What is going on with this article?