Given some type signatures like this:
type Foo = { name : String }
getFooName : Foo -> String
getName : { a | name : String } -> String
Is it possible to infer that getName can be used in place of getFooName?
Join Stack Overflow to learn, share knowledge, and build your career.
Given some type signatures like this:
type Foo = { name : String }
getFooName : Foo -> String
getName : { a | name : String } -> String
Is it possible to infer that getName can be used in place of getFooName?
Yes. Here is a working example in elm. You can run it at http://elm-lang.org/try
import Html exposing (text)
type alias Foo = { name : String }
fnThatUsesAGetFooName : ( Foo -> String ) -> Foo -> String
fnThatUsesAGetFooName x foo = x foo
getName : { a | name : String } -> String
getName v = v.name
arg: Foo
arg = { name = "gmorenz" }
main =
text (fnThatUsesAGetFooName getName arg)
This is known as "structural typing" (and I think "row typing").
You could also view this problem in a type system with interfaces or typeclasses like rust. Automatically create and implement "varname_with_type_typeid" traits (interfaces) with getters and setters for every field in every structure. Obviously this requires extending the compiler, but since it's a simple textual transformation it is easy theoretically speaking. Then getName would look like
fn getName<T: name_with_type_TypeIdOfString>(input: T) -> String {
t.get_name_with_type_TypeIdOfString()
}
and you could make a fn(Foo) -> String out of it by saying getName::<Foo>.