Skip to content

Commit 8fd1aea

Browse files
committed
Total conversion functions conventions
1 parent 2d069d3 commit 8fd1aea

4 files changed

+74
-5
lines changed

docs/ADR-008-Use-RIO-in-cardano‐cli.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Status
2-
- [x] Adopted 2025/02/10
3-
2+
- [x] Adopted 2025-02-10
43

54
# Required Reading
65

@@ -183,4 +182,4 @@ The purpose of `CustomCliException` is to represent explicitly thrown, structure
183182
# Consequences
184183
- This should dramatically improve our code's composability and remove many unnecessary error types.
185184
- Readability concerning what errors can be thrown will be negatively impacted. However, `ExceptT` already lies about what exceptions can be thrown because it is not limited to the error type stated in `ExceptT`'s type signature. In other words, `IO` can implicitly throw other `Exception`s.
186-
- Initially, this will be adopted under the "compatible" group of commands so `cardano-cli` will have a design split temporarily. Once we are happy with the result we will propagate to the rest of `cardano-cli`
185+
- Initially, this will be adopted under the "compatible" group of commands so `cardano-cli` will have a design split temporarily. Once we are happy with the result we will propagate to the rest of `cardano-cli`

docs/ADR-009-cardano-api-exports-convention.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Status
22

3-
✅ Accepted 21-02-2025
3+
✅ Accepted 2025-02-21
44

55
# Context
66

docs/ADR-010-cardano-api-script-witness-api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Status
22

3-
-[x] Accepted 13-03-2025
3+
-[x] Accepted 2025-03-13
44

55
# Context
66

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Status
2+
3+
- [ ] Proposed 2024-05-07
4+
5+
# Context
6+
7+
## The Problem
8+
In `cardano-api` we have multiple functions performing conversions between one value of the type to the other, for example:
9+
10+
```haskell
11+
toShelleyScriptHash :: ScriptHash -> ScriptHash
12+
fromShelleyScriptHash :: ScriptHash -> ScriptHash
13+
```
14+
15+
There are multiple naming conventions for the conversion functions which makes them hard to locate.
16+
Some conversion functions with lengthy names, are not very convenient to use.
17+
18+
## Solution Proposal
19+
20+
### Type classes
21+
22+
For total functions, which are simply converting a value from one type to another, we can use type classes [`Inject`](https://cardano-ledger.cardano.intersectmbo.org/cardano-ledger-core/Cardano-Ledger-BaseTypes.html#t:Inject) & [`Convert`](https://cardano-api.cardano.intersectmbo.org/cardano-api/Cardano-Api-Internal-Eras.html#t:Convert):
23+
```haskell
24+
class Inject t s where
25+
inject :: t -> s
26+
27+
class Convert (f :: a -> Type) (g :: a -> Type) where
28+
convert :: forall a. f a -> g a
29+
```
30+
31+
The use of those conversion functions is limited to **internal use only**.
32+
The library should still export conversion functions with explicit type names for better readability.
33+
34+
### Explicit conversion functions
35+
36+
For explicit conversion functions, the following naming convention should follow:
37+
38+
```haskell
39+
fooToBar :: Foo -> Bar
40+
```
41+
42+
43+
If the module exporting conversion functions is meant to be imported qualified, and provides functions for operating on a single data type, a shorter name with `from` prefix is allowed:
44+
45+
```haskell
46+
module Data.Foo where
47+
48+
fromBar :: Bar -> Foo
49+
```
50+
51+
where the usage would look like:
52+
```haskell
53+
import Data.Foo qualified as Foo
54+
55+
Foo.fromBar bar
56+
```
57+
58+
# Decision
59+
60+
TBD
61+
62+
# Consequences
63+
64+
## Advantages
65+
- An uniform API for total conversions
66+
- Less exported symbols, which currently have few conventions for the conversions
67+
- Less maintenance burden with regards to the naming conventions of the conversion functions
68+
69+
## Disadvantages
70+
- It may be a bit difficult to discover available conversions, because one would have to browse the type's `Inject` instances to find the conversion functions they are looking for - instead of looking for exported functions.

0 commit comments

Comments
 (0)