泛型和鑄造用在以下方式問題在F#
type Genome<'T> (listIn : List<'T>)=
member this.list = listIn
member this.GetValues() =
this.list
我遇到了一個問題,而寫另一個代碼段實施了基因組型。
type IMutation =
abstract member MutateMethod: Genome<'T> -> Genome<'T>
type DoubleGenomeMutation() =
member this.Mutate = (this :> IMutation).MutateMethod
interface IMutation with
member this.MutateMethod(genome: Genome<'T>) =
let randomIndex = MyRandom.Next(0, genome.list.Length)
printfn "%d" randomIndex
let newValue = MyRandom.NextDouble(Parameters.LowerBounds.Item(randomIndex), Parameters.UpperBounds.Item(randomIndex))
let (newGenomeList : List<'double>, _) =
List.foldBack (fun elem acc ->
match acc with
| (l, 0) -> (newValue::l, -1)
| (l, i) -> (elem::l, i-1)) (genome.GetValues()) ([], genome.GetValues().Length - randomIndex - 1)
new Genome<'T>(newGenomeList)
我發現不可能在這裏正確使用泛型機制。在
Error 2 Type mismatch. Expecting a
float list
but given a
List<'a>
The type 'float' does not match the type ''a'
引述的樣本效應線
| (l, i) -> (elem::l, i-1)) (genome.GetValues()) ([], genome.GetValues().Length - randomIndex - 1)
鑄造變量朝上或朝下ofter描述爲類似案件的解決方案 - 不幸的是我無法修復我的這樣的程序。我將不勝感激任何直接幫助以及任何有關所有問題根源的建議。
我打賭你有一個'System.Collections中開放.Generic'在頂部,並與列表類型產生混淆 –
可能根本不是。我沒有在任何地方使用System.Collections.Generic,只有不可變的F#列表。 –
如果將簽名從「列表<'a>」更改爲「列表」,會發生什麼情況? –