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 1025811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:59:06+00:00 2026-05-16T11:59:06+00:00

Ocaml programmers can use so called ‘phantom types’ to enforce some constraints using the

  • 0

Ocaml programmers can use so called ‘phantom types’ to enforce some constraints using the type system. A nice example can be found at http://ocaml.janestreet.com/?q=node/11.

The syntax type readonly doesn’t work in F#. It could be replaced with a pseudo-phantom type defined as type readonly = ReadOnlyDummyValue in order to implement the tricks in the above mentioned blog post.

Is there a better way to define phantom types in F#?

  • 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-05-16T11:59:07+00:00Added an answer on May 16, 2026 at 11:59 am

    I think that defining type just using type somename won’t work in F#. The F# compiler needs to generate some .NET type from the declaration and F# specification doesn’t explicitly define what should happen for phantom types.

    You can create a concrete type (e.g. with type somename = ReadOnlyDummyValue) in the implementation file (.fs) and hide the internals of the type by adding just type somename to the interface file (.fsi). This way you get quite close to a phantom type – the user outside of the file will not see internals of the type.

    Another appealing alternative would be to use interfaces. This sounds logical to me, because empty interface is probably the simplest type you can declare (and it doesn’t introduce any dummy identifiers). Empty interface looks like this:

    type CanRead = interface end
    type CanWrite = interface end
    

    The interesting thing, in this case, is that you can also create inherited interfaces:

    type CanReadWrite = 
      inherit CanRead
      inherit CanWrite
    

    Then you can write a function that can take values of type Ref<CanRead, int> but also values of type Ref<CanReadWrite, int> (because these values also support reading):

    let foo (arg:Ref<#CanRead, int>) = // ...    
    

    This seems like something that could be useful. I would be actually quite interested whether this can be done in OCaml too (because it relies on the F# support for interfaces and inheritance).

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

Sidebar

Related Questions

I noticed that, among OCaml programmers I know, some of them always use polymorphic
OCaml process can use just one core and in order to use multiple cores
I have a OCaml library with lots of classes I need some translator to
I am reading through OCaml lead designer's 1994 paper on modules, types, and separate
Well in Ocaml you can do this at compile-time : let handle item =
Are there hashtables in Ocaml that use == instead of = when testing for
An Ocaml interpreter app was put up on iTunes last November. I've done some
I want to use OCaml to generates sets of data and make comparisons between
I am building two libraries in OCaml which contain the same variant type. The
I'm starting learning Ocaml, using hickey book , and I'm stuck on Exercise 3.4,

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.