I want simulate a functional programming Option class with map and flatMap but It can't work in Unity engine. as a example, I create three .cs file as follow:
1 .NET 2.0 default delegate is't covariance and contravariance, so I override it.
using System;
namespace Functional
{
public delegate R Func<out R>();
public delegate R Func<in A, out R>(A a);
public delegate R Func<in A, in B, out R>(A a, B b);
public delegate R Func<in A, in B, in C, out R>(A a, B b, C c);
public delegate R Func<in A, in B, in C, in D, out R>(A a, B b, C c, D d);
public delegate R Func<in A, in B, in C, in D, in E, out R>(A a, B b, C c, D d, E e);
public delegate void Action<in A>(A a);
}
2 define Option classes
using UnityEngine;
using System.Collections;
using Functional;
namespace Lorance.RxScoket.Util {
public interface IOption<out T> {
void Foreach (Action<T> func);
IOption<R> Map<R> (Func<T, R> func);
IOption<R> FlatMap<R> (Func<T, IOption<R>> func);
bool IsEmpty();
T Get ();
}
public static class IOptionEx {
public static T GetOrElse<T> (this IOption<T> opt, Func<T> defaultValue) {
return opt.IsEmpty() ? defaultValue() : opt.Get();
}
}
public abstract class Option<T>: IOption<T> {
public abstract bool IsEmpty ();
public abstract T Get ();
public void Foreach (Action<T> act) {
if (!IsEmpty ()) {
act (Get());
}
}
public IOption<R> Map<R>(Func<T, R> func) {
if (IsEmpty ())
return new None<R> ();
else
return new Some<R>(func (Get()));
}
public IOption<R> FlatMap<R>(Func<T, IOption<R>> func) {
if (IsEmpty ())
return new None<R> ();
else
return func (Get());
}
public static Option<T> apply(T value){
if (value == null)
return new None<T> ();
else
return new Some<T> (value);
}
public static Option<T> Empty = new None<T>();
}
public class Some<T> : Option<T> {
T value;
public Some(T value){
this.value = value;
}
public override bool IsEmpty() {return false;}
public override T Get() {
return value;
}
}
public class None<T> : Option<T> {
public override bool IsEmpty() {return true;}
public override T Get() {
throw new NoSuchElementException ("None.Get()");
}
}
public class NoSuchElementException : System.SystemException {
public NoSuchElementException(string message)
: base(message)
{
}
}
}
3 crash example
using UnityEngine;
using System.Collections;
using Lorance.RxScoket.Util;
public class TestFlatMap : MonoBehaviour {
// Use this for initialization
void Start () {
Dog d = new Dog ();
IOption<Dog> dogOpt = new Some<Dog> (d);
//ok
Dog dog = dogOpt.GetOrElse<Dog> (() => new Dog());
//crash at this point. does it care about polymorphism? but it works on pure .net project
Animal animal = dogOpt.GetOrElse<Animal> (() => new Cat());
print ("r2 -" + animal);
}
}
public class Animal {}
public class Dog: Animal {}
public class Cat: Animal {}
Does someone also meets the problem and how to solve it?
a complete crash example at : https://github.com/Unlimited-Works/crash-eg
Get the crash with this step:
1. Open the project with unity editor.
2. Open scene 'eg.unity'.
3. Press play.
4. result: Unity crashes.
it crashes when executing 'TestFlapMap.cs':
Animal animal = dogOpt.GetOrElse (() => new Cat());
if you also encounter the promblem please give me a vote at unity issues, it has delay one month ago and not any response from Unity official.
thanks
Related
I know this is a basic question but I really struggle with this :(
First class:
public class A{
C c= new C();
B b= new B();
public static void main(String[] args) {
b.start();
System.out.println(c.getSomething());
}
}
Second Class:
public class B{
C c= new C();
public void start(){
c.setSomething(2);
}
}
Third Class:
public class C{
int x;
public int getSomething() {
return x;
}
public void setSomething(int x) {
this.x = x;
}
}
Now I know I make a new object in class A thats why the sysout returns null.
How can I make it so that I GET the value 2 instead of null in class A, and that I'm able to SET things in class B.
So I stay at the same object so to say.
I just want to be able to SET things in class B and GET that same value from the setter-getter-class-C, in class A. Please help
Thanks in advance, Jimme
I just want to know if i have a code like -
public class A {
static String b = "hello";
public static String a() {
String b = b();
String d = d(b);
String e = e(d, b);
return e;
}
public static String b() {
return b;
}
public static void c(String c) {
b = c;
}
public static String d(String d) {
return d;
}
public static String e(String e1, String e2) {
return e1 + e2;
}
}
than how will i write the test cases for this using power mockito since all the methods are static and a method is even void too.
The first thing i got is that i only need to write the test case for method a since it is using all the other methods so all others will also get cover in it.
Can anybody help me and tell me how i can test this .
how interface knows which class method to call?
it is the correct code? or not
namespace IntExample
{
interface Iinterface
{
void add();
void sub();
}
public partial class Form1 : Form,Iinterface
{
public Form1()
{
InitializeComponent();
}
public void add()
{
int a, b, c;
a = Convert.ToInt32(txtnum1.Text);
b = Convert.ToInt32(txtnum2.Text);
c = a + b;
txtresult.Text = c.ToString();
}
public void sub()
{
int a, b, c;
a = Convert.ToInt32(txtnum1.Text);
b = Convert.ToInt32(txtnum2.Text);
c = a - b;
txtresult.Text = c.ToString();
}
private void btnadd_Click(object sender, EventArgs e)
{
add();
}
private void button2_Click(object sender, EventArgs e)
{
sub();
}
class cl2 : Form1,Iinterface
{
public void add()
{
int a, b, c;
a = Convert.ToInt32(txtnum1.Text);
b = Convert.ToInt32(txtnum2.Text);
c = a + b;
txtresult.Text = c.ToString();
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
Interface does not "know" which class method to call. It just defines what methods are available.
The code you posted would not compile since cl2 does not implement sub method but it's pretty much meaningless anyway.
I have no idea what you are trying to do so I will give example for valid usage of the interface.
You can have several forms implementing that interface, then in your main form you can choose which of those forms to show according to index or name.
So, to store all the forms you can use generic list:
List<Iinterface> forms = new List<Iinterface>();
Adding to the list all the forms implementing the interface:
forms.Add(new Form1());
forms.Add(new Form2());
forms.Add(new Form3());
//...
Then you can show specific form and call a method from the interface:
//find by index:
forms[index].Show();
forms[index].add();
//find by name:
string name="form 2";
Iinterface form = forms.Find(f => f.Name == name);
if (form != null)
{
form.Show();
form.add();
}
I know there is a Comparable interface, trying to figure out how to write my own.
Here's the interface
public interface MyComparable {
public boolean lt(Object other);
}
and a class that implements it and packages an int (yes, I know there is an Integer class)
public class MyInteger implements MyComparable {
private int value;
public MyInteger(int v)
{ value = v; }
public void set(int v)
{ value = v; }
public int get()
{ return value; }
public boolean lt(MyInteger other)
{ return get() < other.get(); }
}
I get " MyInteger is not abstract and does not override abstract method eq(Object) in MyInteger error". MyComparable doesn't declare an eq method. So it's comping from the superclass but I don't understand.
I am new to RhinoMocks, and I am trying to write a test as shown
I have classes like these
public class A
{
public void methodA(){}
}
public class B
{
public void methodB(A a)
{
a.methodA();
}
}
And i am trying to test it like this
A a = MockRepository.GenerateMock<A>();
public void ShouldTest()
{
B b = new B();
b.methodB(a);
a.AssertWasCalled(x=>x.methodA());
a.VerifyAllExpectations();
}
But it is giving the error as shown:
System.InvalidOperationException : No expectations were setup to be verified, ensure that the method call in the action is a virtual (C#) / overridable (VB.Net) method call.
How do I test methodB then?? Can someone help??
Rhino mock creates proxy class when you call MockRepository.Generate *** method. This means that it extends your type. If you don't declare any abstraction you cannot make any derivation which is essential in any mocking framework.
You can do two things
Create an interface (better design)
Make the member virtual (this will allow RhinoMocks to derive from your type and create a proxy for the virtual member
Sample code
public interface IA { void methodA();}
public class A:IA{public void methodA() { }}
public class B
{
public void methodB(IA a)
{
a.methodA();
}
}
[TestFixture]
public class Bar
{
[Test]
public void BarTest()
{
//Arrange
var repo = MockRepository.GenerateMock<IA>();
//Act
B b = new B();
b.methodB(repo);
//Assert
repo.AssertWasCalled(a => a.methodA());
repo.VerifyAllExpectations();
}
}
You have concrete classes with no virtual methods and no interfaces. You can't mock anything.
Update:
Here's one way to do it:
public interface IA
{
void methodA();
}
public class A : IA
{
public void methodA(){}
}
public class B
{
public void methodB(IA a)
{
a.methodA();
}
}
Then use
IA a = MockRepository.GenerateMock<IA>();