String.split - FunctionClauseError no function clause matching in String.split/3

thanks @eksperimental that is being done tonight, and will be back in the am with a better understanding on pattern matching…

1 Like

I’m really curious if you solved your problem. Seems pretty easy from the side but it can be hell while you’re learning.

So, what happened?

well folks due to other pressing problems it took several days before I could come back to this interesting problem for me.

since the the protocol introduction to my brain I started doing more research into the code and found this gem inside the FieldValue module:

fv = %FieldValue{
    module: __MODULE__,
    field: field_name,
    value: value,
    recased: recased,
    label: label,
    rank: rank,
    active?: Keyword.get(opts, :active?, true)
}

and when I tried to put values into the fields in the structure I got this error message

%FieldValue{module: :MODULE, field: :bar, value: 25, recased: :false, label: “now is the time”, rank: 1, active?: true}

%Inspect.Error{
  message: "got ArgumentError with message \"argument error\" while inspecting %{__struct__: Aspire.FieldValue, active?: true, field: :bar, label: \"now is the time\", module: :MODULE, rank: 1, recased: false, value: 25}"
}```


and no @dimitarvp, I've not been able to resolve the issue as I still don't understand the code but I am progressing.

Can you post the exact error whole surrounding it with triple backticks, please?

1 Like

Just as an advice for future times. If you have an error, please paste the minimum needed code to reproduce the error, so we can copy and paste it in IEx and investigate, along with the full error message, and properly format it with the triple backticks as Dimitri mentioned.

I have managed to trace the error, in part is it not clear what is going on due to Elixir giving a cryptic error message, so I will submit a fix for this as well.

2 Likes

You’ve got an error in the inspect function, and Elixir does not show the stacktrace along with it, so it is confusion to figure out what is the function that expects its first argument to be atom, and instead it shows the struct map.

you error is that you are passing fv,value (which is an integer) to Atom.to_string. it should be Integer.to_string instead.

Paste this in IEx.

defmodule FieldValue do
  @enforce_keys [:module, :field, :value, :recased, :label, :rank, :active?]
  defstruct @enforce_keys

  defimpl Inspect do
    import Inspect.Algebra

    def inspect(%FieldValue{} = fv, opts) do
      value_doc = color(Atom.to_string(fv.value) <> ":", :atom, opts)
      label_doc = color(inspect(fv.label), :string, opts)

      concat(["#FV<", value_doc, " ", label_doc, ">"])
    end
  end
end
iex(3)> fv = %FieldValue{module: :MODULE, field: :bar, value: 25, recased: :false, label: "now is the time", rank: 1, active?: true}
%Inspect.Error{
  message: "got ArgumentError with message \"errors were found at the given arguments:\\n\\n  * 1st argument: not an atom\\n\" while inspecting %{__struct__: FieldValue, active?: true, field: :bar, label: \"now is the time\", module: :MODULE, rank: 1, recased: false, value: 25}"
}

This should be the proper stacktrace.

got ArgumentError with message "errors were found at the given arguments:

  * 1st argument: not an atom
. Stacktrace:
    :erlang.atom_to_binary(25, :utf8)
    iex:9: Inspect.FieldValue.inspect/2
    (elixir 1.14.0-dev) lib/inspect/algebra.ex:342: Inspect.Algebra.to_doc/2
    (elixir 1.14.0-dev) lib/kernel.ex:2246: Kernel.inspect/2
    (iex 1.14.0-dev) lib/iex/evaluator.ex:336: IEx.Evaluator.io_inspect/1
    (iex 1.14.0-dev) lib/iex/evaluator.ex:313: IEx.Evaluator.handle_eval/3
    (iex 1.14.0-dev) lib/iex/evaluator.ex:285: IEx.Evaluator.do_eval/3
    (iex 1.14.0-dev) lib/iex/evaluator.ex:274: IEx.Evaluator.eval/3
" while inspecting %{__struct__: FieldValue, active?: true, field: :bar, label: "now is the time", module: :MODULE, rank: 1, recased: false, value: 25}
2 Likes

sure:

%Inspect.Error{
  message: "got ArgumentError with message \"argument error\" while inspecting %{__struct__: Aspire.FieldValue, active?: true, field: :bar, label: \"now is the time\", module: :MODULE, rank: 1, recased: false, value: 25}"
}```

this I do not understand or how this is being created. the error I got does not look anything like this…

Yes, it does not look like that because Elixir is not displaying it properly. I am working on a PR to fix it.

thanku

1 Like

I’m still looking for a way to pattern match the string. however, I’m beginning to see where I will be able to translate/converse the dynamic nature of the string to its value of “mss” by using the correct values to the struct conversion master string, but have not figured it out yet…
interesting puzzle… hummm… :slight_smile:

PS: going on 10 days on this string puzzle…

1 Like

Word of advice: delete the defimpl Inspect part of of your code.
I still not quite follow what you are trying to do, but a bug in that implementation got you stuck and is abstracting you from seeing how that struct works. Once you have your code working bring it back and fix any bugs.

If you want to see the real structure of a struct, you can do: IO.inspect(your_struct, structs: false)

trying that next…
either I used in a wrong way or my format is invalid

iex(25)> IO.inspect (%FieldValue{module: :MODULE, field: :bar, value: 25, recased: :false, label: "now is the time", rank: 1, active?: true}, struts: false)
** (SyntaxError) iex:25: unexpected parentheses. If you are making a function call, do not insert spaces between the function name and the opening parentheses. Syntax error before: '('```

I removed the space in front of the "(" but the error is a before

The error message is quite clear, I guess you can figure out yourself what is wrong with your syntax.
Also note there is a typo in your second argument.

1 Like

yeah, I’m back to this error

  message: "got ArgumentError with message \"argument error\" while inspecting %{__struct__: Aspire.FieldValue, active?: true, field: :bar, label: \"now is the time\", module: :MODULE, rank: 1, recased: false, value: 25}"
}

still don’t see what’s wrong with the second argument.

did you fix or remove the implementation of the Inspect protocol as I suggested?

1 Like

Also, if you could provide the code I should enter in a new IEx session to reproduce the error, it will help a lot.

1 Like

hello @eksperimental regarding your message:

Word of advice: delete the `defimpl Inspect` part of of your code.

I did not write this code nor do I understand it and the person who wrote just left the company so I can’t ask questions on the code, then there is the issue that since I’m a “less than a junior coder” I can’t make nilly willy changes to code created by a master coder so I’m stuck on deleting anything, the only thing I can do is work with what I’ve got.

the main problem that I have is that this code now produces a weird string which is being compared to a simple string like “mss” but the crap being produces is "#FV<mss: " blah blah blah which is no longer == to simple “mss” string… anything else is garbage.

anywho: thank you very much for your help, you guys have been awesome and I apologize for being so dumb… for now I need to see if I can use a static table for the time being… oh and did I tell you, this crap broke code that had worked for years… as they say “HAPPY CODING”… :frowning:

Well. comment that code out or change it in your local version until you have all working and you can submit a fix.

The issue with your code is that the Inspect protocol is expecting an atom as value, and you are passing an integer. So you either fix it, or pass the atom representation of the integer 25, value: :"25".

As I mentioned earlier, I think you should forget about the Inspect protocol for now. Move forward! later come back and fix whatever necessary as you will have a better understanding of your code.

PM if you need any more help, Ideally upload some code and we can discuss over code and I can explain you what I see it is wrong with it.

Thank your for bring this up, it unearthed a bug in Elixir. That means an improvement for the language.

Cheers

1 Like

Sure, the only problem my boss is having is this code apparently is have a wider effect on several modules that are using these field values for creating dynamic changes on the fly. another thing is I use “value: 25” as a way to create the strut but when I used a a string “25” it barfed way bad giving me a different error message in all red letters format so I went back to the 25 to move on.

I would love to do as you have suggested of removing the Inspect protocol, however, I don’t know what to replace it with… and that’s what happens when you’re a newbe… of course my boss keeps saying “geez when are u going to fix it…” but with no training I feel like saying “I think it’s time you find somebody who can do this well because I don’t know what I’m doing…” and that’s where I am…

thanku

1 Like