I'm trying to convert some C# code to F# and have encountered a slight problem. Here is the F# code that I have already:
open System open System.Collections open System.Collections.Generic type Chromosome<'GeneType>() = let mutable cost = 0 let mutable (genes : 'GeneType[]) = Array.zeroCreate<'GeneType> 0 let mutable (geneticAlgorithm : GeneticAlgorithm<'GeneType>) = new GeneticAlgorithm<'GeneType>() /// The genetic algorithm that this chromosome belongs to. member this.GA with get() = geneticAlgorithm and set(value) = geneticAlgorithm <- value /// The genes for this chromosome. member this.Genes with get() = genes and set(value) = genes <- value /// The cost for this chromosome. member this.Cost with get() = cost and set(value) = cost <- value /// Get the size of the gene array. member this.Size = genes.Length /// Get the specified gene. member this.GetGene(gene:int) = genes.[gene] member this.GeneNotTaken(source:Chromosome<'GeneType>, taken:IList<'GeneType>) = let geneLength = source.Size for i in 0 .. geneLength do let trial = source.GetGene(i) if(not (taken.Contains(trial))) then taken.Add(trial) trial Everything was going fine until I started on the Gene not taken method. Here is the C# code for that method (I also need help with returning the default type as well, but just didn't make it that far yet):
private GENE_TYPE GetNotTaken(Chromosome<GENE_TYPE> source, IList<GENE_TYPE> taken) { int geneLength = source.Size; for (int i = 0; i < geneLength; i++) { GENE_TYPE trial = source.GetGene(i); if (!taken.Contains(trial)) { taken.Add(trial); return trial; } } return default(GENE_TYPE); } Compiler errors I'm seeing include:
"The generic member 'GeneNotTaken' has been used at a non-uniform instantiation prior to this program point. Consider reordering the members so this member occurs first. Alternatively, specify the full type of the member explicitly, including argument types, return type and any additional generic parameters and constraints."
and
"This code is less generic than required by its annotations because the explicit type variable 'GeneType' could not be generalized. It was constrained to be 'unit'."
You would think the first error would be crystal clear, except as you can see I didn't use the GeneNotTaken member prior to that point, which is why I don't know what the problem is.
The second part of my question is how to add the return default('GeneType) at the end of the method.
If you have some other suggestions for improvement of my code in general, please feel free to share them.