0

I would like to add type constraints as shown below. When I do, I get the error

error FS0698: Invalid constraint: the type used for the constraint is sealed, which means the constraint could only be satisfied by at most one solution

type PropertyValue<'T when 'T :> System.Int16 and 'T :> System.String> =     | Single of 'T    | Array of 'T[]  

This is what I am trying to do.

type PropertyValueInfo = | String of string | Int of int | StringArray of string[] | IntArray of int[] 

In our domain, users can define properties and the properties can be string, int or array versions of string and int. I am trying to model this in a generic sense so that in the future, I can add say, double type.

4
  • 1
    There can be no type 'T that is a subtype of both Int16 and String. Commented Aug 19, 2021 at 16:12
  • 1
    Can you elaborate what you're trying to achieve? e.g what's the concrete scenario for your problem. Why can't you have distinct types for your property values e.g. Int Property, string property? Commented Aug 19, 2021 at 16:12
  • 1
    You also can't subtype Int16 either. Commented Aug 19, 2021 at 16:56
  • Thanks Fyodor, Koenig and Phillip for your quick responses. This is what I am trying to do. <br/> type PropertyValueInfo = | String of string | Int of int | StringArray of string[] | IntArray of int[] <br/> In our domain, users can define properties and the properties can be string, int or array versions of string and int. I am trying to model this in a generic sense so that in the future, I can add say, double type. Hope this makes sense. Thanks Commented Aug 19, 2021 at 17:52

1 Answer 1

2

It's not possible to use "or" as a type constraint, and it doesn't really make sense - what would the common type be?

Another way to model this is to use two DUs:

type PropertyValue<'T> = | Single of 'T | Multiple of 'T list type PropertyType = | Int of PropertyValue<int> | String of PropertyValue<string> let singleString = String (Single "hello") let multInt = Int (Multiple [41; 42]) 
Sign up to request clarification or add additional context in comments.

6 Comments

did you mean "It's not possible to use and as a type constraint"?
In fact no :), I can see that the question is using and, but the problem is about constraining the parameter to int or string.
but your example shows how to use 'or' e.g. union types :)
I'm sure we mean the same thing here :), but the initial paragraph is just to highlight that the question seems to be looking for a generic or constraint, and then the rest of the answer shows how to do it with DU's instead.
Yeah, I don't agree with the wording, strictly speaking you can do type constraints using 'or' they're called DU or union types. Mathematically speaking you can also do 'and' e.g. type intersections and but it's not supported in F#, for example in OWL. Your example though is great :) Hopefully helps the OP.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.