新的F#和不知道如何修复FS0025错误

时间:2015-03-17 13:31:44

标签: f# ocaml f#-3.0

我努力通过构建一个非常重要的应用程序(或尝试)来记录用户输入并写入JSON文件(或最终,数据库),努力自学F#(我的第一个函数式编程语言) )贫困的贩运幸存者向美国所有反贩运慈善机构/非政府组织提出的帮助请求的结果。在尝试了我能想到的所有内容之后,我不断得到这个错误,以确保考虑所讨论模式的所有可能情况。我不知道如何修复它,我真的不能在这个应用程序上使用任何错误抑制。任何帮助都将非常感激,因为我没有其他任何人可以寻求任何帮助和支持。这是我在TerminalBuilder.fs文件中的代码:

namespace Nonprofits

module TerminalBuilder =
    open System
    open System.IO
    open Nonprofits.Types

    let rec caller():Caller =
        printfn "Are you reporting as an advocate on behalf of someone else or for yourself?"
        printfn " 1 for Advocate"
        printfn " 2 for Self"
        let answer = Console.ReadLine()
        match answer with
        | "1" -> Advocate
        | "2" -> ClientOrVictim 
        | _ -> printfn "Invalid Entry"
               caller()

    let specialneeds():Set<Disability> =
        let rec spnds(s:Set<Disability>): Set<Disability> =
            printfn "Do you/the person on whose behalf you're reporting have any disabling conditions?"
            printfn " 1 for Learning Disability"
            printfn " 2 for Physical Disability"
            printfn " 3 for Mental Health Issues"
            printfn " 4 for Substance Addiction Issues"
            printfn " 5 for Pregnancy-Related Limitations"
            printfn " 6 for Chronic Illness"
            printfn " 7 for Don't KNow/Undiagnosed"
            printfn " Enter 'Exit' for None"
            let answer = Console.ReadLine()
            match answer.Trim().ToLower() with
            | "exit" -> s
            | _ -> 
                let sn =
                    match answer.Trim().ToLower() with
                    | "1" -> Some LearningDisabled
                    | "2" -> Some PhysicallyDisabled
                    | "3" -> Some MentalIllness
                    | "4" -> Some SubstanceAddiction
                    | "5" -> Some Pregnancy
                    | "6" -> Some ChronicIllness
                    | "7" -> Some Undiagnosed
                    | "exit" -> printfn "No disabling conditions"
                                None
                match sn with
                | None -> spnds(s)
                | Some (x) -> spnds(s.Add(x))
        spnds(new Set<Disability> ([]))

这是我的类型文件,Nonprofits.fs:

namespace Nonprofits

open System.Collections

module Types =

    type NgoType =
        | HomelessShelter
        | DVShelter
        | TraffickingVictimSafehouse
        | TraffickingSurvivorAftercare // gamut of legal, housing, clothing, food, medical, reintegration, etc.
        | FoodPantries
        | ClothingAssistance
        | FreeMedicalDentalClinic

    type Ngo = Ngo of NgoType * string 


    type Caller =
        | ClientOrVictim 
        | Advocate

    and CallerStatus =
        | VictimServicesAdvocate of Caller
        | DVvictim of Caller
        | SexTraffickingSurvivor of Caller
        | HomelessVictim of Caller
        | NaturalDisasterVictim of Caller
        | GeneralPovertyVictim of Caller

    and Disability =
        | Pregnancy 
        | PhysicallyDisabled 
        | LearningDisabled 
        | MentalIllness 
        | SubstanceAddiction 
        | ChronicIllness 
        | Undiagnosed 

    and SpecialNeeds = SpecialNeeds of Set<Disability>

    type UnmetNeeds =
        | TraffickingSafebed
        | DVsafebed  
        | Housing
        | Clothing
        | Food
        | Legal 
        | Medical
        | Dental
        | Vision
        | DrugRehab
        | TraumaCare
        | PsychiatricCare
        | SkillsTraining
        | EducationHelp
        | JobPlacement
        | EconomicSupport

    type CallerRequest =
        | TraffickingVictimAftercare of Set<UnmetNeeds>
        | PovertyVictimCare of Set<UnmetNeeds>

    type Followup =
        | SocialWorkerFollowup of Help
        | CallerSelfDirected of Help

    and Help =
        | Helped //fully helped with everything caller needed
        | ExhaustedOptions // exhausted resources and still not helped
        | WrongHelp //i.e. caller offered smoking cessation counseling when caller needed sex trafficking aftercare
        | NotHelped of Followup
        | GivenReferral of ReferredToNextNgo

    and ReferredToNextNgo = ReferredToNextNgo of Followup * Ngo

    type CallOutcome =
        | CallerHelped
        | CallerNotHelped of Followup
        | CallerReferred of ReferredToNextNgo


    type Call = Call of Caller * CallerRequest * CallOutcome

这是我得到的错误:

  

C:\ Users \ 3CU1501060 \ Documents \ Projects \ DUandTypesPractice \ DUandTypesPractice \ TerminalBuilder.fs(27,27):警告FS0025:此表达式上的模式不匹配。例如,值&#39;&#34; a&#34;&#39;可能表示模式未涵盖的案例。 (FS0025)(DUandTypesPractice)

1 个答案:

答案 0 :(得分:1)

模式匹配时

      let sn =
                match answer.Trim().ToLower() with
                | "1" -> Some LearningDisabled
                | "2" -> Some PhysicallyDisabled
                | "3" -> Some MentalIllness
                | "4" -> Some SubstanceAddiction
                | "5" -> Some Pregnancy
                | "6" -> Some ChronicIllness
                | "7" -> Some Undiagnosed
                | "exit" -> printfn "No disabling conditions"
                            None

```

你检查“1”,“2”,......“退出”,但还有其他值,例如“a”,你需要添加另一个案例

| answer -> //do something

在你的情况下可能

| answer -> printfn "not supported answer %A" answer
            None // or retry?

编译器正在帮助您,告诉您“如果用户选择'a'会发生什么?”