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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:36:08+00:00 2026-05-24T05:36:08+00:00

Consider the following F# code: type ILinear = interface end type IMetric = interface

  • 0

Consider the following F# code:

type ILinear =
  interface 
  end

type IMetric =
  interface
  end


[<Measure>] type cm =
    interface ILinear
    interface IMetric 

[<Measure>] type m =
    interface ILinear
    interface IMetric

[<Measure>] type time

I want to use these interfaces as a means of both grouping the types of the measures and as a way of allowing a level of genericness between “any measure” and “a specific measurement”–something akin to:

(* Yes, I know this syntax is probably incorrect *)    
let rate (distance:float<'u:#ILinear,#IMetric>) (time:float<time>) =
       distance/time

I realize this is probably pushing the limits of possibility but I’m just kind of curious if this is possible and if so, what the syntax would be. As I say, this is using interfaces as sort of a poor man’s mixin.

  • 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-24T05:36:09+00:00Added an answer on May 24, 2026 at 5:36 am

    I don’t think this is possible, but I quite like the idea :-).

    If it was possible, then the constraints would be probably written using the same syntax that you can use to write interface constraints for ordinary (non-measure) type parameters:

    let rate<[<Measure>] 'u when 'u :> IMetric> (distance:float<'u>) (time:float<time>) =
        distance/time
    

    The error message clearly says that constraints can be only specified on ordinary type parameters (actually, I was even surprised that units of measure can implement interfaces – it doesn’t look very useful as they are completely erased during the compilation):

    error FS0703: Expected type parameter, not unit-of-measure parameter

    The best workaround that I can think of is to write a simple wrapper that stores a value (with some unit) and additional (phantom) type that represents the constraints:

    [<Struct>]    
    type FloatValue<[<Measure>] 'u, 'constr>(value:float<'u>) =
      member x.Value = value
    
    let cm f = FloatValue<_, IMetric>(f * 1.0<cm>)
    

    The cm function takes a float and wraps it into a FloatValue. The second type argument is an ordinary type argument, so it can be provided with some type that implements interfaces (or with just a single interface). The rate function then looks like this:

    let rate (distance:FloatValue<'u, #IMetric>) (time:float<time>) =
      distance.Value / time
    

    Since the constraints cannot be specified on a unit type, we have to specify them on the second type argument. You can then call the function using:

    rate (cm 10.0) 5.0<time>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please consider the following code: type TFoo1 = class public procedure DoSomething1; end; TFoo2
Please consider the following code. enum type {CONS, ATOM, FUNC, LAMBDA}; typedef struct{ enum
Consider the following code. public interface IFoo { } public class Bar { public
Consider the following code sample: interface IData { int Count(); } interface IOperations {
I'm trying to learn C#'s restrictions on an anonymous type. Consider the following code:
Consider the following example F# code: type mytype() = member this.v = new OtherClass()
Consider the following code-snippet typedef int type; int main() { type *type; // why
Consider the following piece of Haskell code: type Parser a = String -> [(a,
Please consider the following code: typedef struct { int type; } object_t; typedef struct
Consider the following Code snippet, Assembly asm = Assembly.LoadFile(DLL_Path); Type t = asm.GetType(DLL_NameSpace.MyClass, false,

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.