You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Even though both properties are specified as non_neg_integer().
@QuinnWilton pointed me to the TypeClass helpers as means of doing some checks at compile time.
Would it be possible or make sense to create a TypeClass that validated that those struct porperties should be integer values?
[details="Spoiler"] My current guess is that it would be overkill/misuse of type classes since these are for checking the "semantics" of a module via property checking and not necessarily data checking.
[/details]
@creminology:
I haven't used TypeClass yet, but have been meaning to try it.
I think my initial assessment for TypeClass library was a bit sidetracked… I think TypeClass is more interested in validating that your protocols conform to a set of invariats via property tests; In that sense it has to do more with “behaviour” than “type-checks”.
Your examples are very helpful since implementing a TypeClass requires data generation, for which Norm is of great help.
But I was wondering if by any means Witchcraft may help at checking structs at runtime/complie time?
The short answer is no. Elixir does not have great support for type checking today, though you can get some limited checking with typespecs + Dialyzer (including when using the Algae types). Those tools all run as a separate step, and won't stop you from writing ill-typed programs unless you run them. There is some effort going towards adding a more robust type layer to BEAM languages, but nothing is available today.
Would it be possible or make sense to create a TypeClass that validated that those struct porperties should be integer values?
Typeclass isn't meant for type checking — it's more like suped-up protocols. You could absolutely write precondition checking in a constructor function ("smart constructor", which is actually more expressive than most type systems), but that will come with runtime costs in the general case.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
[Ported from the old Witchcrafters Discourse]
@chouzar:
I just started playing with
Algae
and derived some very simple examples from the documentation:Which by itself already creates the respective elixir structs and constructors:
Also, the constructor typespecs:
Which is already super nice 😃.
But I was wondering if by any means Witchcraft may help at checking structs at runtime/complie time?
For example, with
Algae
it is possible to use constructors as:Even though both properties are specified as
non_neg_integer()
.@QuinnWilton pointed me to the TypeClass helpers as means of doing some checks at compile time.
Would it be possible or make sense to create a TypeClass that validated that those struct porperties should be integer values?
[details="Spoiler"]
My current guess is that it would be overkill/misuse of type classes since these are for checking the "semantics" of a module via property checking and not necessarily data checking.
[/details]
@creminology:
I haven't used TypeClass yet, but have been meaning to try it.
Last year, I did successfully got Witchcraft working with Norm for runtime checks:
elixir-toniq/norm#30 (comment)
@chouzar:
Thank you so much for sharing @creminology 😁
I think my initial assessment for TypeClass library was a bit sidetracked… I think TypeClass is more interested in validating that your protocols conform to a set of invariats via property tests; In that sense it has to do more with “behaviour” than “type-checks”.
Your examples are very helpful since implementing a TypeClass requires data generation, for which Norm is of great help.
@expede:
🙌
The short answer is no. Elixir does not have great support for type checking today, though you can get some limited checking with typespecs + Dialyzer (including when using the Algae types). Those tools all run as a separate step, and won't stop you from writing ill-typed programs unless you run them. There is some effort going towards adding a more robust type layer to BEAM languages, but nothing is available today.
Typeclass isn't meant for type checking — it's more like suped-up protocols. You could absolutely write precondition checking in a constructor function ("smart constructor", which is actually more expressive than most type systems), but that will come with runtime costs in the general case.
Beta Was this translation helpful? Give feedback.
All reactions