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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:46:11+00:00 2026-06-17T08:46:11+00:00

Doesn’t it make sense if I do type Point = struct val Row: int

  • 0

Doesn’t it make sense if I do

type Point = 
    struct
        val Row: int
        val Column: int

        new (row, column) = if row >= 0 && column >= 0 then { Row = row; Column = column }
                            else failwith "Cooridinators must be non-negative!" 
        // This is not a valid object construction
        static member (+) (x: Point, y: Point) = Point (x.Row + y.Row, x.Column + y.Column)
        static member (-) (x: Point, y: Point) = Point (x.Row - y.Row, x.Column - y.Column)
        static member (*) (x: Point, a) = Point (x.Row * a, x.Column * a)
        static member (*) (a, x: Point) =  Point (x.Row * a, x.Column * a)
    end

If it was a class then maybe I can raise exception during the do binding, but in a structure there is no do, what am I supposed to do?

I figured out that it is possible to add another constructor after failwith to get around this, but it raises another question, how can I call the implicit constructor? Do I have to construct it explicitly first like

new () = { Row = 0; Column = 0} 
// Error structs auto supports a default constructor

If i simply do this using the default constructor

new (row, column) = if row >= 0 && column >= 0 then { Row = row; Column = column }
                    else
                        failwith "Cooridinators must be non-negative!"
                        new Point () // error

It appears to me that Point () returns a unit rather than Point?

  • 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-17T08:46:12+00:00Added an answer on June 17, 2026 at 8:46 am

    I think the F# compiler complains because constructors should always have a structure:

    new ( patterns ) = expressions { initialization } [then expressions ]

    So, the part that initializes the fields cannot be nested under if or inside any other expression. You can throw exceptions either before the initialization or after (if you add then keyword). (This matters for classes with inheritance, but I don’t think it makes any difference for structs.)

    So, one way to write the code is to write:

    type Point = 
        struct
            val Row: int
            val Column: int
    
            new (row, column) = 
              if row < 0 || column < 0 then failwith "Cooridinators must be non-negative!"
              { Row = row; Column = column }          
    
            // (Static members omitted)
        end
    

    Note that I had to negate the condition, because you need to specify the case when you want to throw the exception (rather than saying when you can construct the object). The other alternative would be:

    new (row, column) = 
      { Row = row; Column = column }          
      then if row < 0 || column < 0 then failwith "Cooridinators must be non-negative!"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why doesn't this work? struct O { O(int i, int j) : i(i) ,
This doesn't work: int number = 1; String numberstring = IntToString(number); I get The
Why doesn't this work in VS 2010 typename std::enable_if<!std::has_trivial_destructor<Titem>::value, BOOL>::type Clear() { ... }
This doesn't work. I've got an exception from SQL databse that column does not
This doesn't work: create table event ( ... time int default date_part('epoch', timestamp 'now'),
Why doesn't this: def subsets(x): if not x: return [[]] else: return [x +
Why doesn't this work? find . -maxdepth 1 -type f -print0 | xargs -0
It doesn't seem to work: $ref = new ReflectionObject($obj); if($ref->hasProperty('privateProperty')){ print_r($ref->getProperty('privateProperty')); } It gets
Why doesn't client validation work? <script src=<%: Url.Content(~/Scripts/jquery-1.4.1.min.js)%> type=text/javascript></script> <script src=<%: Url.Content(~/Scripts/MicrosoftAjax.js)%> type=text/javascript></script> <script
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all

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.