Чтобы не писать полное имя, в F# можно воспользоваться open:
open System
let greeting = String.Format("Hello {0}", "world")
Это работает и в обычном скомпилированном приложении, и в F# interactive (сначала введите объявление open). Здесь важно писать String с заглавной S. Причина в том, что в C# string — не обычное имя типа, а ключевое слово, обозначающее System.String.
Другой вариант, который стоит рассмотреть, — sprintf. Эта специфичная для F# альтернатива String.Format даёт полезные преимущества, например проверку типов:
let greeting = sprintf "Hello %s! Number is %d" "world" 56
Компилятор проверяет, соответствуют ли аргументы (string и int) указателям формата (%s означает строку, а %d — целые числа). Частичное применение функции — ещё одна ситуация, где эта функция удобнее:
let values = [ 3 .. 12 ]
let descriptions = values |> List.map (sprintf "number %d")
Получится список строк с "number 3", "number 4" и так далее... Чтобы добиться того же с String.Format, пришлось бы явно написать лямбда-функцию.
Переведено с английского·Показать оригиналTo skip spelling out the fully qualified name, F# lets you use open:
open System
let greeting = String.Format("Hello {0}", "world")
That works in an ordinary compiled application as well as F# interactive (submit the open declaration beforehand). What's important here is spelling String with a capital S. The reson is that C#'s string isn't an ordinary name for a type - it's a keyword referring to System.String.
Another option worth examining is sprintf. This F# alternative to String.Format offers useful advantages - one being that it is checked for correct types:
let greeting = sprintf "Hello %s! Number is %d" "world" 56
The compiler verifies that the arguments (string and int) agree with their format markers (%s means string, while %d means integers). Partial function application is another situation in which this function fits more naturally:
let values = [ 3 .. 12 ]
let descriptions = values |> List.map (sprintf "number %d")
The result is a list of strings holding "number 3", "number 4" and so on... Achieving the equivalent with String.Format would require writing out a lambda function.