Skip to content
This repository has been archived by the owner on Nov 13, 2023. It is now read-only.

Add support for exporting references. #238

Merged
merged 1 commit into from
Aug 22, 2019
Merged

Conversation

cristianoc
Copy link
Collaborator

Fixes #237

@cristianoc
Copy link
Collaborator Author

The PR contains examples on how to export a reference as an abstract type, in case the argument type of the reference requires conversion:

// Abstract version of references: works when conversion is required.

module R: {
  [@genType]
  type t('a);
  let get: t('a) => 'a;
  let make: 'a => t('a);
  let set: (t('a), 'a) => unit;
} = {
  type t('a) = ref('a);
  let get = r => r.contents;
  let make = ref;
  let set = (r, v) => r.contents = v;
};

[@genType]
type t('a) = R.t('a);

[@genType]
let get = R.get;

[@gentype]
let make = R.make;

[@genType]
let set = R.set;

type requiresConversion = {x: int};

// Careful: conversion makes a copy and destroys the reference identity.
[@genType]
let destroysRefIdentity = (x: ref(requiresConversion)) => x;

// Using abstract references preserves the identity.
[@genType]
let preserveRefIdentity = (x: R.t(requiresConversion)) => x;

@cristianoc cristianoc merged commit c9df407 into master Aug 22, 2019
@cristianoc cristianoc deleted the export_references branch September 25, 2019 08:15
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support to export refs.
1 participant