대부분의 경우 스칼라의 정적 타입 지정은 프로그래머에게 이롭다. 정적 타입은 실행 시점에 정
확성을 보장할 수 있고, 코드를 볼 때 이해를 더 쉽게 해주는 안전한 제약조건을 부가해준다. 이
런 이점은 특별히 대규모 시스템에서 유용하다.
하지만 때때로 동적 타입 지정의 이점이 그리운 경우가 있다. 예를 들어 컴파일 시점에는 존재하
지 않던 메서드를 호출하는 경우를 들 수 있다! 명성이 자자한 루비 온 레일즈
Ruby
on
Rails
웹 프레
임워크(
http
://
rubyonrails
.
org
)는
ActiveRecord
에서 이런 기법을 매우 효과적으로 사용한
다. 스칼라에서는 이런 기법을 어떻게 구현할 수 있는지 살펴보자.
19.1
동기를 불어넣는 예제: 루비 온 레일즈의
ActiveRecord
ActiveRecord
는 원래 레일즈에 통합되어 있던 객체
-
관계 매핑
object
-
relational
mapping
(
ORM
) 라
이브러리였다. 여기서 대부분의 자세한 사항은 우리에게 중요하지 않다.
1
그러나 그것이 제공하
는 가장 중요한 기능 중 하나는 도메인 객체에 대한 메서드 호출을 연쇄시킴으로써 질의를 조합
할 수 있는
DSL
을 제공한다는 것이다.
하지만 이런 ‘메서드’가 실제로 있는 것은 아니다. 대신 모든 관련 메서드 호출은 루비가 정의되
1
주_ 액티브레코드기본레일즈가이드 ...
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.