자바 와 스칼라 의 패키지 닫 기
원문 주소: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 -- , 。
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JPA + QueryDSL 계층형 댓글, 대댓글 구현(2)이번엔 전편에 이어서 계층형 댓글, 대댓글을 다시 리팩토링해볼 예정이다. 이전 게시글에서는 계층형 댓글, 대댓글을 구현은 되었지만 N+1 문제가 있었다. 이번에는 그 N+1 문제를 해결해 볼 것이다. 위의 로직은 이...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.