Even if string literals were allowed, the proposed spec wouldn’t be accepted by
dialyzer. Dialyzer does not support overloaded specs with overlapping
arguments. In this case “account_created” is a subtype of String.t(), so the
spec would be rejected.
You can see by having a similar spec using atoms:
-spec to_struct
(account_created) -> {account_created};
(account_cloded) -> {account_closed};
(atom()) -> no_return().
Produces the following diagnostic from dialyzer:
test.erl:5:2: Overloaded contract for test:to_struct/1 has overlapping domains;
such contracts are currently unsupported and are simply ignored
Michał.
From: [email protected] <[email protected]> on
behalf of Yordis Prieto <[email protected]>
Date: Sunday, 13 February 2022 at 22:55
To: elixir-lang-core <[email protected]>
Subject: [elixir-core:10777] [Proposal] Allow String Literal in the TypeSpec
I would like to be able to do the following in my typespec:
```
@spec to_struct("account_created") :: %AccountCreated{}
@spec to_struct("account_closed") :: %AccountClosed{}
@spec to_struct(String.t()) :: no_return()
@spec to_string(%AccountCreated{}) :: "account_created"
@spec to_string(%AccountClosed{}) :: "account_closed"
@spec to_string(struct()) :: no_return()
```
This will help the documentation much better, while also keeping the typespec
closer to what the intention was.
--
You received this message because you are subscribed to the Google Groups
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to
[email protected]<mailto:[email protected]>.
To view this discussion on the web visit
https://groups.google.com/d/msgid/elixir-lang-core/147aa05a-be3d-4f32-aab8-13a1938341b0n%40googlegroups.com<https://groups.google.com/d/msgid/elixir-lang-core/147aa05a-be3d-4f32-aab8-13a1938341b0n%40googlegroups.com?utm_medium=email&utm_source=footer>.
--
You received this message because you are subscribed to the Google Groups
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/elixir-lang-core/AS8P192MB136745C18049D851403F563DFA359%40AS8P192MB1367.EURP192.PROD.OUTLOOK.COM.