자바 와 스칼라 의 패키지 닫 기

2633 단어 JavaScalajava7SQLJDK
더 읽 기
  
원문 주소:Closures in Java and Scala
  
Eastsun
  
People argue that verbose code is easier to understand. Do you agree when reading these two examples, one method in Java, one in Scala?
사람들 은 일반적으로 상세 한 코드 가 이해 하기 쉽다 고 생각한다.하지만 다음 두 단락 의 코드 를 읽 으 면 하 나 는 자바 를 사용 하고 다른 하 나 는 스칼라 를 사용 합 니 다.-아직도 그렇게 생각 하 십 니까?
public List bought(User user) {
    List result = new ArrayList();
    for (Item item : currentItems) {
        if (user.bought(item)) {
            result.add(item);
        }
    }
    return result;
}
def bought(user: User) = items.filter(user bought _)

  
If you are familiar with Java, which is more likely then you being familiar with Scala, you may tend to the Java-version. Scala has a different syntax for declaring types and generics, and supports closures, including anonymous parameters (the underscore).
자바 에 익숙 하 다 면 스칼라 를 알 게 될 것 이다.자바 버 전 을 사용 하 는 경향 이 있 을 지도 모른다.Scala 는 서로 다른 유형의 성명 과 일반적인 문법 을 가지 고 있 으 며 패 킷 폐쇄 와 익명 매개 변수(밑줄 긋 기)를 지원 합 니 다.
  
When Closures are introduced in one of the next Java releases, JDK 7 or 8, the example could be rewritten like this:
자바 의 후속 버 전에 서 패키지 닫 기 를 도입 한 후(JDK 7 또는 JDK 8)위의 자바 버 전의 코드 는 다음 과 같이 다시 쓸 수 있 습 니 다.
public List bought(User user) {   
    return ListUtils.filter(Item item : items ) {      
        user.bought(item);    
    }
}

  Or with extension methods:
또는 사용 방법 확장:
public List bought(User user) { 
    return items.filter(Item item : )  { 
        //  
 
   The interesting differences between Java with closures and Scala is the static typing: Scala inferences that items are all of type Item, so there is no need to specify that information again.

   Java Scala :Scala , 。

   So, while the current Java Closures specification is a great step in the right direction, lead by Neil Gafter, who is quite the right man for the job, it may not be worth to wait for it, if you have the choice. Its not even sure yet that we'll see that spec implemented in JDK7, and even that is at best one and a half year away.
   , JAVA Neil Gafter ; , 。 JDK7 -- , 。

좋은 웹페이지 즐겨찾기