Let's say i have an instruction :
CharTermAttribute cattr = stream.addAttribute(CharTermAttribute.class);
Note : CharTermAttribute.class is a class
In F#, it's could be like this :
let cattr:CharTermAttribute = stream.addAttribute(..........)
How can i say to F# that (CharTermAttribute.class ) is a class
I'm not a Lucene.NET expert, but if you are trying to call this AddAttribute method then in the .NET version, this is a generic method and you can call it as:
let cattr = stream.addAttribute<CharTermAttribute>()
According to Ali Yacine question and the error: AddAttribute() only accepts an interface that extends Attribute, you must use interface of the types CharTermAttribute, etc in generic constraints of AddAttribute<> method. for example below:
var cattr = stream.AddAttribute<ICharTermAttribute>();
var another= stream.AddAttribute<IPositionIncrementAttribute>();
Related
I have some model classes like this one:
class Environment {
String _oidClientId;
String _oidIssuerUrl;
get issuerUrl => _oidIssuerUrl;
set issuerUrl(String pIssuerUrl) => this._oidIssuerUrl = pIssuerUrl;
get clientId => _oidClientId;
set clientId(String pClientID) => this._oidClientId = pClientID;
}
I've tried to create an instance and use cascade to set values on it:
var env = Environment.dev()
..clientId("my.id.public")
..issuerUrl("https://demo.myserver.com")
But the compiler is given me this error:
Undefined name 'clientId'.
Try correcting the name to one that is defined, or defining the name.dart(undefined_identifier)
If I change the code, renaming the getter(due a duplicated name) and turn the setter into a void clientId(String pClientID) then the compiler doesn't complains. but I would prefer not doing this.
Am I missing something?
You misused the setters. The following code should work:
var env = Environment.dev()
..clientId = "my.id.public"
..issuerUrl = "https://demo.myserver.com";
Does swift not have nested classes??
For example, I can't seem to access the property test of the master class from the nested class.
class master{
var test = 2;
class nested{
init(){
let example = test; //this doesn't work
}
}
}
Swift's nested classes are not like Java's nested classes. Well, they're like one kind of Java's nested classes, but not the kind you're thinking of.
In Java, an instance of an inner class automatically has a reference to an instance of the outer class (unless the inner class is declared static). You can only create an instance of the inner class if you have an instance of the outer class. That's why in Java you say something like this.new nested().
In Swift, an instance of an inner class is independent of any instance of the outer class. It is as if all inner classes in Swift are declared using Java's static. If you want the instance of the inner class to have a reference to an instance of the outer class, you must make it explicit:
class Master {
var test = 2;
class Nested{
init(master: Master) {
let example = master.test;
}
}
func f() {
// Nested is in scope in the body of Master, so this works:
let n = Nested(master: self)
}
}
var m = Master()
// Outside Master, you must qualify the name of the inner class:
var n = Master.Nested(master:m)
// This doesn't work because Nested isn't an instance property or function:
var n2 = m.Nested()
// This doesn't work because Nested isn't in scope here:
var n3 = Nested(master: m)
This solution is sort of similar to how I use it in C#, and I have successfully tested it in Xcode.
Here's a breakdown of the process:
Your nested class needs to be made optional so you don't have to initialize it, so theres a '?' in the declaration; if you initialize both your parent class and your nested class, you end up with a 'recursion' effect and an error is generated
Create a regular function that receives an argument of the same type as your main class
Pass that argument to your nested class (this can go into the nested object's normal initializer). - Since objects are passed by reference by default, there's nothing special to get your nested class to link to the parent class
Inside your nested class, you need a variable of the same type as your parent class
From here on out set up everything as you normally would.
In the code execution area, your nested class object also needs to be regarded as optional (hence the '?'). If you forget about it, Xcode will add it anyways.
In this example, I wanted to design a keyword "set," so when I set variables, I can type:
testClass.set.(and then a descriptive method name)
Here is the code, and its goal is to output "test" in the console, after the value is set via the nested object:
class testClass
{
var test_string:String = ""
var set: class_set?
func construct_objects(argument: testClass)
{
self.set = class_set(argument: argument)
}
class class_set
{
var parent:testClass
init(argument: testClass)
{
parent = argument
}
func test_string_to_argument(argument: String)
{
parent.test_string = argument
}
}
}
var oTestClass = testClass()
oTestClass.construct_objects(oTestClass)
oTestClass.set?.test_string_to_argument("test")
print(oTestClass.test_string)
Nested for Swift and Java
Swift has Nested Types definitions
Java has more complex hierarchy of nested class
Swift's Nested is more similar to Java's Static Nested, as a result you do not have an access to properties of outer class. To get access of outer class you can pass it as a parameter.
I am working on a project where the F# code will be consumed by other .NET projects - so I am using classes. I created a code file like this:
namespace StockApplication
open System
type Stock =
{Symbol: String;
DayOpen: Decimal;
Price: Decimal;
}
member x.GetChange () =
x.Price - x.DayOpen
member x.GetPercentChange() =
Math.Round(x.GetChange()/x.DayOpen,4)
This works fine when I consume it from some unit tests written in C#. For example:
[TestMethod]
public void CreateStock_ReturnsValidInstance()
{
Stock stock = new Stock("TEST", 10, 10.25M);
Assert.IsNotNull(stock);
}
I then went to create another file with another class. This class uses the 1st class so I made sure it was below the original class in VS2012. When I created the next class, I can see it available via intellisense.
namespace StockApplication
open System
type StockTicker() =
member x.GetStock () =
StockApplication.Stock
However, every attept to either new it or refer it gives me the same error:
Error 1 The value, constructor, namespace or type 'Stock' is not
defined
Does anyone have any insight on why I can just simply new up a class that I created in F# in another F# file?
Thanks in advance.
Your C# test having Stock stock = new Stock("TEST", 10, 10.25M); that was compiled without a problem prompts to believe that F# constructor for the Stock should look the same. But this is not true and, perhaps, was the source of your confusion.
Your original
type Stock =
{Symbol: String;
DayOpen: Decimal;
Price: Decimal; }
is of F# type Record indeed, not an ordinary class. The following excerpt from MSDN applies:
Record construction also differs from class construction. In a record type, you cannot define a constructor.
Meaning that
let stock = Stock("ABC"; 10M; 10M)
will produce error FS0039: The value or constructor 'Stock' is not defined while
let stock = { Symbol = "ABC"; DayOpen = 10M; Price = 10M; }
will successfully create a record instance.
In order to build an instance of type Stock in your second F# type StockTicker you should use record construction syntax, something like
member x.GetStock () = { Symbol = "MSFT"; DayOpen = 32M; Price = 32.5M; }
which compiles without any problems.
When it comes to interop use of F# record from C# the latter follows the syntax that you applied in your test method.
OK, after digging into this reference (MSDN was 0 help) here, I found the answer.
Here is the syntax for the Stock class:
namespace StockApplication
open System
type Stock = class
val Symbol: String
val DayOpen: Decimal
val Price: Decimal
new (symbol, dayOpen, price) =
{
Symbol = symbol;
DayOpen = dayOpen;
Price = price
}
member x.GetChange () =
x.Price - x.DayOpen
member x.GetPercentChange() =
Math.Round(x.GetChange()/x.DayOpen,4)
end
And here is the syntax for the consuming class:
namespace StockApplication
type StockTicker() =
member x.GetStock () =
let y = new Stock("AET",1m,1m)
y.DayOpen
Consider the following types
type
TRecs = array[0..100000] of TRec;
PRecs = ^TRecs;
TRecObject = class
private
fRecs: PRecs;
public
constructor Create;
property Recs: PRecs read fRecs;
end;
I would like to make TRec a generic parameter. The problem is that I need to place outside of the class scope. Because something like
T<MyType>Object = class
private
fRecs: ^array[0..100000] of MyType;
public
property Recs: ^array[0..100000] of MyType read fRecs
end
is not possible.
Making PRecs a parameter also is not an option because there is TRec-related code in my actual object.
Is there a solution in modern Object Pascal? And if not, just curious is there any other generic-enabled language that can solve something like this?
I'm not entirely sure I understand your question, but I think you are looking for something like this:
type
TMyObject<T> = class
public
type
PArr = ^TArr;
TArr = array[0..100000] of T;
private
fRecs: PArr;
public
property Recs: PArr read fRecs
end;
That said, I can't see the point of that class. You could just use TList<T> from Generics.Collections.
And if your need an array, then you can use a dynamic array: TArray<T> or array of T, as you prefer.
You got your generic syntax a bit muddled up. Try this:
TRecArray<T> = array[0..100000] of T;
TGenericRecObject<T> = class
private
FRecs: TRecArray<T>;
public
property Recs: TRecArray<T> read FRecs;
end;
is there any way to query how many class inherit a class using linq and reflection ?
eg. how to know classes that inherit from System.Web.Mvc.ActionResult in System.Web.Mvc.dll
if you want to retrieve only classes, and not interfaces (IsAssignablefrom returns also interfaces), you should try
var t = typeof(System.Web.Mvc.ActionResult);
var asmb = Assembly.GetAssembly(t);//or get all assemblies you need and put next code in loop
return asmb.GetTypes().Where(x=>x.IsSubClassOf(t)).ToList();
You can do this with two methods, 1. IsAssignableFrom, 2. assembly.GetTypes:
var t = typeof(System.Web.Mvc.ActionResult);
var asmb = Assembly.GetAssembly(t);
return asmb.GetTypes().Where(x=>x.IsAssignableFrom(t) && x != t);