In a class unit I have the type
type
TFolderType = (
dirRoot,
dirDatabase,
dirDocs,
dirConfig,
dirBackup,
dirDown,
dirUp,
dirScripts,
dirLicense,
dirImages,
dirMail,
dirProjects,
dirInput,
dirOutput
);
Now I would like to inherit this class from another class and add some extra elements
Is that in any way possible or can I do this in another way
I have thought of creating a class with all the elements as properties but I am not sure if that is the way to go
Like David says, it is not a class.
As an alternative, you can declare your bigger enumeration like:
type
TThingType = (dirThingA, dirRoot, dirDataBase, ..., dirOutput, dirThingY, dirThingZ);
And then declare your TFolderType as:
type
TFolderType = dirRoot..dirOutput;
Some concerns:
A folder 'needs' to be of type thing
The prefix dir 'needs' to be meaningful for both thing and folder
Current code that assumes enumeration index of the TFolderType elements needs to be rewritten, or add all 'new' elements in the back.
That is not class, rather it is an enumerated type. You cannot use inheritance with enumerated types. You will need to create a new enumerated type. Or perhaps solve the problem using something other than an enumerated type.
Related
I need a modifiable collection like a List or a Set to be passed as a parameter. Using Iterable doesn't guarantee this argument to have methods like add or remove.
Example method:
void foo(Iterable bar) {
bar.add(); // The method 'add' isn't defined for the type 'Iterable'.
}
Is there a class / interface for (modifiable) collections which guarantees those methods? If not, why?
There is not a modifiable type. Very early (before Dart 1) we had some other types in our hierarchy, but we decided to avoid including them because things were getting a bit too complex.
I still wish we'd shipped a List interface without the mutation members. 🤷
So the following code snippet
Set mySet = {1,2,3};
is an instance of type Set which is permissible, however what would the class of the set literal be. I have tried to search for this, however I have found no answer in the dart documentation.
A literal exists only in your source code. Asking for its "class" doesn't make a lot of sense.
Using a Set, Map, or List literal is just syntactic sugar for invoking a corresponding constructor. The Set factory constructor constructs a LinkedHashSet.
However, you'll see that LinkedHashSet is also abstract. Its factory constructor returns an instance of a private, internal class. You can see its typename via print(Set().runtimeType); the actual type might be different for different platforms and is unlikely to be useful to you.
I just started to study F# and accidentally wrote this binding
let List = 1
Now when I try to obtain List methods such as 'filter' I get this error
error FS0039: The field, constructor or member 'filter' is not defined.
Of course using method with full type name like Microsoft.FSharp.Collections.List.filter is still working.
I'm wondering why it is possible to use type name as identifier in F# and how I can set back name List to type List from Microsoft.FSharp.Collections.
When I tried to reassign like this
type List = Microsoft.FSharp.Collections.List<'T>
I get
Error FS0039: The type parameter 'T is not defined.
Thank you!
In F# you can redefine almost everything and shadow existing definitions. This applies to both types (well actually types have a different behavior regarding shadowing, they shadow their values as you open the namespaces) and values but not interchangeably since values and type (and also modules) can somehow coexist at the same time in the scope. The compiler will do his best to find out which one is.
You are not forced to, but it's a common good practice in F# not to use let bindings in uppercase.
Regarding your second question, you are using a type parameter in the right side which doesn't exist in the left side of the assignment, it should be:
type List<'T> = Microsoft.FSharp.Collections.List<'T>
But notice that filter doesn't belong to the type. It's rather defined in the List module.
You should just rename your let binding from List to something sensible - as Gustavo mentioned, your definition is shadowing the core List module from F# and there is no way to use List to refer both to your integer and to the module. Shadowing core functions will make your code pretty confusing. It's also a good idea to use camelCase for let bindings, but that's a matter of taste.
If you insist on shadowing List, then you won't be able to call List.filter using List.filter. If you wanted something shorter, you could define module alias:
module FsList = Microsoft.FSharp.Collections.List
Note that your attempt to do something similar with List<'T> does not do the same thing, because functions such as filter are in a module named List rather than being static members of the type. With this, you can call filter using FsList.filter.
How do I define a generic TList type so that I can declare a variable of that type and then assign any specialization of TList<> to it?
I want to declare this variable:
var
MyList:THowToDeclareThisListType<T>;
And then instantiate it like this:
MyList:=THowToDeclareThisListType<integer>.Create;
or
MyList:=THowToDeclareThisListType<double>.Create;
etc. I must be missing something pretty obvious here. I don't want classes, just a simple type definition.
You are trying to declare a variable like this:
var
List: TList<?>;
such that List can be assigned objects of type TList<Integer> or TList<Double> or TList<string>.
That is not possible. When you define a variable using a generic type, the type must be fully instantiated.
The only way that you can have a variable that holds any object of type TList<T> is if the variable is declared to have a common base class to TList<T>. And the common base class cannot be a non-instantiated generic. For TList<T> the only possible common base class is TObject.
So you could write
var
List: TObject;
and then assign any of your objects to List. But I'm not sure that would be terribly useful!
Lets say I have 2 class types TEmployee (with properties A,B) and TDept (with properties C,D). Then I make a class descended from TList like so :
TMyCcontainer<T>=class(TList<T>)
So I can create instances of TMyCcontainer and fill with TEmployee or TDept. In my TMyCcontainer class is there anyway to access properties A,B of TEmployee, or properties C,D of TDept?
Of course the type is generic so it would appear not. And this is the problem I always have with generics - maybe I am mis-using them. I recently learnt abaout constraints, and thought I had found out what I had been missing....
So I created 2 interfaces say IEmployee and IDept, made my 2 orig class es to be interfaceobjects, and put in my contraint on my Tlist ie
TMyCcontainer<T:IEmployee,IDept>=class(TList<T>)
Of course I was quickly disappointed as this is saying you must implement BOTH of these interfaces in any type I put in my generic TList (TMyContainer), whereas I just want ONE in any particular instance, then ther other in another instance. I would have to implement both IEmployee and IDept in my TDept class which is not what I want obv.
Is there any good way to access members of a Type within a generic container? Or should I not be using generics to do this type of thing. Ty
All generic constraints you put on a class have to be fulfilled by the generic type. Looks like what you're really looking for is two different generic types: TMyContainer<TDept> and TMyContainer<TEmployee>. Then you'll have access to all the properties of those types, individually.