haxe operator overloading ==, Class not found error for template parameters - operator-overloading

I am trying to overload the equals (==) operator on a custom "pair" type, as follows:
private typedef Data<A, B> = { a: A, b: B }
abstract Pair<A, B>(Data<A, B>) {
public var a(get, set):A;
public var b(get, set):B;
public function equals1(lhs:Pair<A,B>, rhs:Pair<A,B>):Bool {
return (lhs.a == rhs.a && lhs.b == rhs.b);
}
#:op(X == Y) static public function equals(lhs:Pair<A,B>, rhs:Pair<A,B>):Bool {
return (lhs.a == rhs.a && lhs.b == rhs.b);
}
public inline function new(a:A, b:B) this =
{ a: a, b: b };
inline function get_a():A
return this.a;
inline function get_b():B
return this.b;
inline function set_a(v:A):A
return this.a = v;
inline function set_b(v:B):B
return this.b = v;
}
I am new to abstract classes and operator overloading. But the overloading part is almost copied verbatim from the haxe documentation example. The overloaded operator is intended to test equality of two pairs. However, when I compiled the code to neko, I get an error:
Pair.hx:11: lines 11-13 : Class not found : A
I am confused here, because as you can see immediately above the overloaded function, there is an plain function version "equals1", which compiles just fine. As soon as I add #:op(X == Y), the template parameter A in Pair is "not found".
My questions is how to make this overloading work, and what went wrong in my code?
Thanks in advance.
P.S. I am using Haxe Compiler 3.0.1, installed on Windows.
--Update--:
I looked over the code again, it seems that declaring the equals function "static" is causing the problem. If I add "static" to the plain function,
static public function equals1(lhs:Pair<A,B>, rhs:Pair<A,B>):Bool {
...
The same error is reported.
--update--:
The answer by back2dos is correct:
private typedef Data<A, B> = { a: A, b: B }
abstract Pair<A, B>(Data<A, B>) {
public var a(get, set):A;
public var b(get, set):B;
public function equals1(lhs:Pair<A,B>, rhs:Pair<A,B>):Bool {
return (lhs.a == rhs.a && lhs.b == rhs.b);
}
#:op(X == Y) static public function equals<A, B>(lhs:Pair<A,B>, rhs:Pair<A,B>):Bool {
return (lhs.a == rhs.a && lhs.b == rhs.b);
}
public inline function new(a:A, b:B) this =
{ a: a, b: b };
inline function get_a():A
return this.a;
inline function get_b():B
return this.b;
inline function set_a(v:A):A
return this.a = v;
inline function set_b(v:B):B
return this.b = v;
}
class Main {
static public function main() {
var p1 = new Pair(1,2), p2 = new Pair(1,5);
trace (p1 == p2);
}
}
Outputs:
Main.hx:34: false
Main.hx:35: true

The issue here is that type parameters declared on classes/abstracts are scoped to instances/values and thus are not defined in the static context.
Therefore, you need to parametrize the function:
#:op(X == Y) static public function equals1<A, B>(lhs:Pair<A,B>, rhs:Pair<A,B>):Bool;

Related

Where did Option[T] come from in Scala?

