Skip to content

Commit bc39f83

Browse files
committed
Add more fidelity to type errors.
1 parent e3fded6 commit bc39f83

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

Graphics/Implicit/ExtOpenScad/Primitives.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,14 @@ import Linear.Affine (qdA)
6464

6565
import System.Directory (doesFileExist)
6666

67+
import Type.Reflection (Typeable)
68+
6769
default ()
6870

6971
-- FIXME: `defaultTo` is used inconsistently. The line between defaults and examples is a bit blurry.
7072

7173
-- | Use the old syntax when defining arguments.
72-
argument :: OTypeMirror desiredType => Text -> ArgParser desiredType
74+
argument :: (OTypeMirror desiredType, Typeable desiredType) => Text -> ArgParser desiredType
7375
argument a = GIEUA.argument (Symbol a)
7476

7577
-- | The only thing exported here. basically, a list of modules.

Graphics/Implicit/ExtOpenScad/Util/ArgParser.hs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
-- Allow us to use string literals for Text
99
{-# LANGUAGE OverloadedStrings #-}
1010

11+
-- Allow us to display a target type.
12+
{-# LANGUAGE TypeApplications #-}
13+
1114
module Graphics.Implicit.ExtOpenScad.Util.ArgParser (
1215
argMap,
1316
argument,
@@ -40,6 +43,8 @@ import Data.Maybe (isNothing, fromJust, isJust)
4043

4144
import Data.Text.Lazy (Text, pack, unpack)
4245

46+
import Type.Reflection (Typeable, typeRep)
47+
4348
import Control.Arrow (first)
4449

4550
-- * ArgParser building functions
@@ -48,7 +53,7 @@ import Control.Arrow (first)
4853

4954
-- | Builds an argparser for the type that is expected from it.
5055
-- FIXME: make a version of this that accepts multiple symbol names, so we can have h= and height=
51-
argument :: forall desiredType. (OTypeMirror desiredType) => Symbol -> ArgParser desiredType
56+
argument :: forall desiredType. (OTypeMirror desiredType, Typeable desiredType) => Symbol -> ArgParser desiredType
5257
argument name =
5358
AP name Nothing "" $ \oObjVal -> do
5459
let
@@ -58,7 +63,9 @@ argument name =
5863
errmsg = case oObjVal of
5964
OError err -> "error in computing value for argument " <> pack (show name)
6065
<> ": " <> err
61-
_ -> "arg " <> pack (show oObjVal) <> " not compatible with " <> pack (show name)
66+
_ -> "arg " <> pack (show name) <>
67+
" expected " <> pack (show $ typeRep @desiredType) <>
68+
" but found " <> oTypeStr oObjVal
6269
maybe (APFail errmsg) APTerminator val
6370
{-# INLINABLE argument #-}
6471

0 commit comments

Comments
 (0)