Open bamarsha opened 1 year ago
I agree that typeclasses are the right solution here. Here's a potential the haskell-style approach, modified for Q# syntax*:
typeclass Display {
function display(a: Self) : String;
}
newtype MyType = (myNumber: Int);
instance Display of MyType {
function display(a: Self) : String{ toString(a::myNumber) }
}
// generic bounded polymorphic callable
function Foo<'A: Display>(a: 'A): Unit {
let stringified = display(a);
}
function Main() : Unit {
let myType = MyType(1);
Foo(myType);
}
*: All keywords and syntax is subject to later bikeshedding. This comment serves only for semantic outline purposes.
It is really quite difficult to write most abstractions without bounded polymorphism. I think this task can be split up into smaller tasks:
Number 1 gets us a lot of functionality, and may be enough for a while. For example, being able to constrain inputs to being Eq + Show
would unblock things like a testing framework. Numbers 2 and 3 could be done eventually.
From microsoft/qsharp-language#149: