Struct syntax

Hi,

I wonder why line breaks are required when defining fields of a structure, and where this is documented:

julia> struct S
       a
       b
       end

julia> struct S a b end
ERROR: syntax: extra token "b" after end of expression

julia>

You can use ; instead of line breaks. Also consider

3 Likes

Thank you, but I find it strange, documentation (Punctuation section) says:

semicolons separate statements [...]

OK, it’s not a hot topic, just curiosity :slight_smile:

1 Like

It’s only required between the fields themselves, so one can write struct A a end.

I’m also interested to know why it’s required at all. Maybe the devs didn’t want to complicate parsing just to let people write code that’s harder to read :slight_smile:

Subjective of course but struct A b c end is arguably less readable than

struct A
  b
  c
end
1 Like

Something like this would be hard to parse (but maybe not impossible) without new-lines:

julia> struct T
       a::T where T<:Integer
       b::U where U<:AbstractArray
       end
3 Likes

This is not a topic about readability, but about parsing rules. Semicolon is ok for the preceeding example:

julia> struct S a::T where T<:Integer; b::U where U<:AbstractArray end

julia> 
1 Like