May 22, 2020
On 5/22/20 5:39 PM, Vinod K Chandran wrote:
> On Friday, 22 May 2020 at 20:51:20 UTC, Steven Schveighoffer wrote:
>> On 5/22/20 4:04 PM, Vinod K Chandran wrote:
>>> [...]
>>
>> Yes. What you cannot do is this (which I hope doesn't compile in VB.net, but I wouldn't be surprised):
>>
>> Dim sampleList As New List(Of Child)
>> sampleList.Add(New Base(10))
>>
>> Which is the equivalent of what you were requesting.
>>
> Nope--
> List(Of Base) will contain an instance of a Child.
> So in the same manner, i want
> void function(Base) = fnPtr wiil work with
> void function(Child)
> 

That is the opposite of what you are thinking. A function pointer has to be valid based on its parameter types. Covariant functions are allowed.

This is OK:

void function(Child) fptr;

void foo(Base) {}

fptr = &foo; // OK! it's fine to call fptr with a Child, because it is a Base as well

void function(Base) fptr2;

void foo2(Child) {}

fptr2 = &foo2; // Error! if you called fptr2 with a Base that is NOT a Child, bad things will happen.

This is more clear if you actually try calling them:

fptr2(new Base); // the compiler should allow this
foo2(new Base); // but would not allow this

So why should fptr2 be allowed to point at foo2?

-Steve
May 22, 2020
On Fri, May 22, 2020 at 09:39:16PM +0000, Vinod K Chandran via Digitalmars-d-learn wrote: [...]
> So in the same manner, i want
> void function(Base) = fnPtr wiil work with
> void function(Child)

You cannot, because that's type unsafe:

	class Base {}
	class Derived : Base {
		void derivedFunc() {}
	}
	class Another : Base {}

	void derivedFunc(Derived d) { d.derivedFunc(); }

	void function(Base) funPtr;
	funPtr = derivedFunc; // suppose this was allowed

	Base obj = new Another;
	funPtr(obj); // crash: obj does not have derivedFunc()


T

-- 
Век живи - век учись. А дураком помрёшь.
May 23, 2020
On Friday, 22 May 2020 at 22:44:17 UTC, H. S. Teoh wrote:
> On Fri, May 22, 2020 at 09:39:16PM +0000, Vinod K Chandran via Digitalmars-d-learn wrote: [...]
>> So in the same manner, i want
>> void function(Base) = fnPtr wiil work with
>> void function(Child)
>
> You cannot, because that's type unsafe:
>
> 	class Base {}
> 	class Derived : Base {
> 		void derivedFunc() {}
> 	}
> 	class Another : Base {}
>
> 	void derivedFunc(Derived d) { d.derivedFunc(); }
>
> 	void function(Base) funPtr;
> 	funPtr = derivedFunc; // suppose this was allowed
>
> 	Base obj = new Another;
> 	funPtr(obj); // crash: obj does not have derivedFunc()
>
>
> T

Yeah, I understand that. And i just changed my code. Thanks for the guidance. :)
May 23, 2020
On Friday, 22 May 2020 at 22:40:50 UTC, Steven Schveighoffer wrote:
> On 5/22/20 5:39 PM, Vinod K Chandran wrote:
>> [...]
>
> That is the opposite of what you are thinking. A function pointer has to be valid based on its parameter types. Covariant functions are allowed.
>
> This is OK:
>
> void function(Child) fptr;
>
> void foo(Base) {}
>
> fptr = &foo; // OK! it's fine to call fptr with a Child, because it is a Base as well
>
> void function(Base) fptr2;
>
> void foo2(Child) {}
>
> fptr2 = &foo2; // Error! if you called fptr2 with a Base that is NOT a Child, bad things will happen.
>
> This is more clear if you actually try calling them:
>
> fptr2(new Base); // the compiler should allow this
> foo2(new Base); // but would not allow this
>
> So why should fptr2 be allowed to point at foo2?
>
> -Steve

Thank you for the guidance. I got the point. :)
1 2
Next ›   Last »