The standard name of the type of amount, for example, or for three cases?

Is there a standard type of amount, as in 3 cases? Haskell has These , but this is not entirely true.

+6
source share
5 answers

I think that relying heavily on this type is an anti-pattern.

One of the nicest things you get from using algebraic data types is that the type you receive tells you something about the domain you are working with. With a generic type like Choice<T1, T2, T3> you really don't say anything about the domain.

I think option<T> (aka Maybe ) is very clear that it says that a value of type T either exists or is missing for some reason. I think Either<'T, exn> is still very clear that it says that you are getting a value or an exception. However, when you have more than two cases, it becomes quite difficult to understand what is meant by cases, and therefore, explicitly defining the type with the names corresponding to the domain may be a good idea.

(Sometimes I use Choice<T1, T2, T3> in F #, but usage is usually limited to a small area - less than 50 lines of code, so I can easily find that the value is surrounded by the code that it consumes.)

+15
source

In recent Haskell, I would include a little kitchen sink.

 {-# LANGUAGE PolyKinds, DataKinds, GADTs, KindSignatures, TypeOperators, PatternSynonyms #-} 

Then I would define type membership

 data (:>) :: [x] -> x -> * where Ze :: (x ': xs) :> x Su :: xs :> x -> (y ': xs) :> x 

and now I have all the final sums without provoking a whole series of definitions like OneOfN:

 data Sum :: [*] -> * where (:-) :: xs :> x -> x -> Sum xs 

But to address Thomas' readability problem, I would use template synonyms. In fact, these kinds of things are the reason why I have pounded synonyms of patterns for many years.

You may have a funny version of Maybe :

 type MAYBE x = Sum '[(), x] pattern NOTHING :: MAYBE x pattern NOTHING = Ze :- () pattern JUST :: x -> MAYBE x pattern JUST x = Su Ze :- x 

and you can even use newtype to create recursive sums.

 newtype Tm x = Tm (Sum '[x, (Tm x, Tm x), Tm (Maybe x)]) pattern VAR :: x -> Tm x pattern VAR x = Tm (Ze :- x) pattern APP :: Tm x -> Tm x -> Tm x pattern APP fs = Tm (Su Ze :- (f, s)) pattern LAM :: Tm (Maybe x) -> Tm x pattern LAM b = Tm (Su (Su Ze) :- b) 

The newtype wrapper also allows you to make an instance declaration for types constructed in this way.

You can, of course, also use template synonyms to hide the Either iteration beautifully.

This method is not exclusive to sums: you can do this for products as well, and this pretty much happens in the de Vries and LΓΆh Generics-SOP libraries.

The big victory of this coding is that the data description is data of type (s), allowing you to compile a lot of deriving functionality without breaking the compiler.

In the future (if I have my own way), all data types will be defined, not announced, data type descriptions made up of data characterizing both the algebraic structure (allowing to calculate the general equipment) of the data and their appearance (as you can see what you do when working with a particular type).

But the future is already here.

+8
source

What language do you use? If it is F #, there is a three-way Choice<'T1,'T2,'T3> type . (Also type 4-, 5-, 6- and 7-way Choice in addition to the more β€œstandard” double-sided type).

+3
source

They are called co-products, and Either are just 2 arguments. You can use the helpers from the formless library to create custom code products using:

 type CP = Int :+: String :+: Boolean :+: CNil val example = Coproduct[CP]("foo") 

Then you can use all the useful poly magic to match them or perform other operations:

 object printer extends Poly1 { implicit def caseInt = at[Int](i => i -> s"$i is an int") implicit def caseString = at[String](s => s -> s"$s is a string") implicit def caseBoolean = at[Boolean](b => s -> s"$b is a bool") } val mapped = example map printer mapped.select[(String, String)] shouldEqual "foo is a string" 

Scala.JS + Shapeless can work together as far as I know, so it can give you what you want.

+3
source

All Articles