Debug.todo for constants

Since the old Debug.crash was replaced by Debug.todo the intention is clear that this is made for development purposes. And in that sense it is extremely useful when prototyping or making top-down progress on some code. The idea being that as long as your scenario does not get in the todo branch, you can make progress and test your progress in your app. This assumption breaks however as soon as Debug.todo is used for a constant.

module ToBeDone exposing (complicatedCode)

complicatedCode bool =
    if bool then
        "happy path"
    else
        tbd

tbd =
    Debug.todo "Constant not coded yet"

Whatever you do with the above code, even if you never call complicatedCode False it will crash at the start of the application. This is because tbd is a constant and thus evaluated on start.

I have a use case for teaching material where we provide a template files containing Debug.todos and a test file checking that the todos are replaced with valid code. But if we have a Debug.todo for one of those constants like we used to have here, elm-test would crash instead of reporting a failure for the test calling the todo constant, which is not very convenient for learning material.

I’d love if it would be possible to have constant Debug.todo that behave like for functions, crashing only when called.

Just make it a function:

module ToBeDone exposing (complicatedCode)

complicatedCode bool =
    if bool then
        "happy path"
    else
        tbd 1

tbd _ =
    Debug.todo "Constant not coded yet"
1 Like

Indeed, that solves the “developing” part. Not very clean for learning material though. Is it possible in other languages? I know that the todo!() macro does not compile if used in a const ... definition in Rust.

What about a good ol’ TODO comment?

tbd =
    -- TODO: Constant not decided yet
    0

That wouldn’t do it for “complex” constants. For example, we used to have

defaultRobot : Robot
defaultRobot =
    Debug.todo "Please implement this"

And now we just give the answer for that one

defaultRobot : Robot
defaultRobot =
    { bearing = North
    , coordinates = { x = 0, y = 0 }
    }

and it’s just not part of the things being tested anymore, students directly work on the rest of the exercise.

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.