Commit 944eab06 authored by Giuseppe Castagna's avatar Giuseppe Castagna
Browse files

examples in flow

parent 4df3091b
/* @flow */
type Document = {
nodeType: 9;
};
type Element = {
nodeType: 1;
childNodes: NodeList;
}
type Text = {
nodeType: 3;
isElementContentWhiteSpace: bool;
}
type Node = Document | Element | Text
type NodeList = null | [Node, NodeList]
function is_empty_node (x : Node) {
if(x.nodeType === 9) false
else if(x.nodeType === 3) x.isElementContentWhiteSpace
else (x.childNodes == null)
}
/*
it cannot split the cases of empty and non-empty lists and the type
below is completely wrong: not a single return type is correct
*/
var is_empty_node : (Document => false | void)
& (Text => "boo" | void)
& ({ nodeType: 1; childNodes: NodeList} => "foo" | void) =
function (x : Node) {
if(x.nodeType === 9) false
else if(x.nodeType === 3) x.isElementContentWhiteSpace
else (x.childNodes == null)
}
/*
If we add a return then everything breaks
function foo(x : number | string) {
return (typeof(x) === "number")? x++ : x.trim()
}
*/
function foo(x : number | string) {
(typeof(x) === "number")? x++ : x.trim()
}
var y = foo("Hello")
var x = (foo (42))
var z = (typeof y !== 'undefined')? y.trim() : "pippo"
var u = (typeof x !== 'undefined')? x.trim() : "pippo"
var foo2 : (number => number | void ) & (string => string | void ) = x => {
(typeof(x) === "number")? x++ : x.trim()
}
var f : (number => number) & (any => boolean) = x => {
if (typeof(x) === "number"){ throw "false" } else { throw "true" }
}
var g : (number => number) & (any => boolean) = x => {
if (typeof(x) === "number"){ throw "false" } else { throw "true" }
}
/* cannot detect that the "then" branch will be never selected */
function example10( x ) {
if ((typeof (f(x)) === "number") && (typeof (g(x)) === "boolean")){
return 1
} else {
return 2
}
}
/* these version of f and g do not work even if they should check */
var f1 : (number => number) & (any => boolean) = x => {
if (typeof(x) === "number"){ throw "false" } else { return true }
}
var g1 : (number => number) & (any => boolean) = x => {
if (typeof(x) === "number"){ throw "false" } else { return true }
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment