undefined: actually I have expected to never face undefined again with elm

144 views
Skip to first unread message

Andreas Kobler

unread,
Jul 19, 2016, 2:59:04 AM7/19/16
to Elm Discuss
Hi all

Just started to play around with elm and very eager to get more fluid. I picked a silly POC scenario of a rule composer: basically you have regex rules and want to edit/test them in a form. When applying a regex and showing the replace output in an input field, I face an "undefined". Below you see the isolated part forcing my observation while applying the rule directly in init.

Unfortunately I could really not trace down to the root of this happening. Is somebody able to tell me:
- What I am doing wrong? Why does there an "undefined" appear? (wenn executing my own "replace" function in the REPL it works fine as expected)
- How do you guys debug such behaviour?

Here my silly example:


module RuleTest exposing (..)


import Html.App
import Html exposing (..)
import Html.Attributes exposing (..)
import Regex


main
: Platform.Program Basics.Never
main
=
 
Html.App.program
   
{ init = init
   
, view = viewRuleTest
   
, update = update
   
, subscriptions = \_ -> Sub.none
   
}


type
alias Rule =
 
{ regex : String
 
, substitution : String
 
}


type
alias RuleTest =
 
{ rule : Rule
 
, input : String
 
, output : String
 
, matched : Bool
 
}


type
Msg
 
= StartTest String




-- init
init
: (RuleTest, Cmd Msg)
init
=
 
( apply "reg1" (Rule "reg1" "sub1")
   
, Cmd.none
 
)


-- update
update
: Msg -> RuleTest -> (RuleTest, Cmd Msg)
update msg ruleTest
=
 
case msg of


   
StartTest input ->
     
( apply input ruleTest.rule, Cmd.none )




apply
: String -> Rule -> RuleTest
apply input rule
=
  let
    output
= replace rule.regex rule.substitute input
    matched
= input /= output
    rule
= rule
 
in
   
RuleTest rule input output True


replace
: String -> String -> String -> String
replace regex substitute input
=
 
Regex.replace Regex.All (Regex.regex regex) (\_ -> substitute) input


viewRuleTest
: RuleTest -> Html Msg
viewRuleTest ruleTest
=
  div
[]
   
[ label [] [ text "Pattern" ]
   
, input [ Html.Attributes.value ruleTest.rule.regex, disabled True ] []
   
, label [] [ text "Substitution" ]
   
, input [ Html.Attributes.value ruleTest.rule.substitution, disabled True  ] []
   
, label [] [ text "Input" ]
   
, input [ Html.Attributes.value ruleTest.input, disabled True  ] []
   
, label [] [ text "Output" ]
   
, input [ Html.Attributes.value ruleTest.output, disabled True  ] []
   
, label [] [ text "matched" ]
   
, input [ type' "checkbox", checked ruleTest.matched ] []
    ]


Environment: 
elm repl --version
0.17.1
OSX 10.11.3

{
    "version": "1.0.0",
    "summary": "helpful summary of your project, less than 80 characters",
    "license": "BSD3",
    "source-directories": [
        "."
    ],
    "exposed-modules": [],
    "dependencies": {
        "elm-lang/core": "4.0.1 <= v < 5.0.0",
        "elm-lang/html": "1.1.0 <= v < 2.0.0"
    },
    "elm-version": "0.17.1 <= v < 0.18.0"
}


Thanks for any advice!

Best, Andi


Ian Mackenzie

unread,
Jul 19, 2016, 3:07:11 AM7/19/16
to Elm Discuss
The issue is likely with the rule = rule line, as Elm sees that as a recursive variable definition which has known bugs (https://github.com/elm-lang/elm-compiler/issues/873). You should just be able to take that line out, there's no need to re-declare a variable with the same name as an argument.

Andreas Kobler

unread,
Jul 19, 2016, 4:15:16 AM7/19/16
to Elm Discuss
Thanks for your hint! The reason for the re-declaration is: Without that line, the compiler yields the following error message. My re-declaration line was not well-founded, I just try-n-error'ed it out. Is is a record-thing, do I call the RuleTest constructor in a wrong way? Do you have an explanation for that compiler error without the rule=rule line? Or a pointer in a direction how I can figure it out?

Many thanks

Code chunk adapted to:

apply : String -> Rule -> RuleTest
apply input rule
=
  let
    output
= replace rule.regex rule.substitute input
    matched
= input /=
output
 
in

   
RuleTest rule input output True


==== error in test.elm:55:5: ====
The 1st argument to function `RuleTest` is causing a mismatch.
Function `RuleTest` is expecting the 1st argument to be:
    { ..., substitution : ... }
But it is:
    { a | ..., substitute : ... }
----
[Finished in 0.1s with exit code 1]
[Elm says]: To highlight build errors: Install with Package Control: Highlight Build Errors

Ian Mackenzie

unread,
Jul 19, 2016, 4:59:00 AM7/19/16
to Elm Discuss
Try 'rule.substitution' instead of 'rule.substitute'. It looks like the compiler sees the 'rule.substitute' line and assumes that the 'rule' variable is something that has a 'substitute' field (and then gets confused when you try to pass such an object to the RuleTest constructor), instead of checking it first against your 'apply' type annotation that says that the second argument is of type Rule.

Andreas Kobler

unread,
Jul 19, 2016, 5:18:23 AM7/19/16
to elm-d...@googlegroups.com
Thanks a lot! 
Bad typo... was not really awake this morning :-/ Now it works as expected!

--
You received this message because you are subscribed to a topic in the Google Groups "Elm Discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elm-discuss/-zSBcLpbWo0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elm-discuss...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ian Mackenzie

unread,
Jul 19, 2016, 6:15:14 AM7/19/16
to Elm Discuss
Glad I could help! 

Note, though, that the compiler was giving you a pretty strong hint (zeroing in on 'substitute' vs 'substitution' as the difference between the expected and actual types), and in general is pretty good at giving useful error messages. It won't always figure out exactly what's wrong (I've had my share of "I am looking for one of the following things: end of input, whitespace"), but it does tend to give more useful error messages than a lot of other compilers (I'm looking at you, every C++ compiler ever).
Reply all
Reply to author
Forward
0 new messages