터와 세터를 통해 객체 안의 필드를 노출시키게 된 것이다. 이들은 종종 상태는 캡슐화되어야
하고 적절한 방식으로 노출되어야 한다는(특히 변경 가능한 필드의 경우 더 그래야 한다) 주의
사항을 무시한다. 상태 정보에 대한 접근은 노출시킨 추상화를 반영하도록 주의 깊게 설계되어
야 한다.
정보를 정제된 방식으로 추출해야 하는 경우 패턴 매칭을 고려하라.
4
.
6
.
1
절 ‘
unapply
메서
드’에서 본 것처럼, 노출시키는 상태를 제어하기 위해
unapply
메서드를 원하는 대로 바꿀 수
있다. 이 메서드를 사용하면 자세한 구현은 숨기면서 정보를 외부에 노출시킬 수 있다. 실제로
unapply
가 반환하는 정보가 해당 타입의 필드가 아니고 필드를 변환한 것일 수도 있다.
패턴 매치 문을 설계하는 경우 기본
case
절에 의존하는 것을 조심할 필요가 있다. 어떤 경우에
‘위의 모든
case
문에 해당하지 않는 경우’가 정상적인 상황이 될 수 있을까? 그런 절을 사용한
다는 것은 어쩌면 발생 가능한 모든 경우를 고려할 수 있도록 좀 더 엄밀하게 설계를 상세화해
야 한다는 것을 의미할 수도 있다.
for
내장과 함께 패턴 매칭은 자주 사용하는 스칼라 코드를 간결하면서 강력하게 만들어준다.
스칼라 프로그래머가
1
/
10
정도의 줄 수로 자바로 만든 프로그램과 비슷한 기능을 작성하는
경우도 드물지 않다.
따라서
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.
O’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
I wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
I’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
I'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.