I want it all, and I want it now.... debugging Julia code

I decided that I was actually fairly demanding in what I want from a language, and I think most others are also. What I want , anyway, is not only to be informed with a descriptive error message, but to be informed at a certain time, e.g., an early pass, pre-runtime. For example,
if I type:

Blockquote

abstract type abstractparentaltype end

julia> struct mystruct abstractparentaltype **
** x

** end**

julia> mystruct(19)
ERROR: MethodError: no method matching mystruct(::Int64)
The type mystruct exists, but no method is defined for this combination of argument types when trying to construct it.

Closest candidates are:
** mystruct(::Any, ::Any)**
** @ Main REPL[27]:1**

Stacktrace:
** [1] top-level scope**
** @ REPL[28]:1**

Blockquote

I get an error message alright, but one that does not help (me) solve what is going on. I would much rather see (before the program is run). a message like:

Did you leave out the ā€˜<:’ in the line:

struct mystruct abstractparentaltype

So the block should read:

struct mystruct <: abstractparentaltype
x
end

???

You can format your code using triple backticks.

2 Likes

Improving error messages is a very long-tail sort of problem, but something we care enough about to have a dedicated label on the issue tracker: GitHub Ā· Where software is built

Having concrete examples where things can be better is useful — and while improving them can sometimes be tricky, it’s a great way to get involved!

(but yes, please take the time to format your posts; see Please read: make it easier to help you)

2 Likes

I am not sure how you imagine that Julia could catch that. You can name your fields after abstract types:

julia> struct D
       AbstractVector
       end

julia> d = D(1)
D(1)

julia> d.AbstractVector
1

and

julia> struct A B
       C
       end

is legitimate syntax, eg

julia> fieldnames(A)
(:B, :C)
2 Likes

yes,
struct A B
C
end is legal,
but
struct A B
C D is illegal
I think it would involve a combination of knowing the current run time information (error)
combined with the text of the program then you could deduce the answer.

OR

if you made illegal
struct A B
end

forcing
struct A
B
end
or
struct A <:B
C
end
that would work also.

given that struct A <: B
C D
end
does not work I was surprised that
struct A B end
works
so the rule is
You can have 0 or 1 names after the name of the struct and then on lines after that you can only have 1 name on a line by itself.
Not a nice rule.
should be
after the name of the struct you can have ā€œ<: abstract nameā€
or you can have
<< nothing>>

then you can distinguish the two cases.

It’s weird that struct A B end is allowed but not struct A B C end. I guess the former is ā€œniceā€ for 1-field structs (although a semicolon is hardly imposing)… but then why wasn’t this pattern good enough for n-field structs? Maybe it was never a deliberate choice and merely emerged from more generic parser rules.

I expect disallowing struct A B end (instead requiring struct A; B end) wouldn’t be controversial except that it’d be breaking. Breaking changes have an extremely high barrier to integration, so I expect we’re stuck with this.

All this said, it’s legal syntax (and arbitrarily distant from the MethodError thrown) so I think it’d be tough to offer a ā€œhelpfulā€ tip in this case.

1 Like

Honestly, I don’t even bother trying to read messages from users that don’t bother to format their posts. You have been advised above to format the them with the triple back ticks.

4 Likes

I don’t think clarifying edge cases of ambiguous syntax specifications is ā€œbreakingā€. It would be breaking only if the documentation explicitly mentioned the old syntax

:100:

just thought of a near-trivial way to solve this and perhaps many other problems. The error message Julia spit out says

Blockquote

struct son abstractparent
field1
end

julia> son(3)
ERROR: MethodError: no method matching son(::Int64)
The type son exists, but no method is defined for this combination of argument types when trying to construct it.

Closest candidates are:
son(::Any,::Any)
@ Main REPL[2]:1

Blockquote

if it added the field names I think it would be near-trivial to debug…
so the eror message should look like this:

Blockquote
struct son abstractparent
field1
end

julia> son(3)
ERROR: MethodError: no method matching son(::Int64)
The type son exists, but no method is defined for this combination of argument types when trying to construct it.

Closest candidates are:
son(abstractparent::Any, field1::Any)
@ Main REPL[2]:1
struct son abstractparent
field1
end

julia> son(3)
ERROR: MethodError: no method matching son(::Int64)
The type son exists, but no method is defined for this combination of argument types when trying to construct it.

Closest candidates are:
son(abstractparent::Any, field1::Any)
@ Main REPL[2]:1
field1
end

that would tell you that it is getting confused about what the field names in the type are and that might lead you to the missing <:

@Tigerbyte you need to spend time and energy in formatting your posts and making sure they are readable.

You can edit your posts after they’ve been posted. There’s a friendly editor interface to help you along the way if you don’t feel like using markdown; use the bar above the post.

11 Likes

when I click on the double quotes it puts the line in my file:
backquote
I am using this it just clearly is not having the desired effect. the post looks formatted to me
line breaks are in the right places, but no colors or bold. so I’m just doin it wrong somehow.
I do a backquote at the end of the thing I want to highlight or not ? I added one.
so I have one before and after the error message.

You may find the rich text editor easier to use. Click the far-left button on the toolbar — that M↓ A toggle.

image

Either way, the button to format text is </>.

Just enclose your code in triple backticks, like ```

Do you have a backtick key on your keyboard?

here goes

         field1
         end

julia> son(3)
ERROR: MethodError: no method matching son(::Int64)
The type `son` exists, but no method is defined for this combination of argument types when trying to construct it.

nice

The syntax ambiguity would be arguable if Julia generally had more rigid rules about newlines, but there isn’t, and we can mostly space out one-liners with unpredictable exceptions e.g. let header, catch without a variable name, and struct fields after the first one. Adding narrower rules would be breaking, so this would have to be left to a v2 that probably won’t come. A v2 could be nice for simplifying the syntax and thus parsing e.g. shortening function and not having 3 definition syntaxes with various parsing issues, and ; isn’t so bad as a substitute for spaces if there is a rule mandating newlines in the v2 documentation.

Have you tried Pluto.jl and the AI assistant it has now? I think that kind of analysis can well be job of an AI assistant.