Skip to content

Instantly share code, notes, and snippets.

@neetsdkasu
Last active March 21, 2017 01:21
Show Gist options
  • Save neetsdkasu/c366bd483d57976e88f7b32da4b1093d to your computer and use it in GitHub Desktop.
Save neetsdkasu/c366bd483d57976e88f7b32da4b1093d to your computer and use it in GitHub Desktop.
F#でファイル分けてコンパイル

概要

ファイルを

  • module1.fs
  • module2.fs
  • test.fs

の3つに分けてコンパイルする。
これらの結果がおま環なのかは不明。
検証不十分。

検証環境:

  • F# 2.0 Compiler build 4.0.40219.1
  • Windows7 Starter SP1
  • .NET Framework v4.0.30319(?) v4.5.51209(?)

方法1

fsc.exe --target:library --out:templib\module1.dll module1.fs
fsc.exe --target:library --out:templib\module2.dll module2.fs
fsc.exe --target:exe --out:bin\test.exe --lib:templib --reference:module1.dll --reference:module2.dll test.fs
  • 記述が冗長的になる。(依存するdllは1つ1つ--referenceで指定する必要がある)
  • dllのコードはexeにまとめられるらしい。(dllを削除しても動く) (動かない)
  • 何故か生成されたdllやexeにソースファイルのパスなどが含まれている。(謎)

方法2

fsc.exe --target:exe --out:bin\test.exe module1.fs module2.fs test.fs
  • 依存関係を自動解決してくれないので自分で依存順にファイルを列挙する必要がある。
  • 何故か生成されたexeは方法1のものよりサイズが大きい。(謎)
  • exeなどを生成する場合はエントリポイントを含むファイルを最後に置く必要がある。
  • エントリポイントを含まない全てのファイルは必ずモジュール名を定義する必要がある。

他に分かったこと

同じ名前のモジュールを複数のファイルに分けてコンパイルはできない模様(モジュールは1ファイル内に収める)
C#のpartialみたいなソースファイル分割は…出来るかどうかは調べてないのでよく分からない

// ファイル全体をモジュールにするにはは全てのコードの最初に書かなければダメらしい?(名前空間があるなら名前空間も含めて書く)
module hogehoge.module1
let fuga() =
printfn "FUGA!"
// トップレベル名前空間は全てのコードの最初に書かなければダメらしい?
// 名前空間下には直接、値や関数を定義できない(モジュールなどの下に書く必要がある)
namespace hogehoge
module module2 =
let piyo() =
printfn "PIYO!!"
// モジュールは静的メンバを持つCLRのクラスとして生成されるらしい
module Program
module Test1 =
let test1() =
printfn "Test1"
hogehoge.module1.fuga()
hogehoge.module2.piyo()
module Test2 =
open hogehoge
let test2() =
printfn "Test2"
module1.fuga()
module2.piyo()
module Test3 =
open hogehoge.module1
open hogehoge.module2
let test3() =
printfn "Test3"
fuga()
piyo()
module Test4 =
open hogehoge
open module1
open module2
let test4() =
printfn "Test4"
fuga()
piyo()
[<EntryPoint>]
let main _ =
Test1.test1()
Test2.test2()
Test3.test3()
Test4.test4()
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment