I am trying to use generics in an F# signature file, but I am having some troubles with the implementation.
this is the content of the signature file:
module BalancedBST
type 'a BalancedBST
val add : 'a -> 'a BalancedBST -> 'a BalancedBST
and here is the implementation file:
module BalancedBST
type SongName = SN of string
type AlbumName = AlN of string
type Year = Y of int16
type ArtistName = ArN of string
type Song = SongName * AlbumName * Year * ArtistName
type 'a BalancedBST =
| Leaf
| Node of Song * Song BalancedBST * Song BalancedBST
let add (el : Song) (avl : Song BalancedBST) = avl
As I am quite a newbie with this language and functional programming in general, I was expecting an OOP like behavior, where you define an interface with a generic type which will be specified later with the concrete class. Instead, I got this error while creating the dll:
Microsoft (R) F# Compiler version 10.2.3 for F# 4.5
Copyright (c) Microsoft Corporation. All Rights Reserved.
/path/to/fs/file error FS0034: Module 'BalancedBST' contains
val add : SongName * AlbumName * Year * ArtistName -> avl:Song BalancedBST -> Song BalancedBST
but its signature specifies
val add<'a> : 'a -> 'a BalancedBST -> 'a BalancedBST
The respective type parameter counts differ
Assuming that I undrestood that message properly, this means that the implementation file has to use functions that keep the generic inside their signature, which isn't something that I want.
Is there any solution to my problem or is it something that doesn't stick with functional programming and just belongs to OOP?
You can't have a generic signature file and a "more concrete" implementation that backs the signature this way. The add
function must match the signature in the signature file.
If you want a generic interface with concrete implementations, you'll need to use an interface and back it with objects.