@@ -4095,16 +4095,14 @@ module Util =
40954095
40964096 let makeImportBound com ctx moduleName typeName =
40974097 let importName = getLibraryImportName com ctx moduleName typeName
4098- let objectBound = mkTypeTraitGenericBound [ importName ] None
4099- objectBound
4098+ mkTypeTraitGenericBound [ importName ] None
41004099
41014100 let makeRawBound id = makeGenBound [ rawIdent id ] []
41024101
41034102 let makeOpBound op =
41044103 // makes ops type bound, e.g. T: Add(Output=T)
41054104 let ty = mkGenericPathTy [ argName ] None
41064105 let genArgsOpt = mkConstraintArgs [] [ " Output" , ty ]
4107-
41084106 mkTypeTraitGenericBound ( " core" :: " ops" :: op :: []) genArgsOpt
41094107
41104108 let makeConstraint c =
@@ -4117,7 +4115,7 @@ module Util =
41174115 | Operators.division, true -> [ makeOpBound " Div" ]
41184116 | Operators.modulus, true -> [ makeOpBound " Rem" ]
41194117 | Operators.unaryNegation, true -> [ makeOpBound " Neg" ]
4120- | Operators.divideByInt, true -> [ makeOpBound " Div " ; makeGenBound [ rawIdent " From " ] [ " i32 " ] ]
4118+ | Operators.divideByInt, true -> [ makeImportBound com ctx " Native " " DivideByInt " ]
41214119 | " get_Zero" , true -> [ makeRawBound " Default" ]
41224120 | _ -> []
41234121 | Fable.Constraint.CoercesTo( targetType) ->
@@ -5043,6 +5041,7 @@ module Util =
50435041 Operators.multiply, ( " bin_op" , " Mul" , " mul" ) // The multiplication operator *.
50445042 Operators.division, ( " bin_op" , " Div" , " div" ) // The division operator /.
50455043 Operators.modulus, ( " bin_op" , " Rem" , " rem" ) // The remainder operator %.
5044+ Operators.divideByInt, ( " div_int_op" , " DivideByInt" , " divide_by_int" ) // The integer division operator DivideByInt.
50465045
50475046 Operators.bitwiseAnd, ( " bin_op" , " BitAnd" , " bitand" ) // The bitwise AND operator &.
50485047 Operators.bitwiseOr, ( " bin_op" , " BitOr" , " bitor" ) // The bitwise OR operator |.
@@ -5099,6 +5098,55 @@ module Util =
50995098 )
51005099 )
51015100
5101+ let makeDefaultTraitImpls
5102+ com
5103+ ctx
5104+ ( ent : Fable.Entity )
5105+ entName
5106+ genArgs
5107+ ( members : ( Fable.MemberDecl * Fable.MemberFunctionOrValue ) list )
5108+ =
5109+ // For value types that are not defaultable (e.g. unions),
5110+ // generate a Default impl delegating to the static Zero member:
5111+ //
5112+ // impl<...> core::default::Default for EntName<...> {
5113+ // fn default() -> Self { Self::get_Zero() }
5114+ // }
5115+ let needsExplicitDefault =
5116+ ent.IsValueType && not ( ent |> isDefaultableEntity com Set.empty)
5117+
5118+ if not needsExplicitDefault then
5119+ []
5120+ else
5121+ members
5122+ |> List.tryPick ( fun ( decl , memb ) ->
5123+ if
5124+ not memb.IsInstance
5125+ && ( memb.CompiledName = " get_Zero"
5126+ || memb.CompiledName = " Zero"
5127+ || decl.Name = " get_Zero" )
5128+ then
5129+ let zeroMemberName = Fable.Naming.splitLast decl.Name
5130+ let bodyExpr = makeCall [ " Self" ; zeroMemberName ] None []
5131+ let fnBody = bodyExpr |> mkExprBlock |> Some
5132+
5133+ let fnDecl =
5134+ let output = mkGenericPathTy [ " Self" ] None |> mkFnRetTy
5135+ mkFnDecl [] output
5136+
5137+ let fnKind = mkFnKind DEFAULT_ FN_ HEADER fnDecl NO_ GENERICS fnBody
5138+ let fnItem = mkFnAssocItem [] " default" fnKind
5139+
5140+ let path =
5141+ mkGenericPath ( " core" :: " default" :: ( rawIdent " Default" ) :: []) None None
5142+
5143+ let ofTrait = mkTraitRef path |> Some
5144+ [ fnItem ] |> makeTraitImpl com ctx entName genArgs ofTrait |> Some
5145+ else
5146+ None
5147+ )
5148+ |> Option.toList
5149+
51025150 let withCurrentScope ctx ( usedNames : Set < string >) f =
51035151 let ctx =
51045152 { ctx with UsedNames = { ctx.UsedNames with CurrentDeclarationScope = HashSet usedNames } }
@@ -5224,6 +5272,9 @@ module Util =
52245272 ( baseTypeOpt, " &self." + baseName)
52255273 ||> makeDerefTraitImpls com ctx entName genArgs
52265274
5275+ let defaultTraitImpls =
5276+ nonInterfaceMembers |> makeDefaultTraitImpls com ctx ent entName genArgs
5277+
52275278 let displayTraitImpls =
52285279 let hasToString =
52295280 nonInterfaceMembers |> List.exists ( fun ( d , m ) -> m.CompiledName = " ToString" )
@@ -5283,8 +5334,9 @@ module Util =
52835334 |> makeInterfaceTraitImpls com ctx entName genParams ifcEntRef ifcGenArgs
52845335 )
52855336
5286- derefTraitImpls
5287- @ nonInterfaceImpls
5337+ nonInterfaceImpls
5338+ @ derefTraitImpls
5339+ @ defaultTraitImpls
52885340 @ displayTraitImpls
52895341 @ operatorTraitImpls
52905342 @ refEqualityTraitImpls
0 commit comments