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?

  • You're possibly thinking of something along the lines of row polymorphism, see this answer for a good discussion of the differences between structural subtyping, row polymorphism, and actual type judgements. – kieran Jan 26 at 15:39

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>.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.