Single type annotation

Hi there :slight_smile:

I am a newbie, how you can infer from this question:

RoomID : String is how it works in my imagination.

Why does it work otherwise? :smile:

Are you saying that you want RoomId to be an alias for String?

That would look something like this:

type alias RoomId = String

But if RoomId is just a string, you could also declare the record like so:

    { roomId: String
    , details...etc ```
1 Like

When there are syntax errors in the code, it really helps to spell out what you’re trying to do, because it’s hard for other people to understand the intention behind the code otherwise. Can you confirm what you are trying to do here?

My guess is you are trying to wrap a string in a custom type so that you can’t pass any old string instead of a room ID? The rest of this answer assumes that.

The correct syntax is:

type RoomId = RoomId String

The first RoomId is the name of a type, and the second RoomId is a type constructor.

You could also write:

type RoomId = RoomIdConstructor String

In Elm, types and type constructors exist in different namespaces, so you can use the same name for both and the compiler never gets them mixed up. People often do this. It saves you thinking of another name, and it’s so common in Elm code that you get used to it quickly. Confusing for beginners though!

You can also use the type constructor as a function. You apply it to a String and it gives you back a RoomId.

So why didn’t your version work?

RoomID : String

The : syntax applies a type to a value. So the compiler expects a value on the left and a type on the right. But you are giving it a type on both sides, so that’s an error.
If you actually wanted to say “all values of type RoomID are also type String”, then this would mean RoomID is just an alias for String, and you would use the type alias syntax, as @DanAbrams mentioned.
Hope this helps, feel free to ask further questions otherwise! :slight_smile:

1 Like

Well, I wanted to declare the type RoomID as a String, if that makes sense.
As it turns out, there was an underlying issue:

A union type is a correct technique here, so:

type RoomId 
    = RoomId String
1 Like

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