Maybe Add A Warning For Including Commas In ~w List

61 views
Skip to first unread message

Onorio Catenacci

unread,
Jun 22, 2016, 8:06:16 AM6/22/16
to elixir-lang-core
Hi all,

Consider the following code:

defmodule WSigilTest do
  def t1 do
    ~w(abc def ghi)
  end

  def t2 do
    ~w(abc, def, ghi)
  end
end

I was working with some Ecto code and had a changeset and rather foolishly used the form in the function t2 rather than the form in t1.  In case anyone else isn't clear on the difference:

iex(3)> WSigilTest.t1
WSigilTest.t1
["abc", "def", "ghi"]
iex(4)> WSigilTest.t2
WSigilTest.t2
["abc,", "def,", "ghi"]

Notice the comma appended to each string in t2?  I didn't. :)

I realize that we need to be careful of syntax, of course, but would it be possible to maybe add a compiler warning when someone includes commas in the string passed to ~w?  I would be glad to see about creating a PR for this but I first wanted to see if there's even any interest in the idea.  Maybe it's just me since I don't have a background in Ruby; I guess the ~w comes pretty much straight from Ruby.  

Please let me know your thoughts on this.

--
Onorio


Augie De Blieck Jr.

unread,
Jun 22, 2016, 8:18:02 AM6/22/16
to elixir-l...@googlegroups.com
A minor point, but I can't pass up the opportunity to reference Perl:
The ~w goes back further than even Ruby. Perl (which was really Ruby
v0.1 ;-) has qw// which "quotes words" in a similar way. And I'm
sure Perl probably got it from sed or awk or somewhere.

A warning her isn't a bad idea, but I'm sure there's some annoying
edge case that would make coding it tricky. Since when has that ever
stopped us?!? ;-)

-Augie
> --
> 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 elixir-lang-co...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/elixir-lang-core/06431f8a-387c-48a1-9156-a1a33e1dc0d3%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Henrik N

unread,
Jul 11, 2016, 3:05:09 PM7/11/16
to elixir-lang-core
Is there precedent for warnings in Elixir about things that could be a mistake but could also be a valid use case?

Onorio Catenacci

unread,
Jul 11, 2016, 7:49:15 PM7/11/16
to elixir-l...@googlegroups.com
That is sort of the definition of "warning". If it were obviously wrong it would be a compiler error.

Henrik N <hen...@nyh.se> wrote:
>Is there precedent for warnings in Elixir about things that could be a mistake but could also be a valid use case?
>

>--
>You received this message because you are subscribed to a topic in the Google Groups "elixir-lang-core" group.
>To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-lang-core/tmAESH8Nhz8/unsubscribe.
>To unsubscribe from this group and all its topics, send an email to elixir-lang-co...@googlegroups.com.
>To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/77dc7cd3-07b4-4ef1-8dfa-587587feb0ee%40googlegroups.com.

Louis Pilfold

unread,
Jul 11, 2016, 8:03:15 PM7/11/16
to elixir-l...@googlegroups.com

Hey

How would I silence the warnings emitted when I have deliberate commas in a w sigil?

Cheers,
Louis

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 elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/xmbipso4kehogg28mex2s1j3.1468280949540%40email.android.com.

Onorio Catenacci

unread,
Jul 11, 2016, 8:09:36 PM7/11/16
to elixir-l...@googlegroups.com

That is part of the discussion. 

I am not probably as familiar with the use cases of sigil as others might be but I get the impression that including commas in a ~w sigil is probably a pretty rare use case. And we are discussing a _warning_ not an error.


Henrik Nyh

unread,
Jul 12, 2016, 2:17:48 AM7/12/16
to elixir-l...@googlegroups.com
I ask because the ones I can think of (unused variables, assignment in conditional) are such that if you heed the warning, the behaviour of your code won't change. So there's never (?) a case where you can't just do what it says to get rid of it, without breaking your code.

Whereas heeding this warning would break any code with intended commas in that sigil, requiring it to be rewritten without the sigil. Though I guess that could be part of the message: "… If the commas are intended, use this other construct instead". (I don't like that idea, though.)

Sent from my iPhone
> To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/xmbipso4kehogg28mex2s1j3.1468280949540%40email.android.com.

Josh Adams

unread,
Jul 12, 2016, 8:25:37 AM7/12/16
to elixir-l...@googlegroups.com

I believe this sort of thing belongs in the realm of linters honestly, c.f. Rubocop. But really good idea. Just my twopence.


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 elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/9A59F3CC-4BA4-4A9D-80B4-9A1B62E6E72E%40nyh.se.

Tallak Tveide

unread,
Jul 12, 2016, 2:00:45 PM7/12/16
to elixir-lang-core
I wouldnt emit a warning here as adding commas should be perfectly valid. How would we deal with the use case where you wanted to include the commas?

Wiebe-Marten Wijnja

unread,
Jul 12, 2016, 4:18:22 PM7/12/16
to elixir-lang-core
Declaring variables without underscores in their names as function parameters but then not using them inside the function is also perfectly valid, but a warning is thrown.

I can think of no use-case of ~w{} that involves commas at the end of the words. If you combine commas at the end of words with ~w{}, your code is very easy to be misinterpreted by other people.

Onorio Catenacci

unread,
Jul 12, 2016, 4:52:14 PM7/12/16
to elixir-lang-core

That's a great point.  The whole idea of warnings is to call attention to syntactically valid code which is probably not what the developer intended.  The canonical example of warnings in C code is

if  (x = 1)
{
/* Do something */
}

For those of you unfamiliar with C that code is valid but instead of comparing x to 1 it assigns 1 to x which is always true.  As I say it's perfectly legal code and very easily overlooked but probably not what the developer wants to happen. Warnings are meant to call out code that may be unintentionally incorrect.

To Josh's earlier point - -  yes this could be caught by a linter.  I don't have a strong feeling either way and now that I have made that mistake once I probably won't make it again. I was thinking of others who might make this mistake in the future.


--
You received this message because you are subscribed to a topic in the Google Groups "elixir-lang-core" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-lang-core/tmAESH8Nhz8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elixir-lang-co...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages