Questions tagged [derived-instances]

Questions relating to the automated process of deriving typeclass instances for particular classes or types, such as using the "deriving" keyword in Haskell or the "derive" macro in Rust.

Typeclass resolution is a process available in several languages, most notably Haskell and Rust, by which polymorphic functionality can be provided in an ad-hoc way to several types.

Writing instances for these types can become tedious in some cases where large amounts of predictable boilerplate can be possible. For instance, the Eq typeclass in Haskell compares two values to determine if they're equal, and it is very common to implement this typeclass by simply comparing each field of the two values for equality recursively.

For classes like this, languages often provide a "deriving" mechanism by which the intuitive, default behavior can be "opted into".

data MyCustomType = MyCustomType Int String
    deriving (Eq)

In this Haskell example, we define a type called MyCustomType which contains an integer and a string. Then we "derive" the Eq instance in the default way. This is equivalent to the longer, more verbose

data MyCustomType = MyCustomType Int String

instance Eq MyCustomType where
    MyCustomType n s == MyCustomType n' s' = n == n' && s == s'

In cases where something unusual needs to be done, the full instance can still be written out.

This tag should be used for questions about the mechanism by which instances are derived in languages that provide this ability.

17 questions
0
votes
1 answer

Customizing the fields generated by automatic derivation of ToSchema

I have the following type: data Device = Device { _deviceId :: DeviceId , _deviceName :: Text , _deviceDtype :: DType } deriving (Show, Eq, Generic) makeFields ''Device $(deriveJSON…
Damian Nadales
  • 4,907
  • 1
  • 21
  • 34
0
votes
1 answer

Multiple SQL Standard Instances on 4 Processor/32-core Server

We have a large 4 processor/32-core server with 192GB of memory available in the data center and over twenty small SQL Standard databases to consolidate. They are a mix of SQL 2012 and 2008 R2 for 3rd-party apps. Is there any issue with simply…
1
2