scriptsharp icon indicating copy to clipboard operation
scriptsharp copied to clipboard

Import a BaseClass

Open andekande opened this issue 13 years ago • 2 comments

Hi, in 0.7.4 I did this:

[Imported]
public abstract class ControlBase
{
    [PreserveCase]
    public int Version;
}

public class ControlA : ControlBase
{
}

So I was happy not to get generated all Code of Base class which I implemented for IntelliSense sake. Also I worked around TypeConnversions with this (since keyword dynamic is missing). Now in 0.8 it breaks, because the Base Type gets registered in in JS and there is a forced call to the contructor in ControlA. Chances are a [ScriptImport] class doesn't take part in JS inheritance in the future?

andekande avatar Jan 21 '13 15:01 andekande

ScriptImport by itself isn't sufficient ... consider the case where you were simply defining an actual type that does exist in a script library so it can be referenced... and do want the prototype chain to be constructed. You're asking for a feature I've considered for interfaces - contract only interfaces, that are there for defining the c# contract, rather than runtime contract.

The question to also ask is why not generate ControlBase? There is goodness in trying to maximize how much of what is visible in c# is also represented in script ... yes, there are cases where it isn't when it comes to imported types. But I think of those as exceptions. Is yours an exception case as well? What would happen if some code did an "is" check or "as" safe cast, and referenced ControlBase?

Looks like a breaking change wrt to 0.8 though ... there have been fair number of changes in this area. Will probably leave this open while thinking about contract types (perhaps through the application of [ScriptIgnore]) in the future.

nikhilk avatar Jan 21 '13 15:01 nikhilk

Well always good to hear those concerns one not thought of. I agree in the use case for beeing able to inherit from a JS class at runtime. I need it vice versa - inherit in designtime but not runtime. An interface you name it. Should be something like what [ScriptObject] and [ScriptExtension] does in combination - ability to declare methods and properties at same time.

In my case I removed now [ScriptImport] form Base class, but that runs into another issue, explained and further commented here: #295

My "exception case" is to take advantage of implicit typing in JS so I do not need code like this:

ControlA ctl = jQuery.ParseJsonData<ControlA>("{...}");
ControlB ctl = jQuery.ParseJsonData<ControlB>("{...}");

if (ctl is ControlA)
{
    ctl.DoA();
}
if (ctl is ControlB)
{
    ctl.DoB();
}

instead with a monster class declaring picked members of A and B:

ControlBase ctl = jQuery.ParseJsonData<ControlBase>("{...}");
//either one of those is undefined - On design time I know which to call
ctl.DoA();
ctl.DoB();

andekande avatar Jan 22 '13 11:01 andekande