Skip to content

Please explain what ex_type_runtime might be good for :) #3

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

Open
PragTob opened this issue Apr 29, 2019 · 1 comment
Open

Please explain what ex_type_runtime might be good for :) #3

PragTob opened this issue Apr 29, 2019 · 1 comment

Comments

@PragTob
Copy link

PragTob commented Apr 29, 2019

Reading both this README and the one from ex_type_runtime I'm unsure what it would provide. A little information on that would help a long way!

Thanks for doing this project, sounds interesting. Curious to see where it goes 🎉

@gyson
Copy link
Owner

gyson commented Apr 30, 2019

There is some doc about T module in :ex_type_runtime package at here.

The :ex_type_runtime is used to provide following advanced features:

1. Generic Protocol support with T.p(ProtocolModule, generic_type) Type

Take Enumerable protocol as example. The enumerable integers would be represented as T.p(Enumerable, integer()). Ideally, this should be something like Enumerable.t(integer()) instead, but Enumerable.t/1 does not exist and other type checker like dialyzer would complain if it's been used. Therefore, T.p(Enumerable, x) is used to simulate Enumerable.t(x).

Some example usage of T.p could be found at enum typespec used by ex_type.

2. Intersection Type support with T.&({ type_1, type_2, type3 }) Type

Intersection type is useful to represent some combination of types.

For example, T.&({ Inspect.t, String.Chars.t }) would represent a type that implements both Inspect and String.Chars protocols.

Note: this feature is not complete yet.

3. Type Assertion with T.assert macro

T.assert is used to do type assertion. It supports two operators.

a. T.assert( expr :: type ) is used to cast type. Similar to as in TypeScript.

b. T.assert( expr == expected_type ) is used to ensure the inferred type is the same as expected one. Type check would fail if it's not the same. Note that it won't cause any exception when the program is running regularly (when not doing type checking with ExType).

There is an example usage here.

4. Type Inspection with T.inspect macro

T.inspect is used to inspect inferred type. Like IO.inspect, T.inspect( expr ) will print out the inferred type of expr when ExType is doing type checking. Note that it won't print anything when program is running regularly (when not doing type checking with ExType).

There is an example here.

Conclusion

Basically, above features require some minimal runtime support. If we want to commit code that includes above advanced features, we would need to add :ex_type_runtime as runtime dependency.

p.s. the name of package :ex_type_runtime was originally named as :t (so its consistent with module T). But I found out that hex.pm requires package name to be at least two characters when publishing. Therefore, I rename package name to :ex_type_runtime.

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