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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:29:19+00:00 2026-05-23T21:29:19+00:00

I have defined some complex data types, for instance: type drawer = { …

  • 0

I have defined some complex data types, for instance:

type drawer =
  { ...
    box: boxes list }
type table =
  { ...
    drawers: drawer list }
type room =
  { ...
    tables: table list }

And I want to define some functions of similar purposes but with different types of arguments, for exemple:

val compare_size_rooms: room -> room -> bool (* true: bigger, false: smaller *)
val compare_size_tables: table -> table -> bool
val compare_size_boxes: box -> box -> bool

One thing is that sometimes, due to different structures, 2 rooms/tables/boxes may be not comparable, therefore I hope the comparison could give me some extra information, besides just true or false.

My question is, whether it is a common way to define a type info:

type info =
  | Incomparable_Tables_One_foldable_Another_non_foldable
  | Incomparable_Tables_One_rectangle_Another_triangle
  | Incomparable_Boxes_One_in_paper_Another_in_metal
  | ...

And I make the functions as following:

val compare_size_rooms: room -> room -> bool * info (* true: bigger, false: smaller *)
val compare_size_tables: table -> table -> bool * info
val compare_size_boxes: box -> box -> bool * info

So that in those functions, I analyse 2 values, if they are comparable, the bool returns which is bigger or smaller, otherwise, info returns useful information of analysis.

This structure seems me not very common, could anyone tell me if it is usual, or to realize the same thing if there is a better way?

Thank you very much

  • 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-23T21:29:20+00:00Added an answer on May 23, 2026 at 9:29 pm

    If the two objects you are comparing are not comparable, it is not advisable to return a boolean value as if they were comparable. Instead, you can raise an exception:

    exception Incomparable_Tables_One_foldable_Another_non_foldable
    
    let compare_size_tables a b = 
      if comparable a b then 
        a.size < b.size (* Or whatever you do *)
      else
        raise Incomparable_Tables_One_foldable_Another_non_foldable
    
    (* val compare_size_tables : table -> table -> bool *)
    

    The alternative is to use a variant return type which can be either a boolean (when the objects are comparable) or an information type (when they are not). For instance, using the Batteries library:

    open BatPervasives
    
    type info = 
      | Incomparable_Tables_One_foldable_Another_non_foldable
      | ...
    
    let compare_size_tables a b = 
      if comparable a b then 
        Ok (a.size < b.size) 
      else
        Bad (Incomparable_Tables_One_foldable_Another_non_foldable)
    
    (* val compare_size_tables : table -> table -> (bool, info) BatStd.result *)
    

    In the latter case, you will have to match whether the result was Bad info or Ok boolean.

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

Sidebar

Related Questions

I have a complex data structure (user-defined type) on which a large number of
I have some logic, which defines and uses some user-defined types, like these: class
I would like to export some macros that I have defined and be able
Most systems will have a user-defined function (UDF) available. Some will not. i want
I have seen some C++ classes with a destructor defined as follows: class someClass
I have a need to evaluate user-defined logical expressions of arbitrary complexity on some
If I have have some overloaded ostream operators, defined for library local objects, is
I have a pretty simple UIScrollView defined inside Interface Builder. I've pasted in some
I have an interesting problem trying to represent complex schedule data in a database.
I have an xsd which has 3 imports to another xsd, 8 complex types

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.