Re[2]: [F#] Unit type
От: desco США http://v2matveev.blogspot.com
Дата: 21.07.09 22:33
Оценка: 10 (1)
Здравствуйте, samius, Вы писали:


S>Т.е. void интерпретируется как и unit.


S>Но не совсем:

S>
>> let f1 () = ()
S>- let f2 () = ()
S>- let f3 = f1 >> f2;;

S>val f1 : unit -> unit
S>val f2 : unit -> unit
S>val f3 : (unit -> unit)
S>

S>объявленные так функции мы можем комбинировать.

S>и так тоже:

S>
>> let f4 = f1 >> ignore;;  

S>val f4 : (unit -> unit)
S>


S>а так уже нельзя:

S>
>> let f5 = ignore >> f1;; 

S>  let f5 = ignore >> f1;;
S>  ----^^

S>stdin(15,5): error FS0030: Value restriction. The value 'f5' has been inferred to have generic type
S>        val f5 : ('_a -> unit)
S>Either make the arguments to 'f5' explicit or, if you do not intend for it to be generic, add a type annotation.
>>
S>


Value restriction не имеет отношения к unit. Automatic generalization применяется к функциям и простым типам данных. Функции, определенные как значения автоматически не обобщаются.

> let a x = 5;;

val a : 'a -> int

> let b x = x + 1;;

val b : int -> int

> let c = a >> b;;

  let c = a >> b;;
  ----^

stdin(22,5): error FS0030: Value restriction. The value 'c' has been inferred to have generic type
    val c : ('_a -> int)
Either make the arguments to 'c' explicit or, if you do not intend for it to be generic, add a type annotation.


простой фикс — явно задать аргументы
> let c x = (a >> b) x;;

val c : 'a -> int
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.