algebraic-data-types

Differences between case object T and case class T() when defining ADT?

穿精又带淫゛_ 提交于 2021-02-10 12:22:08
问题 Let's say in scala I have an ADT as follows: sealed trait Animal object Animal { case class Lion(name: String) extends Animal case class Elephant(name:String) extends Animal case object Tiger extends Animal } Here, is it preferable to declare Tiger as a case object or should it be declared as an empty case class i.e case class Tiger() ? Does one have any advantage over other? 回答1: If there is the only Tiger it should be an object. If there can be several equal Tiger s it should be a class.

Type-safety with ADT and Aux pattern

北城余情 提交于 2021-02-07 09:51:53
问题 I'm designing type-safe code with ADT and Aux-pattern and cannot get rid of some asInstanceOf . Here is the example: sealed trait Source case object FileSystem extends Source case object Network extends Source sealed trait Data { type S <: Source } object Data { type Aux[T <: Source] = Data { type S = T } } case class RegularFile(path: String) extends Data { type S = FileSystem.type } case class Directory(path: String) extends Data { type S = FileSystem.type } case class UnixDevice(path:

Type-safety with ADT and Aux pattern

断了今生、忘了曾经 提交于 2021-02-07 09:51:28
问题 I'm designing type-safe code with ADT and Aux-pattern and cannot get rid of some asInstanceOf . Here is the example: sealed trait Source case object FileSystem extends Source case object Network extends Source sealed trait Data { type S <: Source } object Data { type Aux[T <: Source] = Data { type S = T } } case class RegularFile(path: String) extends Data { type S = FileSystem.type } case class Directory(path: String) extends Data { type S = FileSystem.type } case class UnixDevice(path:

Reducing match indentation for deeply nested properties

天涯浪子 提交于 2021-01-28 18:29:50
问题 I need to refer to a value deep within a structure which includes an Option nested in a struct property, nested in a Result . My current (working) solution is: let raw = &packet[16..]; match PacketHeaders::from_ip_slice(raw) { Err(_value) => { /* ignore */ }, Ok(value) => { match value.ip { Some(Version4(header)) => { let key = format!("{}.{}.{}.{},{}.{}.{}.{}", header.source[0], header.source[1], header.source[2], header.source[3], header.destination[0], header.destination[1], header

Are there algebraic data types outside of sum and product?

倾然丶 夕夏残阳落幕 提交于 2021-01-27 04:32:22
问题 By most definitons the common or basic algebraic data types in Haskell or Scala are sum and product. Examples: 1, 2. Sometimes a definition just says algebraic data types are sum and product, perhaps for simplicity. However, the definitions leave an impression that other algebraic data types are possible, and sum and product are just the most useful to describe selection or combination of elements. Given there are subtraction, division, raising to an integer power operations in a basic

Type Constructor as Return Type

 ̄綄美尐妖づ 提交于 2020-07-05 07:22:43
问题 In Scala, I can define an Algebraic Data Type: scala> sealed trait Maybe[A] defined trait Maybe scala> case class Just[A](x: A) extends Maybe[A] defined class Just scala> case object NothingHere extends Maybe[Nothing] defined object NothingHere It's possible to return a function, f , with a return type of Maybe[A] . scala> def f[A](x: A): Maybe[A] = Just(x) f: [A](x: A)Maybe[A] However, it's also possible to specify that a Just[A] is returned. scala> def f[A](x: A): Just[A] = Just(x) f: [A](x

Type Constructor as Return Type

佐手、 提交于 2020-07-05 07:21:29
问题 In Scala, I can define an Algebraic Data Type: scala> sealed trait Maybe[A] defined trait Maybe scala> case class Just[A](x: A) extends Maybe[A] defined class Just scala> case object NothingHere extends Maybe[Nothing] defined object NothingHere It's possible to return a function, f , with a return type of Maybe[A] . scala> def f[A](x: A): Maybe[A] = Just(x) f: [A](x: A)Maybe[A] However, it's also possible to specify that a Just[A] is returned. scala> def f[A](x: A): Just[A] = Just(x) f: [A](x

How can I see the full expanded contract of a Typescript type?

笑着哭i 提交于 2020-05-10 03:13:18
问题 If I have a collection of types that looks a bit like this, only more verbose: type ValidValues = string | number | null type ValidTypes = "text" | "time" | "unknown" type Decorated = { name?: string | null type?: ValidTypes value?: ValidValues title: string start: number } type Injected = { extras: object } // overriding the types from Decorated type Text = Decorated & Injected & { name: string type: "text" value: string } My actual code has more going on, but this shows the core idea. I don

How can I see the full expanded contract of a Typescript type?

血红的双手。 提交于 2020-05-10 03:11:44
问题 If I have a collection of types that looks a bit like this, only more verbose: type ValidValues = string | number | null type ValidTypes = "text" | "time" | "unknown" type Decorated = { name?: string | null type?: ValidTypes value?: ValidValues title: string start: number } type Injected = { extras: object } // overriding the types from Decorated type Text = Decorated & Injected & { name: string type: "text" value: string } My actual code has more going on, but this shows the core idea. I don

Why can't i re-use same value constructor among different data types?

有些话、适合烂在心里 提交于 2020-02-15 07:37:38
问题 i am new to Haskell and probably missing something really basic here, but i am not able to re-use same value constructor among different data types. data Colour = Red | Pink | Orange | Yellow data Fruit = Apple | Orange | Banana This produces error saying Multiple declarations of ‘Orange’ Not sure why this isn't allowed, i have been using OCaml before learning Haskell and was able to define types like this 回答1: As a quick exercise try just defining one of your data types and then opening up