Devon Burriss
Devon Burriss

Reputation: 2532

The type seq<'a> is not compatible with the type Collections.Generic.IEnumerable<'a>

When using the following code in an fsx file I get the error The type seq<'a> is not compatible with the type Collections.Generic.IEnumerable<'a>.

module ReadOnly =
    let private asList<'a> (en:System.Collections.Generic.IEnumerable<'a>) : System.Collections.Generic.IList<'a> = 
        new System.Collections.Generic.List<'a>(en) :>  System.Collections.Generic.IList<'a>
    let private asReadOnly<'a> (en:System.Collections.Generic.IEnumerable<'a>) = 
        new System.Collections.ObjectModel.ReadOnlyCollection<'a>(asList(en))
    let ofSeq<'a> (ss: 'a seq) = asReadOnly<'a>(ss) // <-- ERROR IS HERE ON ARGUMENT 'ss'

When using the same code in a netcoreapp2.1 console application all is fine.

My paket.dependencies contains the following:

source https://www.nuget.org/api/v2
nuget NETStandard.Library
nuget canopy

and I load the following:

#r "packages/NETStandard.Library/build/netstandard2.0/ref/netstandard.dll"
#r "packages/Selenium.WebDriver/lib/netstandard2.0/WebDriver.dll"
#r "packages/canopy/lib/netstandard2.0/canopy.dll"

Note: I included netstandard2.0 as I was having issues with not finding Object

Upvotes: 1

Views: 1007

Answers (2)

AMieres
AMieres

Reputation: 5004

I do not have a full answer, but this is what I have found out. To recap:

  1. with NETStandard.Library error seq<'a> is not compatible shows.
  2. without NETStandard.Library the error 'Object' is required shows

Yet, in option #2, in my tests the code actually runs when invoked with FSI. Which means the error is a library conflict in the Intellisense module.

In my tests the script runs also when using the option --noframework and referencing either of these two:

  • packages/FSharp.Core/lib/net45/FSharp.Core.dll
  • packages/FSharp.Core/lib/netstandard1.6/FSharp.Core.dll

... although only with version 4.3.4 but not with version 4.5.0

EDIT

A possible solution is adding an explicit reference to the local GAC netstandard.dll, like this:

#r @"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\netstandard\v4.0_2.0.0.0__cc7b13ffcd2ddd51\netstandard.dll"

That seems to solve the issue for Intellisense

Upvotes: 1

kaefer
kaefer

Reputation: 5741

Any reason not to use List<'T>'s method AsReadOnly()?

let ofSeq<'a> (ss: 'a seq) = (ResizeArray ss).AsReadOnly()
// val ofSeq :
//   ss:seq<'a> -> System.Collections.ObjectModel.ReadOnlyCollection<'a>

Upvotes: 1

Related Questions