I'm still a noob in Scala development but I have found the Option[T] concept really awesome, specially the pattern matching when used with Some and None. I am even implementing it so some extent in a C# project I'm working on at the moment, but as there is no pattern matching in there is isn't really that awesome.
The real question is, where is the theory behind this object? is it something specific from Scala? Funcional languages? Where can I find more about it?
Most of the time I was thinking that it comes from the Haskell, and has a name of Maybe monad
But after a little research, I've found that there was some references on option types in SML papers, as #ShiDoiSi said. Moreover, it has the same semantics (Some/None) that Scala has.
The elderest paper I was able to find is that (circa '89) (see footnote on the 6th page)
You don't need pattern-matching to use Option. I have written it in C# for you below. Note that the Fold function takes care of anything that would otherwise be pattern-matched.
Pattern-matching is generally discouraged in favour of higher-level combinators. For example, if your particular function can be written using Select you would use it rather than Fold (which is equivalent to pattern-matching). Otherwise, assuming side-effect free code (and therefore, equational reasoning), you would essentially be re-implementing existing code. This holds for all languages, not just Scala or C#.
using System;
using System.Collections;
using System.Collections.Generic;
namespace Example {
/// <summary>
/// An immutable list with a maximum length of 1.
/// </summary>
/// <typeparam name="A">The element type held by this homogenous structure.</typeparam>
/// <remarks>This data type is also used in place of a nullable type.</remarks>
public struct Option<A> : IEnumerable<A> {
private readonly bool e;
private readonly A a;
private Option(bool e, A a) {
this.e = e;
this.a = a;
}
public bool IsEmpty {
get {
return e;
}
}
public bool IsNotEmpty{
get {
return !e;
}
}
public X Fold<X>(Func<A, X> some, Func<X> empty) {
return IsEmpty ? empty() : some(a);
}
public void ForEach(Action<A> a) {
foreach(A x in this) {
a(x);
}
}
public Option<A> Where(Func<A, bool> p) {
var t = this;
return Fold(a => p(a) ? t : Empty, () => Empty);
}
public A ValueOr(Func<A> or) {
return IsEmpty ? or() : a;
}
public Option<A> OrElse(Func<Option<A>> o) {
return IsEmpty ? o() : this;
}
public bool All(Func<A, bool> f) {
return IsEmpty || f(a);
}
public bool Any(Func<A, bool> f) {
return !IsEmpty && f(a);
}
private A Value {
get {
if(e)
throw new Exception("Value on empty Option");
else
return a;
}
}
private class OptionEnumerator : IEnumerator<A> {
private bool z = true;
private readonly Option<A> o;
private Option<A> a;
internal OptionEnumerator(Option<A> o) {
this.o = o;
}
public void Dispose() {}
public void Reset() {
z = true;
}
public bool MoveNext() {
if(z) {
a = o;
z = false;
} else
a = Option<A>.Empty;
return !a.IsEmpty;
}
A IEnumerator<A>.Current {
get {
return o.Value;
}
}
public object Current {
get {
return o.Value;
}
}
}
private OptionEnumerator Enumerate() {
return new OptionEnumerator(this);
}
IEnumerator<A> IEnumerable<A>.GetEnumerator() {
return Enumerate();
}
IEnumerator IEnumerable.GetEnumerator() {
return Enumerate();
}
public static Option<A> Empty {
get {
return new Option<A>(true, default(A));
}
}
public static Option<A> Some(A t) {
return new Option<A>(false, t);
}
}
}
Wikipedia is your friend: http://en.wikipedia.org/wiki/Option_type
Unfortunately it doesn't give any dates, but I'd bet that it's ML-origin predates Haskell's Maybe.

IComparer using Lambda Expression

class p {
public string Name { get; set; }
public int Age { get; set; }
};
static List<p> ll = new List<p>
{
new p{Name="Jabc",Age=53},new p{Name="Mdef",Age=20},
new p{Name="Exab",Age=45},new p{Name="G123",Age=19}
};
protected static void SortList()
{
IComparer<p> mycomp = (x, y) => x.Name.CompareTo(y.Name); <==(Line 1)
ll.Sort((x, y) => x.Name.CompareTo(y.Name));<==(Line 2)
}
Here the List.sort expects an IComparer<p> as parameter. And it works with the lambda
as shown in Line 2. But when I try to do as in Line 1, I get this error:
Cannot convert lambda expression to
type
System.Collections.Generic.IComparer'
because it is not a delegate type
I investigated this for quite some time but I still don't understand it.Maybe my understanding of IComparer is not quite good.Can somebody give me a hand ?
When you do ll.Sort((x, y) => x.Name.CompareTo(y.Name)); it uses the overload for Comparison<T>, not IComparer. Comparison<T> is a delegate, so you can use a lambda expression for it.
Comparison<p> mycomp = (x, y) => x.Name.CompareTo(y.Name); will work.
There's an existing solution you might refer to: https://stackoverflow.com/a/16839559/371531
This one uses Comparer<T>.Create introduced in .NET Framework 4.5.
IComparer is an interface, not a delegate.
You'll want to use the lambda expression on its .CompareTo(), not on the interface itself.
Use the following simple class:
public static class ComparerUtilities
{
class _Comparer<T> : Comparer<T>
{
Comparison<T> _comparison;
public _Comparer(Comparison<T> comparison)
{
_comparison = comparison;
}
public override int Compare(T x, T y)
{
return _comparison(x, y);
}
}
public static IComparer<T> FromComparison<T>(Comparison<T> comparison)
{
return new _Comparer<T>(comparison);
}
}

Can this extension method be improved?

I have the following extension method
public static class ListExtensions
{
public static IEnumerable<T> Search<T>(this ICollection<T> collection, string stringToSearch)
{
foreach (T t in collection)
{
Type k = t.GetType();
PropertyInfo pi = k.GetProperty("Name");
if (pi.GetValue(t, null).Equals(stringToSearch))
{
yield return t;
}
}
}
}
What it does is by using reflection, it finds the name property and then filteres the record from the collection based on the matching string.
This method is being called as
List<FactorClass> listFC = new List<FactorClass>();
listFC.Add(new FactorClass { Name = "BKP", FactorValue="Book to price",IsGlobal =false });
listFC.Add(new FactorClass { Name = "YLD", FactorValue = "Dividend yield", IsGlobal = false });
listFC.Add(new FactorClass { Name = "EPM", FactorValue = "emp", IsGlobal = false });
listFC.Add(new FactorClass { Name = "SE", FactorValue = "something else", IsGlobal = false });
List<FactorClass> listFC1 = listFC.Search("BKP").ToList();
It is working fine.
But a closer look into the extension method will reveal that
Type k = t.GetType();
PropertyInfo pi = k.GetProperty("Name");
is actually inside a foreach loop which is actually not needed. I think we can take it outside the loop.
But how?
PLease help. (C#3.0)
Using reflection in this way is ugly to me.
Are you sure you need a 100% generic "T" and can't use a base class or interface?
If I were you, I would consider using the .Where<T>(Func<T, Boolean>) LINQ method instead of writing your own Search function.
An example use is:
List<FactorClass> listFC1 = listFC.Where(fc => fc.Name == "BKP").ToList();
public static IEnumerable<T> Search<T>(this ICollection<T> collection, string stringToSearch)
{
Type k = typeof(T);
PropertyInfo pi = k.GetProperty("Name");
foreach (T t in collection)
{
if (pi.GetValue(t, null).Equals(stringToSearch))
{
yield return t;
}
}
}
There's a couple of things you could do -- first you could institute a constraint on the generic type to an interface that has a name property. If it can only take a FactorClass, then you don't really need a generic type -- you could make it an extension to an ICollection<FactorClass>. If you go the interface route (or with the non-generic version), you can simply reference the property and won't have a need for reflection. If, for some reason, this doesn't work you can do:
var k = typeof(T);
var pi = k.GetProperty("Name");
foreach (T t in collection)
{
if (pi.GetValue(t, null).Equals(stringToSearch))
{
yield return t;
}
}
using an interface it might look like
public static IEnumerable<T> Search<T>(this ICollection<T> collection, string stringToSearch) where T : INameable
{
foreach (T t in collection)
{
if (string.Equals( t.Name, stringToSearch))
{
yield return t;
}
}
}
EDIT: After seeing #Jeff's comment, this is really only useful if you're doing something more complex than simply checking the value against one of the properties. He's absolutely correct in that using Where is a better solution for that problem.
Just get the type of T
Type k = typeof(T);
PropertyInfo pi = k.GetProperty("Name");
foreach (T t in collection)
{
if (pi.GetValue(t, null).Equals(stringToSearch))
{
yield return t;
}
}

Convert Method Group to Expression

I'm trying to figure out of if there is a simple syntax for converting a Method Group to an expression. It seems easy enough with lambdas, but it doesn't translate to methods:
Given
public delegate int FuncIntInt(int x);
all of the below are valid:
Func<int, int> func1 = x => x;
FuncIntInt del1 = x => x;
Expression<Func<int, int>> funcExpr1 = x => x;
Expression<FuncIntInt> delExpr1 = x => x;
But if i try the same with an instance method, it breaks down at the Expressions:
Foo foo = new Foo();
Func<int, int> func2 = foo.AFuncIntInt;
FuncIntInt del2 = foo.AFuncIntInt;
Expression<Func<int, int>> funcExpr2 = foo.AFuncIntInt; // does not compile
Expression<FuncIntInt> delExpr2 = foo.AFuncIntInt; //does not compile
Both of the last two fail to compile with "Cannot convert method group 'AFuncIntInt' to non-delegate type 'System.Linq.Expressions.Expression<...>'. Did you intend to invoke the method?"
So is there a good syntax for capturing a method grou in an expression?
thanks,
arne
How about this?
Expression<Func<int, int>> funcExpr2 = (pArg) => foo.AFuncIntInt(pArg);
Expression<FuncIntInt> delExpr2 = (pArg) => foo.AFuncIntInt(pArg);
It is also possible to do it using NJection.LambdaConverter a Delegate to LambdaExpression converter Library
public class Program
{
private static void Main(string[] args) {
var lambda = Lambda.TransformMethodTo<Func<string, int>>()
.From(() => Parse)
.ToLambda();
}
public static int Parse(string value) {
return int.Parse(value)
}
}
I use property instead of method.
public class MathLibrary
{
public Expression<Func<int, int>> AddOne {
get { return input => input + 1;}
}
}
Using above

Question about var type

I am new to C# 3.0 var type. Here I have a question about this type. Take the following simple codes in a library as example:
public class MyClass {
public var Fn(var inValue)
{
if ( inValue < 0 )
{
return 1.0;
}
else
{
return inValue;
}
}
}
I think the parameter is an anonymous type. If I pass in a float value, then the Fn should return a float type. If a double value type is passed in, will the Fn return a double type? How about an integer value type as input value?
Actually, I would like to use var type with this function/method to get different return types with various input types dynamically. I am not sure if this usage is correct or not?
You can't use var for return values or parameter types (or fields). You can only use it for local variables.
Eric Lippert has a blog post about why you can't use it for fields. I'm not sure if there's a similar one for return values and parameter types. Parameter types certainly doesn't make much sense - where could the compiler infer the type from? Just what methods you try to call on the parameters? (Actually that's pretty much what F# does, but C# is more conservative.)
Don't forget that var is strictly static typing - it's just a way of getting the compiler to infer the static type for you. It's still just a single type, exactly as if you'd typed the name into the code. (Except of course with anonymous types you can't do that, which is one motivation for the feature.)
EDIT: For more details on var, you can download chapter 8 of C# in Depth for free at Manning's site - this includes the section on var. Obviously I hope you'll then want to buy the book, but there's no pressure :)
EDIT: To address your actual aim, you can very nearly implement all of this with a generic method:
public class MyClass
{
public T Fn<T>(T inValue) where T : struct
{
Comparer<T> comparer = Comparer<T>.Default;
T zero = default(T);
if (comparer.Compare(inValue, zero) < 0)
{
// This is the tricky bit.
return 1.0;
}
else
{
return inValue;
}
}
}
As shown in the listing, the tricky bit is working out what "1" means for an arbitrary type. You could hard code a set of values, but it's a bit ugly:
public class MyClass
{
private static readonly Dictionary<Type, object> OneValues
= new Dictionary<Type, object>
{
{ typeof(int), 1 },
{ typeof(long), 1L },
{ typeof(double), 1.0d },
{ typeof(float), 1.0f },
{ typeof(decimal), 1m },
};
public static T Fn<T>(T inValue) where T : struct
{
Comparer<T> comparer = Comparer<T>.Default;
T zero = default(T);
if (comparer.Compare(inValue, zero) < 0)
{
object one;
if (!OneValues.TryGetValue(typeof(T), out one))
{
// Not sure of the best exception to use here
throw new ArgumentException
("Unable to find appropriate 'one' value");
}
return (T) one;
}
else
{
return inValue;
}
}
}
Icky - but it'll work. Then you can write:
double x = MyClass.Fn(3.5d);
float y = MyClass.Fn(3.5f);
int z = MyClass.Fn(2);
etc
You cannot use var as a return type for a method.