@@ -106,11 +106,12 @@ declToJs opts mp (BindingGroupDeclaration vals) e = do
106106 js <- valueToJs opts mp e val
107107 return $ JSVariableIntroduction (identToJs ident) (Just js)
108108 return $ Just jss
109−declToJs _ _ (DataDeclaration _ _ ctors) _ = do
109+declToJs _ mp (DataDeclaration _ _ ctors) e = do
110110 return $ Just $ flip concatMap ctors $ \(pn@(ProperName ctor), tys) ->
111− [ makeConstructor ctor (length tys)
112− , JSAssignment (JSAccessor "create" (JSVar ctor)) (go pn 0 (length tys) [])
113− ]
111+ let propName = if isNullaryConstructor e (Qualified (Just mp) pn) then "value" else "create"
112+ in [ makeConstructor ctor (length tys)
113+ , JSAssignment (JSAccessor propName (JSVar ctor)) (go pn 0 (length tys) [])
114+ ]
114115 where
115116 makeConstructor :: String -> Int -> JS
116117 makeConstructor ctorName n =
@@ -198,7 +199,9 @@ valueToJs opts m e (ObjectUpdate o ps) = do
198199 obj <- valueToJs opts m e o
199200 sts <- mapM (sndM (valueToJs opts m e)) ps
200201 extendObj obj sts
201−valueToJs _ m _ (Constructor name) = return $ JSAccessor "create" $ qualifiedToJS m (Ident . runProperName) name
202+valueToJs _ m e (Constructor name) =
203+ let propName = if isNullaryConstructor e name then "value" else "create"
204+ in return $ JSAccessor propName $ qualifiedToJS m (Ident . runProperName) name
202205valueToJs opts m e (Case values binders) = do
203206 vals <- mapM (valueToJs opts m e) values
204207 bindersToJs opts m e binders vals
@@ -394,3 +397,8 @@ isOnlyConstructor e ctor =
394397 typeConstructor (Qualified (Just moduleName) _, (tyCtor, _)) = (moduleName, tyCtor)
395398 typeConstructor _ = error "Invalid argument to isOnlyConstructor"
396399
400+isNullaryConstructor :: Environment -> Qualified ProperName -> Bool
401+isNullaryConstructor e ctor =
402+ case fromMaybe (error "Data constructor not found") $ ctor `M.lookup` dataConstructors e of
403+ (_, TypeConstructor{}) -> True
404+ _ -> False