Is it possible to enforce that a Record respects some invariants?

前端 未结 3 1261
南方客
南方客 2020-12-18 17:47

Suppose I wanted to create a Record type that represents acceptable min/max bounds:

type Bounds = { Min: float; Max: float }

Is there a way

3条回答
  •  感情败类
    2020-12-18 18:40

    A dodgy solution for the string example - use a DU

    type cleverstring = |S of char * string
    

    This will force the string to have at least one charcter. Then you can just use cleverstring instead of string in your record, although you probably want to write some wrapper functions to make it look like a string.

提交回复
热议问题