Skip to content
This repository was archived by the owner on Aug 17, 2022. It is now read-only.
This repository was archived by the owner on Aug 17, 2022. It is now read-only.

Fixed-length lists #146

Closed
Closed

Description

@badeend

This is definately in the Could-Have category.

I have the following type defined in Rust:

type Ipv6Address = [u8; 16];

As this proposal currently stands, I'll have to resort to either a variable length list,

(list u8)

or (and hold on to your hat)

(record
	(byte_0 u8)
	(byte_1 u8)
	(byte_2 u8)
	(byte_3 u8)
	(byte_4 u8)
	(byte_5 u8)
	(byte_6 u8)
	(byte_7 u8)
	(byte_8 u8)
	(byte_9 u8)
	(byte_10 u8)
	(byte_11 u8)
	(byte_12 u8)
	(byte_13 u8)
	(byte_14 u8)
	(byte_15 u8)
)

Either one will work, but neither one pleasantly captures the intent.

Suggestion: add an optional length annotation to lists:

(list <intertype> (length <size>)?)

where <size> is an exact size. The list can never be shorter or longer. This might enable additional optimizations in the glue code.


I'm aware my specific IPv6 example could also be described using a pair of u64s, but that isn't a generic solution.

Activity

  1. lukewagner commented on Jan 26, 2022

    @lukewagner
    Member

    Hm, yeah, I see your point. I suppose we could think of fixed-length lists as another specialization (e.g., as optional is a specialization of variant).

  2. rossberg commented on Jan 26, 2022

    @rossberg
    Member

    I'd say this is in the same category as not having e.g. integer subrange types. If you know your value range of a parameter is 0..9999, you still have to use u16 or something (or a quadruple of decimal digits :) ). You can't express intent more specifically.

    The reason low-level languages like C or Rust include the array size in their types is mostly because their representations depend on it. In higher-level languages that is not typically the case and the size it is not normally included. And I would count interface types among those higher-level type systems.

    Attempts to allow capturing domain-specific semantics beyond mere representation are a huge can of worms for a type system, because there will always be the next case that you cannot express – e.g., what if the length of the list is given by another parameter? There are solutions to this, but none that you'd want to put into a cross-language IDL.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions