어떤 값이 존재하거나 존재하지 않는 경우라는, 아주 유용한 개념을 표현하는 타입 세 가지를
논의해보자.
대부분의 언어에는 참조할 대상이 따로 없는 참조 변수에 대입할 수 있는 특별한 키워드나 타
입 인스턴스가 있다. 자바에서 그것은 어떤 타입의 인스턴스가 아닌 키워드
null
이다. 따라서
null
에 관해 메서드를 호출하는 것은 규칙에 어긋난다. 하지만 이것은 언어 설계자의 관점에
서 볼 때 혼란스러운 결정이다. 프로그래머가 어떤 타입의 인스턴스를 원하는데, 왜 키워드를
반환해야 할까?
물론 실제 문제는
null
이 형편없는 버그를 자주 발생시키는 근원이라는 점에 있다.
null
이 실
제로 보내는 신호는 그 상황에서 우리가 가진 값이 없다는 것이다. 만약 그 값이
null
이 아니라
면, 우리가 사용할 값이 있는 것이다. 이런 상황을 타입 시스템을 사용해서 더 명백히 표현하고,
NullPointerException
(
http
://
bit
.
ly
/
1pbzh3C
)을 피하기 위해 타입 검사를 잘 활용할 수
는 없을까?
Option
(
http
://
bit
.
ly
/
1wl8KBy
)을 사용하면
null
을 사용한 편법을 사용하지 않고도 이런 상
황을 명확히 표현할 수 있다.
Option
은 추상 클래스며, ...
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.