F#을 .NET CLI로 시작해 봐요
F#을 .NET CLI로 시작해 봐요 (Command Line)
F# 공식 문서의 「Get started with F# with the .NET CLI」를 번역한 내용이에요. 이 문서는 운영체제(Windows, macOS, Linux)와 무관하게 .NET CLI만으로 F#을 시작하는 방법을 다뤄요. 콘솔 애플리케이션이 호출하는 클래스 라이브러리를 포함한 다중 프로젝트 솔루션을 처음부터 끝까지 만들어 보면서, F# 프로젝트 구조와 dotnet 명령어 사용법을 자연스럽게 익힐 수 있어요.
출처: Get started with F# with the .NET CLI - Microsoft Learn
본문
소개 (In this article)
이 문서는 어떤 운영체제(Windows, macOS, Linux)에서든 .NET CLI로 F#을 시작하는 방법을 다뤄요. 콘솔 애플리케이션이 호출하는 클래스 라이브러리로 구성된 다중 프로젝트 솔루션을 만드는 과정을 따라가 볼 거예요.
사전 준비 (Prerequisites)
시작하려면 최신 .NET SDK를 설치해야 해요.
이 문서는 명령줄 사용법을 어느 정도 알고 있고, 선호하는 텍스트 편집기가 있다고 가정해요. 아직 편집기가 없다면 F#용 텍스트 편집기로 Visual Studio Code를 추천해요.
간단한 다중 프로젝트 솔루션 만들기 (Build a simple multi-project solution)
명령 프롬프트나 터미널을 열고 dotnet new 명령어로 FSharpSample이라는 솔루션 파일을 만들어 봐요:
dotnet new sln -o FSharpSample
위 명령어를 실행하면 다음과 같은 디렉터리 구조가 만들어져요:
FSharpSample
├── FSharpSample.sln
클래스 라이브러리 작성 (Write a class library)
FSharpSample 디렉터리로 이동해요.
dotnet new 명령어로 src 폴더 안에 Library라는 이름의 클래스 라이브러리 프로젝트를 만들어요.
dotnet new classlib -lang "F#" -o src/Library
위 명령어를 실행하면 다음과 같은 디렉터리 구조가 만들어져요:
└── FSharpSample
├── FSharpSample.sln
└── src
└── Library
├── Library.fs
└── Library.fsproj
Library.fs의 내용을 다음 코드로 바꿔요:
module Library
open System.Text.Json
let getJson value =
let json = JsonSerializer.Serialize(value)
value, json
dotnet sln add 명령어로 Library 프로젝트를 FSharpSample 솔루션에 추가해요. 이 명령어는 프로젝트를 솔루션 파일에 등록해서 솔루션이 해당 프로젝트를 추적하고 빌드할 수 있게 해줘요:
dotnet sln add src/Library/Library.fsproj
dotnet build를 실행해서 프로젝트를 빌드해요. 빌드 과정에서 해결되지 않은 종속성이 있으면 자동으로 복원돼요.
클래스 라이브러리를 사용하는 콘솔 애플리케이션 작성 (Write a console application that consumes the class library)
dotnet new 명령어로 src 폴더 안에 App이라는 이름의 콘솔 애플리케이션을 만들어요.
dotnet new console -lang "F#" -o src/App
위 명령어를 실행하면 다음과 같은 디렉터리 구조가 만들어져요:
└── FSharpSample
├── FSharpSample.sln
└── src
├── App
│ ├── App.fsproj
│ ├── Program.fs
└── Library
├── Library.fs
└── Library.fsproj
Program.fs 파일의 내용을 다음 코드로 바꿔요:
open System
open Library
[<EntryPoint>]
let main args =
printfn "Nice command-line arguments! Here's what System.Text.Json has to say about them:"
let value, json = getJson {| args=args; year=System.DateTime.Now.Year |}
printfn $"Input: %0A{value}"
printfn $"Output: %s{json}"
0 // return an integer exit code
dotnet reference add 명령어로 Library 프로젝트에 대한 참조를 추가해요. 이 명령어는 App.fsproj 파일에 <ProjectReference> 요소를 추가해서, 컴파일러가 App 프로젝트가 Library 프로젝트에 의존한다는 사실을 알 수 있게 해줘요:
dotnet add src/App/App.fsproj reference src/Library/Library.fsproj
위 명령어는 App.fsproj 파일에 다음 XML을 추가해요:
<ItemGroup>
<ProjectReference Include="..\Library\Library.fsproj" />
</ItemGroup>
팁
이 단계를 건너뛰고 App 프로젝트를 빌드하려 하면
Library모듈을 찾을 수 없어서 컴파일 오류가 나요. 이런 경우dotnet add reference명령어를 실행하거나, 위에서 본<ProjectReference>요소를 App.fsproj 파일에 직접 추가하면 해결할 수 있어요.
dotnet sln add 명령어로 App 프로젝트를 FSharpSample 솔루션에 추가해요:
dotnet sln add src/App/App.fsproj
dotnet restore로 NuGet 종속성을 복원하고 dotnet build를 실행해서 프로젝트를 빌드해요.
src/App 콘솔 프로젝트로 디렉터리를 바꾼 뒤 Hello World를 인자로 넘겨 프로젝트를 실행해요:
cd src/App
dotnet run Hello World
다음과 같은 결과가 보일 거예요:
Nice command-line arguments! Here's what System.Text.Json has to say about them:
Input: { args = [|"Hello"; "World"|] year = 2021 }
Output: {"args":["Hello","World"],"year":2021}
다음 단계 (Next steps)
F#의 다양한 기능을 더 배우고 싶다면 F# 둘러보기 문서를 확인해 보세요.