Skip to content

Can constraints be placed on type definitions? #350

Closed Answered by Alasdair
rsnikhil asked this question in Q&A
Discussion options

You must be logged in to vote

Technically yes, but I think it works the other way to how you are thinking. You can do:

struct VA_struct ('levels : Int, 'vpn_size : Int, 'log_pte_size : Int), 'levels >= 0 = ...

but this would make it a type error to use VA_struct anywhere 'levels < 0, rather than implicitly adding the constraint when the type appears. I would avoid adding constraints to types in general, and add them to functions using the types (as you would for typeclasses in Haskell for example).

If you have a complex constraint you can add a synonym like:

type valid_va_struct('levels : Int, 'vpn_size : Int, 'log_pte_size : Int) -> Bool = 'levels > 0 & ...

Replies: 5 comments 4 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by rsnikhil
Comment options

You must be logged in to vote
3 replies
@Alasdair
Comment options

@Alasdair
Comment options

@rsnikhil
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@Alasdair
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants