{X со значением} в ocaml

Чтобы получить синтаксис, о котором вы на самом деле спрашиваете, вы можете легко определить новый оператор, который обернет функциональность NSString моста:

infix operator =~ {}
func =~(string:String, regex:String) -> Bool {
    return string.rangeOfString(regex, options: .RegularExpressionSearch) != nil
}

"abcd" =~ "ab*cd"
"abcd" =~ "abcde+"
28
задан GEOCHET 1 June 2009 в 16:11
поделиться

1 ответ

This is sometimes called a "record update" or "functional update" or something like that. It evaluates to a new record of the same type as X, and whose fields are initialized to the same as those in X, except the ones which are listed after the "with", which are initialized to those given values.

It is useful for immutable records, as a convenient way to take such a record and change one or two things on it (which in an imperative language you would typically mutate the fields), without having to list out all the fields that are not changed.

It is described in the OCaml manual section 6.7.3, scroll down to "Records", second paragraph.

For those who are familiar with Haskell, the OCaml syntax

{ expr with field1 = expr1 ; ... ;  fieldn = exprn }

is the same as the Haskell syntax

expr { field1 = expr1 , ... ,  fieldn = exprn }
40
ответ дан 28 November 2019 в 03:06
поделиться
Другие вопросы по тегам:

Похожие вопросы: