how to have generic constrained type

volia17

I would like to represent some constrained data with scala type system, with constants. For example in pseudo code, what i would like to write is something like this, (and optionaly if possible, using compile-time checking the contraints)

val data1 : String of 10
val data2 : Int of (0..10)
val data3 : Int of (1..1000)
val data4 : String of 30

// etc.

without writing such code for every type :

type Tagged[U] = { type Tag = U }
type @@[T, U] = T with Tagged[U]
object Tag {
  @inline def apply[T, U](t: T): T @@ U = t.asInstanceOf[T @@ U]
}

sealed trait StringOf32
object StringOf32 {
  def apply(value : String) : String @@ StringOf32 = {
    require(value.length <= 32)
    Tag(value)
  }      
  def unapply(s : String @@ StringOf32) : Option[String] = Some(s)      
}

sealed trait IntFrom0To10
object IntFrom0To10 {
  def apply(value : Int) : Int @@ IntFrom0To10 = {
    require(value >= 0 && value <= 10 )
    Tag(value)
  }      
  def unapply(s : Int @@ IntFrom0To10) : Option[Int] = Some(s)      
}

// etc.

Is there alrealdy exists some library wich propose this kind of construction ? Is there a way, to have such generic construction ? Perhaps using macros, but i'm not sure it's a good idea, and i'm not fluent with.

What do you think, in what direction should i go ?

tilois

Not exactly sure if this is what you are looking for but what I see here instantly reminds me of refined. Is that what you are looking for?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Generic constrained type default value

From Dev

Invalid Cast of Type Constrained C# Generic

From Dev

How to store Action delegates with constrained generic type in a type safe collection in C#?

From Dev

How to store Action delegates with constrained generic type in a type safe collection in C#?

From Dev

Using a generic type passed without constraint with a constrained generic type

From Dev

how to have a reference to child generic type from a parent generic type?

From Dev

How to constraint a generic type to have new()?

From Dev

Generic type constrained on f# method involving static type conversion

From Dev

Delphi Generics: cannot cast class and constructor constrained generic type to an interface

From Dev

Returning a lambda function with parameter types constrained by generic type parameters

From Dev

Incorrectly Inferred Type in Generic Closure with Protocol Extension Constrained by Self

From Dev

How to declare type as constrained in F#

From Dev

How to specify type for class constrained value?

From Dev

C++ parameter pack, constrained to have instances of a single type?

From Dev

How to promise the compiler that the generic type will have a certain property?

From Dev

How to have default and specialised function in Swift depending on generic type?

From Java

How to have Java method return generic list of any type?

From Dev

How to have generic numeric type in scala for addition and multiplication?

From Java

How to check if two structs have the same generic parameter type in Swift?

From Dev

Make function of generic class only accept argument that is same generic type but more constrained

From Dev

Is there a way for a Generic object to have no Generic type?

From Dev

Can't omit the extension type argument when calling generic extension method with mutually constrained type parameters

From Dev

Why do I have to cast to type parameter and can't use the constrained type?

From Dev

Why does a direct cast fail but the "as" operator succeed when testing a constrained generic type?

From Dev

Generic argument constrained to be non array

From Dev

Array Type is not constrained - VHDL

From Dev

Constrained closed type family

From Dev

Synonym to constrained type

From Dev

Type of constrained string values

Related Related

  1. 1

    Generic constrained type default value

  2. 2

    Invalid Cast of Type Constrained C# Generic

  3. 3

    How to store Action delegates with constrained generic type in a type safe collection in C#?

  4. 4

    How to store Action delegates with constrained generic type in a type safe collection in C#?

  5. 5

    Using a generic type passed without constraint with a constrained generic type

  6. 6

    how to have a reference to child generic type from a parent generic type?

  7. 7

    How to constraint a generic type to have new()?

  8. 8

    Generic type constrained on f# method involving static type conversion

  9. 9

    Delphi Generics: cannot cast class and constructor constrained generic type to an interface

  10. 10

    Returning a lambda function with parameter types constrained by generic type parameters

  11. 11

    Incorrectly Inferred Type in Generic Closure with Protocol Extension Constrained by Self

  12. 12

    How to declare type as constrained in F#

  13. 13

    How to specify type for class constrained value?

  14. 14

    C++ parameter pack, constrained to have instances of a single type?

  15. 15

    How to promise the compiler that the generic type will have a certain property?

  16. 16

    How to have default and specialised function in Swift depending on generic type?

  17. 17

    How to have Java method return generic list of any type?

  18. 18

    How to have generic numeric type in scala for addition and multiplication?

  19. 19

    How to check if two structs have the same generic parameter type in Swift?

  20. 20

    Make function of generic class only accept argument that is same generic type but more constrained

  21. 21

    Is there a way for a Generic object to have no Generic type?

  22. 22

    Can't omit the extension type argument when calling generic extension method with mutually constrained type parameters

  23. 23

    Why do I have to cast to type parameter and can't use the constrained type?

  24. 24

    Why does a direct cast fail but the "as" operator succeed when testing a constrained generic type?

  25. 25

    Generic argument constrained to be non array

  26. 26

    Array Type is not constrained - VHDL

  27. 27

    Constrained closed type family

  28. 28

    Synonym to constrained type

  29. 29

    Type of constrained string values

HotTag

Archive