Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7714603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:10:15+00:00 2026-06-01T02:10:15+00:00

I am trying to emulate a system of type classes in F#; I would

  • 0

I am trying to emulate a system of type classes in F#; I would like to create pair printer which automatically instantiates the right series of calls to the printing functions. My latest try, which is pasted here, fails miserably since F# cannot identify the right overload and gives up immediately:

type PrintableInt(x:int) =
  member this.Print() = printfn "%d" x

let (!) x = PrintableInt(x)

type Printer() =
  static member inline Print< ^a when ^a : (member Print : Unit -> Unit)>(x : ^a) =
    (^a : (member Print : Unit -> Unit) x)
  static member inline Print((x,y) : 'a * 'b) =
    Printer.Print(x)
    Printer.Print(y)

let x = (!1,!2),(!3,!4)

Printer.Print(x)

Is there any way to do so? I am doing this in the context of game development, so I cannot afford the runtime overhead of reflection, retyping and dynamic casting: either I do this statically through inlining or I don’t do it at all 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T02:10:16+00:00Added an answer on June 1, 2026 at 2:10 am

    What you’re trying to do is possible.
    You can emulate typeclasses in F#, as Tomas said maybe is not as idiomatic as in Haskell. I think in your example you are mixing typeclasses with duck-typing, if you want to go for the typeclasses approach don’t use members, use functions and static members instead.

    So your code could be something like this:

    type Print = Print with    
      static member ($) (_Printable:Print, x:string) = printfn "%s" x
      static member ($) (_Printable:Print, x:int   ) = printfn "%d" x
      // more overloads for existing types
    
    let inline print p = Print $ p
    
    type Print with
      static member inline ($) (_Printable:Print, (a,b) ) = print a; print b
    
    print 5
    print ((10,"hi"))
    print (("hello",20), (2,"world"))
    
    // A wrapper for Int (from your sample code)
    type PrintableInt = PrintableInt of int with
      static member ($) (_Printable:Print, (PrintableInt (x:int))) = printfn "%d" x
    
    let (!) x = PrintableInt(x)
    
    let x = (!1,!2),(!3,!4)
    
    print x
    
    // Create a type
    type Person = {fstName : string ; lstName : string } with
      // Make it member of _Printable
      static member ($) (_Printable:Print, p:Person) = printfn "%s, %s" p.lstName p.fstName
    
    print {fstName = "John"; lstName = "Doe" }
    print (1 ,{fstName = "John"; lstName = "Doe" })
    

    Note: I used an operator to avoid writing the constraints by hand, but in this case is also possible to use a named static member.
    More about this technique here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to emulate a POS printer with System.Drawing and one of the
I'm trying to emulate a SQL IN statement on a series of SQL LIKE
I'm trying to emulate a tab bar with HTML. I'd like the width of
I'm trying to emulate some pseudo-classes and attribute selectors in Internet Explorer 6 and
I've added a paragraph system component in my template, trying to emulate the Geometrixx's
I've been trying to emulate in C# a behavior which I was implementing without
I am trying to emulate some java.lang and java.io classes, e.g. OutputStream within GWT.
I am trying to emulate the size of a Galaxy Note screen, which is
I'm trying to emulate the LIKE operator in LINQ to Objects. Here my code:
I am trying to create a user login system. I have a class/controller called

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.