Skip to content

Adjust color type to make it more safe #746

@jfrolich

Description

@jfrolich

The problem

Now that we have PlatformColor (nice implementation btw), I think we should think about color not being a type alias to string anymore (part reversal of this PR). I would like to gradually start to use platform colors in my app, now all colors are strings, and sometimes I do some manipulation on them (like darken/lighten), of course that wouldn't work on platform colors.

Considered solution

So to make colors safe I think we should make them phantom typed.

type t<'a>
external string: string => t<string> = "%identity"
external get: color<'a> => 'a = "%identity"
...
let blue = Color.string("blue")

let blueString = Color.get(blue)

With platform colors being color<PlatformColor.t>.

In Style.t we can accept all colors (we need a lot of type parameters in the external though)

Alternative solution

An opaque type. The type of the color would be lost, so we can't easily have a get or toString that would raise a type error for a platform color. The upside for this is that we need less type parameters.

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