Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# signature files - defining constructor parameters

Tags:

f#

I'm running into an error when creating a signature file for an F# script file which I can't quite work out.

To replicate, create a new F# class library and add a file, Test.fs:

namespace Signatures

open System

type Test (id : Guid, name : string) =

  member this.Id = id
  member this.Name = name

This will build fine. Then create a new signature file above it, Test.fsi:

namespace Signatures

open System

type Test =

  new : (Guid * String) -> Test

  member Id : Guid
  member Name : String

This will now not build with the error Module 'Signatures' requires a value 'new : (Guid * String) -> Test (this is different to the error you get if the constructor signature is different in the two files). The only real documentation I can find on defining constructors in the signature file is MSDN and that deals with parameterless constructors.

If you hover over Test in the .fs file the constructor's signature matches the one in the .fsi file. I've also tried changing the constructor so that it is not implicit with no joy.

I'm using the VS2012 RC, and have tried .Net 4 and 4.5.

like image 521
Mark Watts Avatar asked Dec 27 '22 20:12

Mark Watts


1 Answers

It's too long for a comment, so I post it as an answer.

Test's constructor receives two arguments as its parameters, not one argument which is a tuple. I admit that * between arguments looks confusing. But the signature Guid -> string -> Test is even worse than that. Constructors should get some inputs and produce a new type instance. Curried form and partial application don't make sense in the context of constructors.

I think the brackets help clarify here.

type Test (id : System.Guid, name : string) =
  member this.Id = id
  member this.Name = name

produces new : id:Guid * name:string -> Test while

type Test (tuple: System.Guid * string) =
  let id, name = tuple
  member this.Id = id
  member this.Name = name

gives me new : tuple:(Guid * string) -> Test in an FSI session. I use F# 2.0/MonoDevelop 3.0 for the record.

Regarding creating type signatures, I usually send code into F# Interactive and copy produced signatures to fsi files to avoid mistakes. If the tooltips and F# Interactive show type signatures wrongly on VS2012 RC, you should report at fsbugs (at) microsoft (dot) com.

like image 88
pad Avatar answered Jan 06 '23 23:01

pad