Stream: beginners

Topic: How to share data types across multiple files


view this post on Zulip Monica (Jun 18 2024 at 18:36):

I have the following:

module [parseNumber]

import JsonData

parseNumber : Str -> Result JsonData [InvalidNumStr, BadUtf8 _ _]
parseNumber = \input ->
    Str.toU64 input
    |> Result.map \n -> Number n

expect parseNumber "123" == Ok (Number 123)

I'm getting an error to say "Nothing is named JsonData in this scope." on the return type of parseNumber.
I have my data type as a module, which exposes the tag union called JsonData
What am I doing wrong?
cheers :)

view this post on Zulip Brendan Hansknecht (Jun 18 2024 at 18:42):

What is the name of the module containing JsonData? I think you need to do import Module.JsonData

view this post on Zulip Monica (Jun 18 2024 at 18:45):

It's called JsonData.roc

view this post on Zulip Monica (Jun 18 2024 at 18:46):

module [JsonData]

JsonData : [
    String Str,
    Number I64,
    Object (List (Str, JsonData)),
]

This is my module

view this post on Zulip Monica (Jun 18 2024 at 18:47):

I basically want to say my return type of parseNumber is Number

view this post on Zulip Akeshihiro (Jun 18 2024 at 19:50):

This seems to work:

module [parseNumber]

import JsonData exposing [JsonData]

# Alternative (if using `import` without `exposing`:
# parseNumber : Str -> Result JsonData.JsonData [InvalidNumStr, BadUtf8 _ _]
parseNumber : Str -> Result JsonData [InvalidNumStr, BadUtf8 _ _]
parseNumber = \input ->
    Str.toI64 input
    |> Result.map \n -> Number n

expect parseNumber "123" == Ok (Number 123)

view this post on Zulip Monica (Jun 18 2024 at 21:32):

thanks that worked!


Last updated: Jul 05 2025 at 12:14 UTC