Skip to content

Commit

Permalink
kotlin: add parent type parameters to sum-of-product cases
Browse files Browse the repository at this point in the history
  • Loading branch information
tadfisher committed Feb 1, 2024
1 parent 91504df commit ae13803
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .golden/kotlinSumOfProductWithTypeParameterSpec/golden
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
sealed class CursorInput<A> {
@Serializable
@SerialName("nextPage")
data class NextPage(val key: A?) : CursorInput()
data class NextPage<A>(val key: A?) : CursorInput<A>()

@Serializable
@SerialName("previousPage")
data class PreviousPage(val key: A) : CursorInput()
data class PreviousPage<A>(val key: A) : CursorInput<A>()
}
20 changes: 14 additions & 6 deletions src/Moat/Pretty/Kotlin.hs
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,14 @@ prettyApp t1 t2 =
-- error is restricted
prettyTaggedObject ::
String ->
[String] ->
[Annotation] ->
[Interface] ->
[EnumCase] ->
String ->
SumOfProductEncodingOptions ->
String
prettyTaggedObject parentName anns cases indents SumOfProductEncodingOptions {..} =
prettyTaggedObject parentName tyVars anns ifaces cases indents SumOfProductEncodingOptions {..} =
intercalate
"\n\n"
( cases <&> \case
Expand All @@ -238,29 +240,35 @@ prettyTaggedObject parentName anns cases indents SumOfProductEncodingOptions {..
++ prettyAnnotations (Just caseNm) indents anns
++ indents
++ "data class "
++ toUpperFirst caseNm
++ caseTypeHeader caseNm
++ "(val "
++ contentsFieldName
++ ": "
++ prettyMoatType caseTy
++ ") : "
++ parentName
++ parentTypeHeader
++ "()"
EnumCase caseNm caseDoc [] ->
prettyTypeDoc indents caseDoc []
++ prettyAnnotations (Just caseNm) indents anns
++ indents
++ "data object "
++ toUpperFirst caseNm
++ caseTypeHeader caseNm
++ " : "
++ parentName
++ parentTypeHeader
++ "()"
EnumCase caseNm _ _ ->
error $
"prettyTaggedObject: The data constructor "
<> caseNm
<> " can have zero or one concrete type constructor!"
)
where
caseTypeHeader :: String -> String
caseTypeHeader name = prettyMoatTypeHeader (toUpperFirst name) (addTyVarBounds tyVars ifaces)

parentTypeHeader :: String
parentTypeHeader = prettyMoatTypeHeader parentName tyVars

prettyEnum ::
() =>
Expand Down Expand Up @@ -325,7 +333,7 @@ prettyEnum doc anns ifaces name tyVars cases sop@SumOfProductEncodingOptions {..
++ classTyp
++ prettyInterfaces ifaces
++ " {\n"
++ prettyTaggedObject name anns cases indents sop
++ prettyTaggedObject name tyVars anns ifaces cases indents sop
++ "\n}"
where
isCEnum :: [EnumCase] -> Bool
Expand Down

0 comments on commit ae13803

Please sign in to comment.