|
|
|
|
|
by wellpast
3155 days ago
|
|
class Person {
def age;
}
There is a second-class "age" property: the only way to specify it or use it is to first declare the ADT/Person class. (In this example, I use a dynamic language; that is, the property can be given any type of value.)Here is the usage of a first-class age property: public boolean isOld(Map<String, Object> entity) {
return entity.getAs("age", int.class) > 30;
}
Note that in this example, age's semantics are independent of any Person/ADT. Now a statically-typed language that would let me define a vocabulary that include "age" and its type independent of an ADT would be supporting first-class properties.I could cheat and do this in a typed language like Java like so: class Age { int val; } // using class to define a property
class Entity { Age age; ...and list all other properties here... }
But this is absurdly cumbersome and obviously Java isn't conducive to this. |
|
What you're referring to is known as row polymorphism or structural typing and it exists in languages such as Elm, Purescript, Scala and Go (and I believe Haskell has these as an extension). For instance, this is valid Scala:
And here's Elm: