Skip to content

[Feature Request] Allow @unboxed on tagged variants #6547

Closed as not planned
Closed as not planned

Description

@cometkim

Use case: to represent GraphQL union value

type user = {
  id: string,
}

type chat = {
  id: string,
}

type message = {
  id: string,
}

@tag("__typename")
type searchResult =
  | User(user)
  | Chat(chat)
  | Message(message)

let result = User({ id: "123" })

is currently compiled to

var result = {
  __typename: "User",
  _0: {
    id: "123"
  }
}

but what is usually needed is

var result = {
  __typename: "User",
  id: "123"
}

Activity

  1. zth commented on Jan 3, 2024

    @zth
    Member

    You can use an inline record to achieve that runtime representation.

  2. cometkim commented on Jan 3, 2024

    @cometkim
    MemberAuthor

    I'm building a code generator, so I just wanna refer an existing type by its name. Is it not the common case?

  3. zth commented on Jan 3, 2024

    @zth
    Member

    There's a bug in the syntax right now that prevents you from doing this without prepending a dummy field, but this will be possible as soon as that bug is fixed:

    type user = {
      id: string,
    }
    
    type chat = {
      id: string,
    }
    
    type message = {
      id: string,
    }
    
    @tag("__typename")
    type searchResult =
      | User({...user})
      | Chat({...chat})
      | Message({...message})
    
    let result = User({ id: "123" })

    This doesn't let you go back to the user type etc easily though since you can't coerce an inline record. But it does solve the problem.

  4. cometkim commented on Jan 3, 2024

    @cometkim
    MemberAuthor

    It does only guarantee the output structure is the same, and it does not point to the same type(name). Do I understand correctly?

  5. zth commented on Jan 3, 2024

    @zth
    Member

    Correct, so it's not the same type, although they're identical (but one is inline and one is not) which limits things a bit. But when it's readonly, and especially in the context of GraphQL unions etc, I think it might be enough.

  6. cometkim commented on Jan 3, 2024

    @cometkim
    MemberAuthor

    The record definition spread is useful for forcing not to reuse value type in ReScript. But when interacting with structurally typed language, I often want the opposite.

    In the example (and its imaginable use case), I need to be able to spread user data into the actual payload. It would be much more convenient if this could be achieved without copying all fields.

    I'd like to mention this (#5207 (comment)) from the original request. We can say the current behavior is a kind of hybrid (internal tag or adjacent tag), but that is chosen by the compiler.

    One way to make this possible without @unboxed is to give the user even more control over the runtime representation.

    For example, @tag.internal("name") and @tag.adjacent("name")

    `

  7. github-actions commented on Dec 29, 2024

    @github-actions

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

  8. added
    stale
    on Dec 29, 2024
  9. removed
    stale
    on Dec 31, 2024
  10. stephanoskomnenos commented on May 27, 2025

    @stephanoskomnenos
    Contributor

    This feature is useful for creating js bindings.

  11. github-actions commented on May 23, 2026

    @github-actions

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

  12. added
    stale
    on May 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementstale

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions