@@ -99,66 +99,64 @@ let caseName (case: FSharpUnionCase) =
9999 else
100100 entity.CompiledName + " _" + case.Name
101101
102-
103- let convertUnion ( ctx : PhpCompiler ) ( info : Fable.UnionConstructorInfo ) =
104- if info.Entity.UnionCases.Count = 1 then
105- let case = info.Entity.UnionCases.[ 0 ]
106- [ let t =
107- { Name = case.Name
108- Fields = [ for e in case.UnionCaseFields do
109- { Name = e.Name
110- Type = convertType e.FieldType } ]
111- Methods = [
112- { PhpFun.Name = " get_FSharpCase"
113- PhpFun.Args = []
114- PhpFun.Matchings = []
115- PhpFun.Static = false
116- PhpFun.Body =
117- [ PhpStatement.Return( PhpConst( PhpConstString( case.Name)))] }
118- { PhpFun.Name = " CompareTo"
119- PhpFun.Args = [ " other" ]
120- PhpFun.Matchings = []
121- PhpFun.Static = false
122- PhpFun.Body =
123- [ for e in case.UnionCaseFields do
124- let cmp = PhpVar( ctx.MakeUniqueVar " cmp" , None)
125- match e.FieldType.TypeDefinition.CompiledName with
126- | " int" ->
127- Assign( cmp,
128- PhpTernary( PhpBinaryOp( " >" ,
129- PhpProp( PhpVar( " this" , None), Prop.Field { Name = e.Name; Type = convertType e.FieldType }, None),
130- PhpProp( PhpVar( " other" , None), Prop.Field { Name = e.Name; Type = convertType e.FieldType }, None) ),
131- PhpConst( PhpConstNumber 1. ),
132- PhpTernary(
133- PhpBinaryOp( " <" ,
134- PhpProp( PhpVar( " this" , None), Prop.Field { Name = e.Name; Type = convertType e.FieldType }, None),
135- PhpProp( PhpVar( " other" , None), Prop.Field { Name = e.Name; Type = convertType e.FieldType }, None)),
136- PhpConst( PhpConstNumber - 1. ),
137- PhpConst( PhpConstNumber 0. )
138-
139-
140- ) ) )
141- | _ ->
142- Assign( cmp,
143- PhpMethod( PhpProp( PhpVar( " this" , None), Prop.Field { Name = e.Name; Type = convertType e.FieldType }, None),
144- " CompareTo" ,
145- [ PhpProp( PhpVar( " other" , None), Prop.Field { Name = e.Name; Type = convertType e.FieldType }, None) ])
102+ let convertSingleCaseUnion ( ctx : PhpCompiler ) ( case : FSharpUnionCase ) =
103+ [ let t =
104+ { Name = case.Name
105+ Fields = [ for e in case.UnionCaseFields do
106+ { Name = e.Name
107+ Type = convertType e.FieldType } ]
108+ Methods = [
109+ { PhpFun.Name = " get_FSharpCase"
110+ PhpFun.Args = []
111+ PhpFun.Matchings = []
112+ PhpFun.Static = false
113+ PhpFun.Body =
114+ [ PhpStatement.Return( PhpConst( PhpConstString( case.Name)))] }
115+ { PhpFun.Name = " CompareTo"
116+ PhpFun.Args = [ " other" ]
117+ PhpFun.Matchings = []
118+ PhpFun.Static = false
119+ PhpFun.Body =
120+ [ for e in case.UnionCaseFields do
121+ let cmp = PhpVar( ctx.MakeUniqueVar " cmp" , None)
122+ match e.FieldType.TypeDefinition.CompiledName with
123+ | " int" ->
124+ Assign( cmp,
125+ PhpTernary( PhpBinaryOp( " >" ,
126+ PhpProp( PhpVar( " this" , None), Prop.Field { Name = e.Name; Type = convertType e.FieldType }, None),
127+ PhpProp( PhpVar( " other" , None), Prop.Field { Name = e.Name; Type = convertType e.FieldType }, None) ),
128+ PhpConst( PhpConstNumber 1. ),
129+ PhpTernary(
130+ PhpBinaryOp( " <" ,
131+ PhpProp( PhpVar( " this" , None), Prop.Field { Name = e.Name; Type = convertType e.FieldType }, None),
132+ PhpProp( PhpVar( " other" , None), Prop.Field { Name = e.Name; Type = convertType e.FieldType }, None)),
133+ PhpConst( PhpConstNumber - 1. ),
134+ PhpConst( PhpConstNumber 0. )
135+
146136
147- )
148- If( PhpBinaryOp( " !=" , cmp, PhpConst( PhpConstNumber 0. ) ),
149- [ PhpStatement.Return cmp],
150- []
137+ ) ) )
138+ | _ ->
139+ Assign( cmp,
140+ PhpMethod( PhpProp( PhpVar( " this" , None), Prop.Field { Name = e.Name; Type = convertType e.FieldType }, None),
141+ " CompareTo" ,
142+ [ PhpProp( PhpVar( " other" , None), Prop.Field { Name = e.Name; Type = convertType e.FieldType }, None) ])
143+
151144 )
152- PhpStatement.Return ( PhpConst ( PhpConstNumber 0. ))
153- ]
154- }
155- ]
156- Abstract = false
157- BaseType = None
158- Interfaces = [ PhpUnion.fSharpUnion; Core.icomparable ]
159- }
160- ctx.AddType( t) |> PhpType ]
161- else
145+ If( PhpBinaryOp( " !=" , cmp, PhpConst( PhpConstNumber 0. ) ),
146+ [ PhpStatement.Return cmp],
147+ []
148+ )
149+ PhpStatement.Return ( PhpConst ( PhpConstNumber 0. ))
150+ ]
151+ }
152+ ]
153+ Abstract = false
154+ BaseType = None
155+ Interfaces = [ PhpUnion.fSharpUnion; Core.icomparable ]
156+ }
157+ ctx.AddType( t) |> PhpType ]
158+
159+ let convertMultiCaseUnion ( ctx : PhpCompiler ) ( info : Fable.UnionConstructorInfo ) =
162160 [ let baseType =
163161 { Name = info.Entity.CompiledName
164162 Fields = []
@@ -259,6 +257,13 @@ let convertUnion (ctx: PhpCompiler) (info: Fable.UnionConstructorInfo) =
259257 Interfaces = [ Core.icomparable ] }
260258 ctx.AddType( t) |> PhpType ]
261259
260+
261+ let convertUnion ( ctx : PhpCompiler ) ( info : Fable.UnionConstructorInfo ) =
262+ if info.Entity.UnionCases.Count = 1 then
263+ convertSingleCaseUnion ctx info.Entity.UnionCases.[ 0 ]
264+ else
265+ convertMultiCaseUnion ctx info
266+
262267let convertRecord ( ctx : PhpCompiler ) ( info : Fable.CompilerGeneratedConstructorInfo ) =
263268 [ let t =
264269 { Name = info.Entity.CompiledName
0 commit comments