Flow map object optional keys to optional values - flowtype

I have a type like so:
type o = {
a?: string,
b: string
}
I want to build a type utility that will convert this into
type o = {
a?: ?string,
b: string
}
ie. I want to convert all optional keys, to have optional values too.
Here's what I have so far using $ObjMapi: Flow
Any suggestions?

Related

How to create Map of tuples in ReasonML?

I'm very new to Reason. I have a tuple containing two strings and want to make a Map where the keys are of that tuple type.
How should I go about doing it?
Map.Make is a functor, which means it expects a module as its argument, not a type. The module argument must conform to the OrderedType signature:
module type OrderedType = {
type t
let compare : (t, t) => int
}
In your case that would be something like:
module TuplesMap = Map.Make({
type t = (string, string)
let compare = (a, b) => ...
});
Then all you need to do is to implement the compare function.

Utility function to convert everything to optional

Many times I find myself passing in as an arg a certain Shape type but where each key is optional, only at least one is required.
For example:
type Shape = {
+isFetching: boolean,
+errorFetching: null | string
}
type ShapeOpt = {
isFetching?: boolean,
errorFetching?: boolean
}
function set(data: ShapeOpt) {
for (const key in data) {
global[key] = data[key];
}
}
Is there a utility function to convert from Shape to ShapeOpt?
There is a $Shape<Type> helper for generating an object type where each key is optional. But I don't know of a way to say that at least one item is required automatically.

Set a struct field with field type of a interface

Is there any way to set an interface field using reflect? When i tried to set it, it paniced saying that the value is non addressable.
type A interface{...}
func CreateA(name string) A {...}
type B struct {
field A
should A
mirror A
}
// normal way of initializing
var b = B{
field: CreateA("field"),
should: CreateA("should"),
mirror: CreateA("mirror"),
}
func MirrorField(b *B) {
t := reflect.TypeOf(b)
v := reflect.ValueOf(b)
for i := 0; i < t.NumField(); i++ {
setTo = CreateA(t.Field(1).Name)
fieldVal := v.Field(i)
fieldVal.Set(reflect.ValueOf(setTo))
}
}
// what i want is something like
var b = &B{}
MirrorField(b)
Interfaces don't have fields, they only define a method set of the value they contain. When reflecting on an interface, you can extract the value with Value.Elem().
You also can't Set unexported fields. You need to capitalize the field names in your B type. When iterating over the fields, use Value.CanSet() to test if they are settable. CanSet() will also return false is the value isn't addressable, or the value is still in an interface.
A working example of your code:
http://play.golang.org/p/Mf1HENRSny

How to force unwrap Optional String stored as Any

How do i force unwrap an optional value thats "stored" as Any?
let optionalString: String? = "optional string"
let anyString: Any = optionalString
if let unwrappedString = anyString as? String {
println(unwrappedString)
// does not recognize the anyString as an optinal String
}
How do i write an if statement that force unwraps the value stored in anyString and prints the optionalString value? I have to do it only accessing the anyString attribute.
The use case for this is to get values out of MirrorType, which stores the attribute values as Any.
Ignore answer as not really providing a solution - the problem looks like a compiler bug
I'm not deleting it by now so readers know what I tried, and how I misunderstood the real question:
How to unwrap an optional value stored in a variable of Any type
Also be sure to read comments.
In your code you are using optional binding. Forced unwrapping instead uses the postfix ! operator:
println(optionalString!)
but if the optional string contains a nil value, that throws a runtime exception. So unless you are 100% sure it's not nil, I recommend using optional binding:
if let unwrappedString = optionalString {
println(unwrappedString)
}
That said, if you have an optional string stored in an Any variable, using optional downcasting and optional binding you can extract as follows:
var anyString: Any? = "optional string" as String?
if let unwrappedString = anyString as? String {
println(unwrappedString)
}
If you want to use a forced downcast, that's simply:
println(anyString as String)
but as in forced unwrapping, this causes a runtime exception if anyString is nil.
If the anyString variable is not optional, things are even simpler, because there's no optional binding, just optional downcasting, although the related code looks exactly the same:
var anyString: Any = "optional string"
if let downcastedString = anyString as? String {
println(downcastedString)
}
If you have an Any then you can test if it's optional and it it's nil by using something as:
var anyValue: String? = "The string"
var theValue: Any = anyValue
let mi:MirrorType = reflect(theValue)
if mi.disposition == .Optional {
if mi.count == 0 { return NSNull() } // Optional.None
let (name,some) = mi[0]
theValue = some.value
}
Now theValue will just be a String and not an Optional String. So you can cast it and use it as a String.

How to cast a value type to Map in Rascal?

I have a variable of type value that stores a map, but I can not access the values by providing the keys:
rascal>a
value: ("s":"s")
rascal>a["s"]
|stdin:///|(2,3,<1,2>,<1,5>): subscript not supported on value at |stdin:///|(2,3,<1,2>,<1,5>)
☞ Advice
How can I parse the value to map in order to be able to retrieve my value ?
if (map[str,str] myMap := a) {
// do stuff with myMap
}
else {
throw "<a> is not a map?";
}
Another way of "narrowing types" is using pattern matching in function parameters:
rascal>value x = 1;
int: 1
rascal>int myFunc(int i) = 2 * i;
ok
rascal>myFunc(x);
int: 2
And yet another way is using visit or switch:
visit(bigValue) {
case Expression e => ...work with e...
}
The general idea is:
pattern matching means narrowing (downcasting)
pattern matching may fail and so is always in a conditional context
there are many places in Rascal where you can use pattern matching: function dispatch, switch, visit, :=, <-

Resources