-
Notifications
You must be signed in to change notification settings - Fork 90
Closed
Labels
Description
Description
I wrote a simple type provider that references String.length in the untyped quotation used to provide a constructor implementation. The type provider library builds, but a consuming project fails to build with the following error:
FSC : error FS0193: The module/namespace 'Microsoft.FSharp.Collections' from compilation unit 'FSharp.Core' did not contain the namespace, module or type 'StringModule' [/Users/ben/proj/oss/constrainedtypes/test/ConstrainedTypes.Test/ConstrainedTypes.Test.fsproj]
The type provider is implemented as follows:
namespace ConstrainedTypes
open System.IO
open System.Reflection
open FSharp.Quotations
open ProviderImplementation
open ProviderImplementation.ProvidedTypes
open FSharp.Quotations
open Microsoft.FSharp.Quotations
[<TypeProvider>]
type ConstrainedStringProvider (config : TypeProviderConfig) as this =
inherit TypeProviderForNamespaces (config, addDefaultProbingLocation=true)
let rootNs = "ConstrainedTypes"
let thisAssembly = Assembly.GetExecutingAssembly()
let boundedStringProvider = ProvidedTypeDefinition(thisAssembly, rootNs, "BoundedString", Some typeof<string>)
do
boundedStringProvider.DefineStaticParameters([ProvidedStaticParameter("Length", typeof<int>)], fun name args ->
let length = args.[0] :?> int
let provided = ProvidedTypeDefinition(thisAssembly, rootNs, name, Some typeof<string>)
ProvidedConstructor(
[ProvidedParameter("value", typeof<string>)],
fun args ->
<@@
printfn "checking bound: %d" length
if (length < String.length %%(args.[0])) then
sprintf "provided value exceeded the bounds: '%s' > %d"
%%(args.[0]) length
|> invalidArg "value"
@@>
)
|> provided.AddMember
provided
)
this.AddNamespace(rootNs, [boundedStringProvider])
[<TypeProviderAssembly>]
do ()This implementation can be cloned here: https://github.com/aggieben/constrainedtypes
Repro steps
See implementation above. Clone the linked implementation and build the ConstrainedTypes.Test project to repro.
Expected behavior
I expected the use of the provided type BoundedString<10> to compile.
Actual behavior
The type provider fails at consumer compile time.
Known workarounds
None
Related information
- MacOS Catalina
- .NET Core 3.1.100
Reactions are currently unavailable