Re: [elixir-core:8371] Re: Runtime typespecs assertion

2018-11-07 Thread Sergiy Kukunin
I afraid you missed my point, I might have expressed it poorly. Let's assume I have a simple type: {is_atom(), is_number(), is_binary()}. I want to define a guard to match it. Without reusing I can write a function accepting it: func({x, y, z}) when is_atom(x) and is_number(y) and

Re: [elixir-core:8372] Re: Runtime typespecs assertion

2018-11-07 Thread Sergiy Kukunin
Actually, that what I understood only in my last message - I can implement it right now. I'm pretty new to Elixir, so that wasn't obvious to me. Currently, it seems it's resolved, there are only suggestions to improve syntax, that are too minor. Thank everyone for assistance On Wed, Nov 7, 2018

Re: [elixir-core:8367] Re: Runtime typespecs assertion

2018-11-07 Thread Louis Pilfold
Hi all The desire for more safety in Elixir is reasonable, both at compile time and at runtime. The core team have previously experimented with introducting a compile time type checking system, and we also have the dialyser and gradualizer tools that can be used with Elixir. Checking at runtime

Re: [elixir-core:8369] Re: Runtime typespecs assertion

2018-11-07 Thread Sergiy Kukunin
Thanks for the answers. Just want to note, that I don't want to invent type system such as in statically typed languages. I mean more about defining schemas we can check different values with. All pattern matching, guards and typespec might work for this. Furthermore, it would be cool to make

Re: [elixir-core:8369] Re: Runtime typespecs assertion

2018-11-07 Thread Louis Pilfold
Hi Sergiy The functionality you've described can be implemented with macros, no need to modify Elixir or Erlang. To start it could be as simple as defining guards that assert nothing in the production environment. defmodule Test do if Mix.env() == :prod do defguard is_my_type(x) when true

Re: [elixir-core:8371] Re: Runtime typespecs assertion

2018-11-07 Thread Louis Pilfold
Hi Sergiy I'm afraid I don't follow. From what I understand of your proposal the current defguard system meets your needs- what are you looking to add? Cheers, Louis On Wed, 7 Nov 2018 at 18:38 Sergiy Kukunin wrote: > I afraid you missed my point, I might have expressed it poorly. Let's >

Re: [elixir-core:8374] Re: Runtime typespecs assertion

2018-11-07 Thread Louis Pilfold
Hey The implementation you've given there is expensive and only works for lists up to a certain length. To solve this one you'll need to step outside of guard clauses as they only support a limited subset of Elixir/Erlang. The idea is that all operations in guards are very fast and run in

Re: [elixir-core:8373] Re: Runtime typespecs assertion

2018-11-07 Thread Sergiy Kukunin
Found another problem: can't express "list of strings" in guards nor pattern matching. It's an easy task for typespecs `[String.t(), ...]`, but I can't check typespecs in runtime. Found a very dirty hack, that works for lists up to 5 strings, enjoy: defguardp is_list_of_strings(x) when

Re: [elixir-core:8375] Re: Runtime typespecs assertion

2018-11-07 Thread Sergiy Kukunin
Thanks for the explanation about guards, makes sense. And I totally agree with you, that can be implemented as an assertion at the very begin of function's body. Again, the question - is there a way to leverage typespecs? Is there a way to implement something like this "hey elixir, is this value

Re: [elixir-core:8376] Re: Runtime typespecs assertion

2018-11-07 Thread Louis Pilfold
Hey I don't believe that exists, at least not in the standard distribution. Perhaps there is a library that provides this functionality. Cheers, Louis On Wed, 7 Nov 2018, 23:05 Sergiy Kukunin, wrote: > Thanks for the explanation about guards, makes sense. And I totally agree > with you, that

[elixir-core:8366] Re: Runtime typespecs assertion

2018-11-07 Thread Sergiy Kukunin
I mistook, sorry: To avoid performance penalty we could enable the typespec runtime asserts only for development and testing environments On Wednesday, November 7, 2018 at 1:00:53 PM UTC+2, Sergiy Kukunin wrote: > > Hello there. This is my first message to the elixir group. Thanks for the >

[elixir-core:8365] Runtime typespecs assertion

2018-11-07 Thread Sergiy Kukunin
Hello there. This is my first message to the elixir group. Thanks for the great language. While I'm writing my code, I want to make functions to be safer. It's bad practice if a function accepts unexpected input and pass it further, and it blows in a completely different part of a system. At

[elixir-core:8367] Re: Runtime typespecs assertion

2018-11-07 Thread Ivan Yurov
If you want type-safety why not to just pick a strongly typed language, like Ocaml for example? Elixir is bound to Erlang VM and will never provide any features like you're describing that are not supported by Erlang. And I don't think type-checking ever happens at runtime in any language. On