4
votes

I'm wondering if it's possible to do some sort of dependent typing like the following in Elm, like you can in Idris:

isQuestion : String -> Type
isQuestion (sentence) with (endsWith "?" sentence)
    | True = Question
    | False = Statement

Is there a library that will let me achieve a similar effect via typing?

1
In general no, There may be a few special cases where you could fake it, but this is not a design goal for Elm near as I can tellZachary K

1 Answers

8
votes

You could do something alike with union types.

type Sentence 
    = Question String
    | Statement String

isQuestion : String -> Sentence
isQuestion sentence =
    case endsWith "?" sentence of
        True -> Question sentence
        False -> Statement sentence