Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implicits as constructor arguments #48

Open
yallop opened this issue Feb 20, 2016 · 3 comments
Open

Implicits as constructor arguments #48

yallop opened this issue Feb 20, 2016 · 3 comments

Comments

@yallop
Copy link
Contributor

yallop commented Feb 20, 2016

It'd be convenient to have implicits as constructor arguments so that we could write

type showable = Show : {S: SHOW} * S.t -> showable;;
let f (Show ({M}, x)) = show x

It's currently possible to achieve something similar with first-class modules in a few cases:

type showable = Show : (module SHOW with type t = 'a) * 'a -> showable
let f (Show ((module M), x)) = let implicit module M = M in show x

but this technique doesn't work for higher-kinded examples, besides being rather cumbersome.

@lpw25
Copy link
Member

lpw25 commented Feb 20, 2016

I agree this would be a good idea. Syntactically, what about:

type showable = Show : {S: SHOW} -> S.t -> showable;;
let f (Show{M}(x)) = show x

@yallop
Copy link
Contributor Author

yallop commented Feb 20, 2016

Yes, that works. As a pleasant side effect, this also introduces a way to bind existential type variables in patterns, if you don't mind passing the types explictly during construction:

module type T = sig type t end
type t = T: {X:T} -> {Y:T} -> X.t * Y.t -> t

let f (T{X}{Y}(x, y)) =
   (* X.t and Y.t available as names for the types of x and y *)
   ...

f (T{Int}{String}(3, "four"))

Can implicit constructor arguments without value components be optimized away so that they don't have a runtime representation?

@lpw25
Copy link
Member

lpw25 commented Feb 21, 2016

Can implicit constructor arguments without value components be optimized away so that they don't have a runtime representation?

I'm not sure, but probably not. If you made the module type abstract then some users of the type would not be aware that the module contains only types.

@yallop yallop changed the title Implicits as constructors arguments Implicits as constructor arguments Feb 26, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants