Open manikmagar opened 3 years ago
I can think of another way to get this working w/ a union type. Would this alleviate the pain point you're experiencing?
fun getName(p : Person | Employee): String ...
Or you could overload
fun getName(p: Person): String ...
fun getName(p: Employee): String ...
If I am using a custom utility module from shared resource that provides me many functions to handle Person
object, I won't be able to use union types unless I modify that custom module.
I think those approaches will work if you have control over all types or they are known in advance.
Have you tried removing the "closed object" syntax?
-type Person = {| firstName: String, lastName: String |}
+type Person = { firstName: String, lastName: String }
fun getName(p:Person): String = "$(p.firstName) $(p.lastName)"
It should work for your use case.. In most cases, DataWeave have structural types. That means it won't pay attention to the name of the type but yes to the structure of the type. In this specific case, by using {| ... |}
you are telling the type system to check that the type only has the properties you specify, nothing else is allowed. Instead, by using a type without the pipes { ... }
you are telling the type system to use a type that matches
, allowing extra properties.
Consider an Object received in an API looks like Following and can be represented as an instance of
Employee
type -In inheritance world, it is possible to have a structure like
Employee extends Person
.Consider following dataweave script -
This script fails with following error -
Person
is represented as a closed subset of anEmployee
.An explicit coercion of Employee to Person
getName(empExample as Person)
fails with following example -There are two ways to get this working -
getName({firstName: empExample.firstName, lastName: empExample.lastName})
.Person
to be an open object -type Person = {firstName: String, lastName: String}
, in which casegetName(empExample)
works.Will it make sense to allow type casting objects into other if they are closed subsets -
getName(empExample as Person)
?