I wrote the below code to get a KClass of Array<*>.
Array::class
However, this code has a compilation error.
Kotlin: Array class literal requires a type argument, please specify one in angle brackets
Do you know the reason or solution?
On the JVM platform, Kotlin Array<T> types are mapped to Java arrays, which, unlike Java generic types, are not subject to type erasure, they are reified instead.
It means, among other things, that arrays with different element types are represented by different classes, which have different Class<T> tokens, and these class tokens contain the information about the element type as well. There's no generic array type, but only array types for arrays with different element types.
Since generic Array<T> doesn't exist, you cannot use its reflection either, you can only get the runtime type information of array types with specified element types:
val c = Array<Int>::class // corresponds to Java Integer[] type
val d = Array<Array<String>>::class // corresponds to Java String[][]
val e = IntArray::class // corresponds to Java int[]
If you need to check whether an arbitrary class is an array type, you can do it with Java reflection:
val c = Array<Int>::class
println(c.java.isArray) // true
Related
I'm trying to learn F# by converting an existing .NET Core solution in C# over, one project at a time. I currently have an interface in C# with nullable reference types:
public interface IVehicle {
public int GetWheels();
public string? GetLicensePlate();
}
And this interface is implemented in a number of other projects which depend on this one. I'm trying to convert one of these over, but if I try
type Car(serialNumber: string) =
interface MyProjectInterfaces.IVehicle with
member this.GetWheels() = 4
member this.GetLicensePlate() =
match LicensePlateService.GetLicencePlate(serialNumber) with
| Some(x) -> System.Nullable(x)
| None -> System.Nullable()
I get the error:
This expression was expected to have type 'string' but here has type 'Nullable<string>'
This doesn't seem to affect value types, so I'm assuming it's something to do with string being a reference type.
What do I do to solve this? Presumably I could rewrite the underlying interface to use F# and thus options, but there are other C# projects that implement the interface and I don't want to have to rewrite the whole solution in one go. Or am I doing F# completely wrong?
This is a confusion between C# 8's nullable references and nullable value types, aka Nullable<T>. If you look at the definition of Nullable<T>, you'll find:
public struct Nullable<T> where T : struct
which means it's only for value types. int? is short for Nullable<int>.
That's different from nullable references.
The nullability modifier for reference types doesn’t introduce a new type. Reference types are still nullable and compiling string? results in IL that’s still just System.String.
The difference at the IL level is the decoration of nullable modified types with a NullableAttribute.
In other words, it's just a compiler construct - one which isn't visible to F#.
match LicensePlateService.GetLicencePlate(serialNumber) with
| Some(x) -> x
| None -> null
will be the correct, albeit non-idiomatic replacement.
I am trying to figure out best way to model class inheritance in DocumentDb.
Say my classes are structured as
class A
property X
property Y
property Z
class B inherits from A
property W
Is there a built in support to handle this? If I use Lambda expressions in my client code, will they be able to distinguish between the types automatically?
Will the following query only return objects of type B back? Or will it also consider instances of base class A?
var bCollection = from o in client.CreateDocumentQuery<B>(collectionLink)
where X > 2
select o;
Is there a built in support to hand this? If I use Lambda expressions in my client code, will it be able to distinguish b/w the types automatically?
Yes, you can use lambda syntax in client side, as long as you specify the specific type in the generic method, as client.CreateDocumentQuery<YourType>(collectionLink).
Will following query only bring objects of type B back? Or will it also consider instances of base class A?
DocumentDB is a schemaless store and does not store type information. The overloaded generic methods provided in the client are syntactic sugar, to let you easily create queries.
All queries are evaluated against json documents, which do not have type information.
Inheritance scenario
So, if you fire a query for a property which is present only in your derived class, you will get values corresponding only to your derived class. But, if the property you are querying on is in both base class and derived class, you'll get back both results. For example, in your case filtering on W would give you results of only class B, but filtering on X, Y or Z would give you values for both classes A and B.
Classes with shared schema in same collection
Note that this does not just happen in the base-derived class scenario. Same behavior would happen if you have 2 separate classes, which do not inherit each other, but have a property with the same name. Querying on that property will return results of both classes.
For example, if you have 2 classes which are stored in the same collection:
class A1 { int x; }
class A2 { int x; }
Even if you form your query using the client.CreateDocumentQuery<A1>(collectionLink), results of both class A & B will be returned. As I mentioned earlier, the type specification in the client is just to make your life easier while forming the query.
I you want to be able to query different types of data, having shared schema elements, stored in the same collection - I would recommend having a separate property to store the type information manually and filtering on that property.
class DocumentDbData
{
string DataType;
DocumentDbData(string type) { DataType = type;}
}
class A1 : DocumentDbData
{
string x;
A1() : base("A1")
}
class A2 : DocumentDbData
{
string x;
A2() : base("A2")
}
The query, client.CreateDocumentQuery<A1>(collectionLink).Where(d => d.DataType == "A1" && d.x == "xvaluefilter") will now return only data for class A1.
I use node.js not .NET, so I don't recognize the CreateDocumentQuery<B> syntax. Is there a way to inspect the actual query that is sent by the .NET SDK to see if there is something added to the WHERE clause to restrict the results to type B? Alternatively, is there some field like _Type added to your documents without your intervention? I highly doubt the query clause you provided is so modified. I'm less sure that an _Type field isn't added, but I consider it less than 50% likely. My instinct is just that the <B> specification on the CreateDocumentQuery simply casts the returned objects to type B for you. I have no idea if that will cause an error if your result set included class A objects or if it will use null or some other default for missing fields.
Assuming all of the above, you'll probably have to model class hierarchy yourself. I have modeled this in two different ways:
Use a materialized array to indicate the entire class hierarchy for each document. So, ["Animal", "Mammal", "Cat"]. Then when you want all Mammals, you can query with WHERE "Mammal" IN c.class.
More recently, I've switched to having a separate field for each type that something is. So, isAnimal = true, isMammal = true, and isCat = true. This is more of a mixin approach, but you can still model class hierarchies this way.
I'm trying to define a Fortran derived type that has a private allocatable array. However, I would like to be able to access the array via a public pointer for use in other modules. E.g.
type,public :: test
private
real,allocatable :: a(:,:,:)
contains
real,pointer,dimension(:,:,:),public :: point => a
end type test
I just get a compiler error when attempting it like the above.
Is this possible without writing a subroutine that does the pointing for me?
No.
The syntax error is perhaps because you have the pointer component in the type bound procedure part of the type definition (after the contains), not in the component part (before the contains).
Beyond syntax, there are some problems with what you want to do:
You cannot associate a pointer with a component of a type definition. Pointers can be associated with components of objects (a subobject). Similarly, you cannot associate a pointer with something that doesn't have the target attribute. Types and components of types can't have the target attribute. Variables of that type, or objects pointed at by pointer components of an object may have the target attribute.
You cannot associate a pointer with something that isn't allocated. If something isn't allocated then there isn't anything to point at.
An initializer for a pointer component cannot refer to something that is allocatable. In addition to the target attribute the thing that it refers to must have the SAVE attribute. As the case with the TARGET attribute, variables have the save attribute, not type or component definitions.
Associating a pointer with a component of an object may defeat the point of making the component private in the first place. This leads to the question - what are you trying to do?
Any good site or explanation on what is a ref class and when to declare a class to be a "ref class"?
The explanation on msdn wasn't enough for me,
base_type(optional)
A base type. A ref class or ref struct can inherit from zero or more managed interfaces and zero or one ref types. A value class or value struct can only inherit from zero or more managed interfaces.
ref
The ref keyword tells the compiler that the class or structure will be allocated on the heap and a reference to it will be passed to functions or stored in class members. The value keyword tells the compiler that all of the data in the class or structure is passed to functions or stored in members.
Basically, a ref class is a CLR class. It's the equivalent of class in C#.
This creates a reference type managed by the CLR. If you want to make a class that's usable from C#, you'd normally create a ref class. (ref struct, by the way, does exactly the same thing, but with C++'s standard class vs. struct default accessibility rules.)
Also, just for reference - in order to make a value type (struct in C#), you'd use value class or value struct.
A good explanation of many of these new keywords is Herb Sutter's post on C++/CLI Keywords. This is a useful reference if you're new to C++/CLI, but have a solid C++ background.
I think i am lost with basics itself. What is the difference between these two. String object is an instance of String Class.
var guru:Object = new Object();
var guru:String = new String();
An object is a basic object. It has very few intrinsic properties and methods. More detail here
A string is an extended object that has the properties and methods relevant to strings. More detail here
If you're really not sure, I'd suggest looking up the answer here:
http://livedocs.adobe.com/flex/3/html/help.html?content=03_Language_and_Syntax_11.html
Briefly, it states:
String data type
The String data type represents a
sequence of 16-bit characters. Strings
are stored internally as Unicode
characters, using the UTF-16 format.
Strings are immutable values, just as
they are in the Java programming
language. An operation on a String
value returns a new instance of the
string. The default value for a
variable declared with the String data
type is null. The value null is not
the same as the empty string (""),
even though they both represent the
absence of any characters.
Object data type
The Object data type is defined by the
Object class. The Object class serves
as the base class for all class
definitions in ActionScript. The
ActionScript 3.0 version of the Object
data type differs from that of
previous versions in three ways.
First, the Object data type is no
longer the default data type assigned
to variables with no type annotation.
Second, the Object data type no longer
includes the value undefined, which
used to be the default value of Object
instances. Third, in ActionScript 3.0,
the default value for instances of the
Object class is null.
If that doesn't satisfy your question, you're going to have to get more specific.
This guide can help you with basic Object Oriented questions regarding ActionScript 3.
The reference guide for String states that String inherits directly from Object.
The String class provides a bunch of useful methods that help with string manipulation on top of the few methods that Object provides (like toString()).