Experimenting with mapped types

For my understanding a mapped type is a new type and needs all the methods and properties you want to use.
So you can map the different platform classes to a single type signature.

As joseasl say’s. This sounds like a extension.

1 Like

I guess if you expose all the method of the original type, then the mapped type is not so cross-platform any more, as you can use methods that are not available on all platforms.

1 Like

Ok, clear.

Thanks for the clarification, all of you.

It is, yes. The goal for mapped types is create a new “virtual” type with different exposed Apis than the original. If all the original members were still available, what would be gained by the new type?

If you simply want the type under a different name, you should use a type alias.

If you want to add additional members to the type, on top of the existing ones, you’ll want to look at defining an Extension, instead.

How would you remove members you want to hide though? I cannot envision a non-ugly way to do that, even if we supported such a mode.

exactly that, yes.

1 Like

As always, I was looking for a way to do the impossible, in this case a way to re-implement methods and properties on sealed classes (I know dirty ,not OOP, and I should not do that - but it would save so much time implementing some things…).

:wink:

Note that mapped classes, like extensions, cannot add new storage to a class. To just add new behavior or helper methods, an extension is the best option, IMHO.

Just add the implementation with the attribute [EditorBrowsable(Never)] to a mapped method to hide it.

Then you have to declare the member for the only reason to hide it?
This would be ugly… :wink:

1 Like

I agree - in this case I wanted to implement Substring is such a way that it never throws an exception, but return an empty string instead - what I can do with a mapped type. But because I have to map all other members by hand (I don’t want to hide anything) it just isn’t worth it.

Just to test my luck :smiley:
Possible syntax:

type Test = public class mapped to String with default mappings //Everything that is not defined is mapped 1 on 1
Public Method SubString(start: Integer; lenght: Integer); //reimplementation
Public Method SubString(Start: Integer); //reimplementation
[EditorBrowsable(never)]Public Method Clone: Object; //Hides the clone method from the mapped class
end;

Yes.

But when you have a class with 100 methods and 50 properties, and you need a mapped class where 4 methods should be hidden, 5 methods should be reimplemented (or mapped in a special way) while the other ones should stay as they were, this saves a lot of work.

If I remember correctly, when a extension has the same signature (name and parameters types) as an existing method of the class, the existing method takes precedence.

I think you missintepret mapped classes.
They are here to help for cross platform, so you can have a single Signature across Platforms.
But maybe I don’t really understand what you try to solve. (happens all the time with a lot of things)

Correct - you can only create new methods with an extension class.

Correct, they are intended for cross platform, but can be used for other purposes (I do not work cross platform, only windows).

In your case maybe a possibility to switch the precedence of extensions
should be enough. This can be a usefull feature. @mh ?

1 Like

I agree - that should be an even better solution.

Hmm. Allow Extensions to hide existing members, basically? interesting idea. @ck?

2 Likes

Problem with this is that it gives the possibility to redefine a method within a dll.
Whoever references this dll will get the new implementation (regardless if they want it or not).
That is why (I think) extensions never hide existing members.

So I that is why I went to the mapped classes - this will define a new class name where the methods are redefined, so it will not have any side effects on other software.

Extensions only apply when their namespace is in scope, though.

I agree, but it is dangerous.

I am going to look if I can port my VB code for shadow classes (allows for a kind of inheritance of sealed classes) that I build a few years ago - see if I can do it with an aspect; code will be like:

[Shadows(MySealedClass)]
type MyClass = public Class
public method ReimplementedMethod;
end;

Everything that is not defined will behave as it did in the MySealedClass - Should be easier with Oxygene then it was with VB because of the duck-typing.

1 Like