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

  • Home
  • SEARCH
  • 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 8848913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:30:55+00:00 2026-06-14T12:30:55+00:00

type Alignment = | Horizontal | Vertical let getMainAttr = function | Horizontal ->

  • 0
type Alignment =
     | Horizontal
     | Vertical

let getMainAttr = function
                  | Horizontal -> fst
                  | Vertical -> snd

let check alignment =
    let mainAttr = getMainAttr alignment
    mainAttr (2,3) |> ignore
    mainAttr (2.0, 3.0) // error

val getMainAttr : _arg1:Alignment -> ('a * 'a -> 'a)
mainAttr : (int * int -> int) // because of the value restriction

it seems the only way to make it generic is to make it explicit, e.g. let mainAttr x = getMainAttr alignment x

However, thus it no longer utilize closure, so that each time mainAttr is called alignment has to be checked.

Is there any way to only check alignment once as well as being generic?

  • 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-14T12:30:58+00:00Added an answer on June 14, 2026 at 12:30 pm

    As @Daniel describes, you’re hitting the value restriction limitation which disallows creating generic values that are result of some F# computation (even if the value is a function). You can find more information about this in other SO questions and there is also an article about advanced points. The reason for this restriction is that generic values can introduce a loophole in the type safety.

    In your example, you do not really need to worry, because executing the getMainAttr function repeatedly is not going to add that much overhead. If the function did a more complex computation, you could return an interface with a generic method (instead of a simple function):

    /// Interface with as single generic function that selects element of a pair
    type PairSelector = 
      abstract Invoke<'T> : 'T * 'T -> 'T
    
    // Two possible implementations of the interface 
    let first = { new PairSelector with member x.Invoke(a, b) = a }
    let second = { new PairSelector with member x.Invoke(a, b) = b }
    
    // Return a non-generic `PairSelector` value instead of a generic function
    let getMainAttr = function
                      | Horizontal -> first
                      | Vertical -> second
    
    // Now we can get `PairSelector` value and call it with different type arguments    
    let check alignment =
        let mainAttr = getMainAttr alignment
        mainAttr.Invoke (2,3) |> ignore
        mainAttr.Invoke (2.0, 3.0) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to animate the horizontal (and vertical) alignment of an element within a
I am aware about the concepts of data type or structure alignment, packing, padding
The type of each member of the structure usually has a default alignment i.e.each
The type of this function is T -> (T -> U) -> U .
I have a example with me where in which the alignment of a type
For some reason on our grid the vertical alignment of the rows is not
Suppose I have a templated function that deals with pointers to yet unknown type
I want to set different backgrounds for GridSplitter 's who are horizontal vs vertical.
Can the alignment of a structure type be found if the alignments of the
main.c:78:25: erreur: assignment from incompatible pointer type [-Werror] main.c:81:9: erreur: passing argument 2 of

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.