defstruct

286 views
Skip to first unread message

Dave Thomas

unread,
Mar 19, 2014, 5:20:47 PM3/19/14
to elixir-l...@googlegroups.com

Would there be any support for extending defstruct so that

defstruct Person, name: "", age: 21

was a shortcut for

defmodule Person
  defstruct name: "", age: 21
end

Josh Adams

unread,
Mar 19, 2014, 5:25:09 PM3/19/14
to elixir-l...@googlegroups.com
I like it.  I do this quite often, as you might guess.  If it's not in the language, there will be a commonly used library of some sort whose sole purpose is to provide a similar macro, I'd bet my arms on it.


--
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.
For more options, visit https://groups.google.com/d/optout.



--
Josh Adams

meh.

unread,
Mar 19, 2014, 5:37:20 PM3/19/14
to elixir-l...@googlegroups.com
Totally agree on this, I thought it was already the case.

José Valim

unread,
Mar 19, 2014, 5:43:45 PM3/19/14
to elixir-l...@googlegroups.com
That won't be the case for now. Structs are different than records. Cases where you define a module simply to define a struct should be more uncommon, I expect it to rather have struct-related functions the majority of the time.

So having only defstruct/1 will help send this message across. After v0.13 is out and we are more familiar with structs, we can re-evaluate the shortcut syntax again.



José Valim
Skype: jv.ptec
Founder and Lead Developer


Juan Uys

unread,
Aug 19, 2014, 5:27:29 PM8/19/14
to elixir-l...@googlegroups.com, da...@pragprog.com
Conversely, I'd like to see 

defmodule Person defstruct do
name: ""
age: 21
end end

Akio Burns

unread,
Dec 27, 2016, 3:33:25 PM12/27/16
to elixir-lang-core, jose....@plataformatec.com.br
Has there been any further consideration on this topic? Structs are great, but they feel severely limited without the ability to easily and cleanly define arbitrarily named structs.

Paul Schoenfelder

unread,
Dec 27, 2016, 3:42:39 PM12/27/16
to elixir-l...@googlegroups.com, José Valim
I definitely wouldn't call this a severe limitation, but it would be a nice-to-have for readability. That said, I haven't actually run into any cases where I would've used the shorthand syntax - as José mentioned in his original reply, the vast majority of the time, structs have functions that go with them, or at the very least, typespecs. 

I do think it's possible for there to be cases where the shorthand syntax would be used, but I have to say that I haven't seen that in the wild, or in my own projects yet, so I can't say the need is particularly dire to have this in the standard library. It's easy to implement this as a macro in your own projects if it's a common need though. I think a real-world use case which demonstrates the value would go a long way towards making your case.

Paul


To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/716bde83-ad19-46ad-896f-90be5493c035%40googlegroups.com.

Louis Pop

unread,
Dec 27, 2016, 4:13:07 PM12/27/16
to elixir-l...@googlegroups.com, José Valim
I use a lot of structs, so I'd like this shorthand. I don't think wrapping the current syntax in a module is a problem though.

You couldn't really implement this as a macro and save any typing as you'd need to require the macro before using it, which would probably be about the same amount of characters.

Cheers,
Louis 

To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.

--
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.

Akio Burns

unread,
Dec 27, 2016, 4:13:21 PM12/27/16
to elixir-lang-core, jose....@plataformatec.com.br
I think the nature of structs in their present state—being visually tied to a `defmodule`—limits the thinking around them. I think that given the ability to create structs "at-will", opportunities will present themselves where one would've previously used a tuple or a plain map.

Some contrived examples:

defmodule Rectangle do
  defstruct
Point, x: 0, y: 0

 
...
end

defmodule User do
  defstruct
Name, first: "", middle: "", last: ""

 
...
end

I will admit that I have no direct experience to back up these claims though. I'll implement the macro in some of my own projects and try it out.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages