Missing type annotation for `T`. `T` is a type parameter declared in array type [1] and was implicitly instantiated at call of method `slice` - flowtype

I have a simple js example where I'm getting a weird inferred error.
Missing type annotation for T. T is a type parameter declared in array type [1] and was implicitly instantiated at call of method slice [2].Flow(InferError)
function getChunk(items: Array<Object>, start: number, end: number): Array<Object> {
const chunk = items.slice(start, end)
return chunk
}
I'm unsure why it's trying to infer the array even though I did express it as an array of objects.

Ok so here's the head scratcher.
The error was displayed before I had entered the return type of Array<Object> so the error was a bit weird and didn't express to me that the return type was missing.
But it was not until I relaunched the editor when the return type was accepted. So maybe the flow server was hung or something.

Related

rJava passing int to constructor

I defined a java class with several constructors with different parameters. If I try to create a java object in R via obj<-.jnew('testRJava/TestRJava', "Hello", "World") or obj<-.jnew('testRJava/TestRJava', "Hello world") everthing working as expected. But if I try to pass an Integer like obj<-.jnew('testRJava.TestRJava', "Hello", 123) the following error is thrown:
Error in .jnew("testRJava/TestRJava", "Hello", 123) :
java.lang.NoSuchMethodError: <init>
On Java side I definitly defined the constructor public TestRJava(String param1, int param2) {... and the rjava doc says:
Any parameters that will be passed to the corresponding constructor.
The parameter types are determined automatically and/or taken from the
jobjRef object. For details see .jcall. Note that all named parameters
are discarded.
But I can't find how to pass an integer. Do I have to use .jcast or anything else?
123 is a double. 123L is a (long) integer.

Interface can not call self method

I have defined two functions. When I pass a pointer to it, I can't get the defined method. Why is this?
type Visitor interface {
work()
}
func test(v *Visitor) {
v.work() // error
}
func test1(v Visitor) {
v.work() // ok
}
Error:
v.work undefined (type *Visitor is pointer to interface, not
interface)
anyone know why, ths
func test(v *Visitor) {
v.work() // error
}
v.work() ought to be a method call. But v is of type *Visitor, a pointer to interface. A pointer to interface has 0 methods, it does not implement anything (except the empty interface interface{}).
When using a non-pointer, the value v (or rather its type) has a method work(), so you can call that:
func test(v Visitor) {
v.work() // ok
}
Here v.work() works, because the v is of type Visitor which is an interface, and it contains the method work().
What may be confusing is that if you add method to a (non-pointer, non-interface) concrete type, the respective pointer type will also have that method, and you can call that. This is in Spec: Method sets:
A type may have a method set associated with it. The method set of an interface type is its interface. The method set of any other type T consists of all methods declared with receiver type T. The method set of the corresponding pointer type *T is the set of all methods declared with receiver *T or T (that is, it also contains the method set of T). Further rules apply to structs containing embedded fields, as described in the section on struct types. Any other type has an empty method set. In a method set, each method must have a unique non-blank method name.
The difference is that you tried the same with interface type, which won't work. It works with concrete (non-interface) types. Lesson is to never use pointer to interface unless you can reason why it is needed (it is rarely needed).
As the error clearly states:
v.work undefined (type *Visitor is pointer to interface, not
interface)
This is because the work() function is called on pointer to the receiver but defined on value.
type Visitor interface {
work()
}
But you are passing pointer type receiver in second case in which you are getting an error.
In Golang spec Method sets are defined as:
A type may have a method set associated with it. The method set of an
interface type is its interface. The method set of any other type T
consists of all methods declared with receiver type T. The method set
of the corresponding pointer type *T is the set of all methods
declared with receiver *T or T (that is, it also contains the method
set of T). Further rules apply to structs containing embedded fields,
as described in the section on struct types. Any other type has an
empty method set. In a method set, each method must have a unique
non-blank method name.
One approach you can do is Implement the interface by using the struct on which you can call the method work().
package main
import "fmt"
type Visitor struct{}
type Visit interface {
work()
}
func test(v Visit) {
v.work() // error
fmt.Printf("%+v", v)
}
func (v *Visitor) work(){}
func main(){
v := Visitor{}
test(&v)
}
Working Code on Go playground

How can &deployment satisfy type runtime.Object in kubernetes code?

In kubectl/run.go in Kubernetes code, the Generate function has a result list of these two types:
runtime.Object, error
The last line of the function is:
return &deployment, nil
runtime is imported:
k8s.io/apimachinery/pkg/runtime
I got runtime by running go get on that import statement, and Object is defined in interfaces.go:
type Object interface {
GetObjectKind() schema.ObjectKind
DeepCopyObject() Object
}
(And I found the same code on the web here.)
The address operator creates a pointer... more specifically, the Go spec states:
For an operand x of type T, the address operation &x generates a pointer of type *T to x.
and pointers have a type distinct from their base type:
A pointer type denotes the set of all pointers to variables of a given type, called the base type of the pointer.
How does &deployment satisfy the runtime.Object type?
My best guess so far is that deployment implements the runtime.Object interface, and mapping &deployment to runtime.Object satisfies this rule of assignability:
T is an interface type and x implements T.
and that a return statement mapping to a result list type is equivalent to assignment in this respect. Is this correct? If not, is there another part of the specification or documentation that explains it?
deployment is a local variable, its declaration:
deployment := extensionsv1beta1.Deployment{
// ...
}
Where extensionsv1beta1 from the imports:
import (
// ...
extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
// ...
)
Doc of extensionsv1beta1.Deployment. Its definition is:
type Deployment struct {
metav1.TypeMeta `json:",inline"`
// ...other fields...
}
It embeds metav1.TypeMeta, which has a method GetObjectKind() method with pointer receiver. This means a pointer to Deployment also has this method, because Spec: Struct types:
Given a struct type S and a defined type T, promoted methods are included in the method set of the struct as follows:
If S contains an embedded field T, the method sets of S and *S both include promoted methods with receiver T. The method set of *S also includes promoted methods with receiver *T.
And Deployment has a "direct" DeepCopyObject() method, again with pointer receiver. So the method set of *Deployment contains this method.
And last quoting Spec: Interface types:
An interface type specifies a method set called its interface. A variable of interface type can store a value of any type with a method set that is any superset of the interface. Such a type is said to implement the interface.
So this means the method set of *Deployment has all the methods defined by Object, or in other words: the method set of *Deployment is a superset of the method set of Object, so *Deployment implements Object.
deployment is of type extensionsv1beta1.Deployment, which means &deployment is of type *extensionsv1beta1.Deployment, which we showed above that it implements Object; so the value &deployment can be assigned to or be stored in a variable of type Object.

Setting field using reflect.Set

I have code
var t reflect.Type = LaunchController(route.controller)
// create controller ptr .
var appControllerPtr reflect.Value = reflect.New(t)
fmt.Println(appControllerPtr) //#=> <**controller.AppController Value>
var appController reflect.Value = appControllerPtr.Elem()
// Create and configure base controller
var c *Controller = &Controller{
Request: r,
Writer: w,
Name: t.Name(),
}
//this should assign *goninja.Controller field in application controllers
var controllerField reflect.Value = reflect.ValueOf(appController).Field(0)
controllerField.Elem().Set(reflect.ValueOf(c))
This creates pointer to element, and afterwards trying to assign value, into 0 field of this struct.
My struct, that i'm trying to reflect looks like
type AppController struct {
*goninja.Controller
}
However when I'm trying to assign this field with code
controllerField.Elem().Set(reflect.ValueOf(c))
I'm facing following error
reflect: reflect.Value.Set using value obtained using unexported field
What am i doin wrong? Also I cant understand why my reflect.New(t) returns reflect.Value with 2 asterisks in beginning **
You don't give your complete code, so I have to guess a bit, but I suspect that the Controller field of the AppController structure has a lower-case name. Right? Here is my attempt to produce a minimal example from your code: working (with upper-case field name) and non-working (with lower-case fieldname).
Also: where you write reflect.ValueOf(appController).Field(0), the appController is already of type reflect.Value, so the ValueOf is not required. You can just write appController.Field(0) as in the example code I linked above.

what is * return type in as3

I saw a method in Action script that has a return type of *
public function f(s:String):*
what does this [*] means ?
That answer is not 100% correct. There is no "untyped" and there is only a small difference between * and Object, because one could argue that Object means untyped as well since every type extends from Object.
However * implies the undefined value and Object not. A big difference! This is useful for dynamic languages because this means a property of an Object can be undefined which is different from defined and null.
So for instance y is undefined in { x: null } and x is defined but without a value. And you can make use of that:
var yesNoMaybe: *;
yesNoMaybe = true;
yesNoMaybe = false;
yesNoMaybe = undefined;
The * symbol means "untyped", meaning that the type can be anything (and that the value can be undefined). Using the asterisk has the same effect as not specifying the type at all, but it's good form to use it in order to be explicit about your intention. See the language reference for more info.

Resources