How can I define a custom ant datatype that checks for required attributes? - ant

Ideally, I would like to throw an exception or log an error at the point where the data type is instantiated, rather than at some later time when the data type instance is referenced.
Is there a method that gets invoked at the point where the data type instance has been fully configured with attributes (and child elements)?

Related

The argument type 'ListOf5' can't be assigned to the parameter type 'ListOf5'

I need to implement an abstract class function, which own a an specific data type. But I need inside my logic layer to make the attribute which is going to be passed as a dynamic data type. But when i Pass it to the function, i am sure that its data type will be as needed. So, i type (product.value.pickedImages) as ListOf5) . But it does an Exception.
The Abstract Class Code Is:
Future<Either<FireStoreServerFailures, List<String>>> uploadProductImages(
{required ListOf5<File> images});
The Implementation Code Is:
Future<Option<List<String>>> _uploadImagesToFirestorage() async {
return await productRepo
.uploadProductImages(
images: (product.value.pickedImages) as ListOf5<File>) // Exception
}
The Exception Is:
The argument type 'ListOf5 < dynamic>' can't be assigned to the
parameter type 'ListOf5 < File>'.
You are trying to cast the List from List<dynamic> to List<String>.
Instead, you should cast each item, using something like this:
void main() {
List<dynamic> a = ['qwerty'];
print(List<String>.from(a));
}
Not sure about the implementation of this ListOf5 though...
The cast (product.value.pickedImages) as ListOf5<File> fails.
It fails because product.value.pickedImages is-not-a ListOf5<File>, but instead of ListOf5<dynamic> (which may or may not currently contain only File objects, but that's not what's being checked).
Unlike a language like Java, Dart retains the type arguments at run-time(it doesn't do "erasure"), so a ListOf5<dynamic> which contains only File objects is really different from a ListOf5<File> at run-time.
You need to convert the ListOf5<dynamic> to a ListOf5<File>.
How to do that depends on the type ListOf5, which I don't know.
For a normal List, the two most common options are:
(product.value.pickedImages).cast<File>(). Wraps the existing list and checks on each read that you really do read a File. It throws if you ever read a non-File from the original list. Perfectly fine if you'll only read the list once.
List<File>.of(product.value.pickedImages). Creates a new List<File> containing the values of product.value.pickedImages, and throws if any of the values are not File objects. Requires more memory (because it copies the list), but fails early in case there is a problem, and for small lists, the overhead is unlikely to be significant. If you read the resulting list many times, it'll probably be more efficient overall.
If the ListOf5 class provides similar options, you can use those. If not, you might have to build a new ListOf5 manually, casting each element of the existing ListOf5<dynamic> yourself.
(If the ListOf5 class is your own, you can choose to add such functionality to the class).

trying to pass by reference in F#

I have a type that receives data through websocket / event and it instantiates a couple other types that also need that data for their work.
I thought it would simplify the code to have only one event listener, in the parent type and pass the result by reference so the children types could access the last data.
I've never used byref in F# and .. looks like I don't get it right
From the parent:
let mutable lastTrade = TradeData.empty // this gets updated through an event
let closeOrderHandler = CloseOrderHandler(coreGuid, instrument, &lastTrade)
and the child object:
type CloseOrderHandler(coreGuid: string, instrument: Instrument, lastTrade: byref<TradeData>) = ...
but this will not compile, I get:
[FS0412] A type instantiation involves a byref type. This is not permitted by the rules of Common IL.
what am I doing wrong?
What I am trying to accomplish is to have one mutable field with results updated by a socket / event and the child types be able to read the last result as they need (their actions are event driven, so they don't always get called from the parent and need to access the latest value as they need).

When is a type registered by the type system in glib?

I'm currently working on a library, and ran into some strange inconsistencies in behaviour between my unit test code, and an actual app I'm basing on the code.
See, I'm trying to get a Type struct from the name of the class I want to instantiate, but I get nothing (e.g. Type is there, but name() produces null, is_a() fails, etc.)
However, if I make an instance of the type first, then try to get the Type based on name once more, it works just as I expected (which is probably why my unit tests work)
So I was wondering, when is a type registered by the type system and available via. from_name(...) ?
Does the type system only know it after it has been instantiated at run time? Is there another reason a class name wouldn't be recognized until an instance of the class has been instantiated? Should I use some other method of registration?
I'm coding in Vala if that makes any kind of a difference.
A type is first registered with the type system when its get_type() function is called. This is called in a number of places, basically whenever you need to get the GType for that type. Typically, it will first be called during class_init of the type, which happens during the first instance init.
So essentially you are right when you say “does the type system only know it after it has been instantiated at run time”, because that’s normally what happens. However, you can bring type registration forward by explicitly calling the get_type() function for a type early, and passing it to g_type_ensure(). For example, see what GLib does here:
/* Initialize types from built-in "modules" */
g_type_ensure (g_null_settings_backend_get_type ());
g_type_ensure (g_memory_settings_backend_get_type ());
g_type_ensure (g_keyfile_settings_backend_get_type ());
…

Guarantee initialisation of stack record

I want to fix a design flaw in a record TMyValue that has been in use for years, and I desperately want to fix it in the record itself - to avoid changing the public interface - and not require the code using the record to be changed.
Essentially, the flaw is that a TMyValue must be initialised to zero when it is created, otherwise calling the Clear method on it can cause a crash. This is because it contains a field that is a pointer to dynamically-allocated memory if the pointer is non-nil, and Clear causes the dynamic memory to be freed.
This is a problem if the TMyValue is created on the stack, because stack variables are not zeroed out automatically when they are created.
I thought I could use a record constructor to zero out the record, but record constructors cannot be parameterless. Presumably this means that you cannot force a record constructor to be executed automatically when a record is created on the stack.
I suspect the answer to my question is "it cannot be done". Please prove me wrong!
Add a dummy string member into your record.
Since a string is a managed type it will be initialized to an empty string when the record comes in scope.
So when calling your Clear method, test if the dummy string is empty first.
Set the string to a value when appropriate to the use logic to mark the record as initialized.

Get Model Type from RazorView object

I have a System.Web.Mvc.RazorView object which is strongly typed when in cshtml.
Can I get the model type from an instance of this class?
This is possible.
Call BuildManager.GetCompiledType(view.ViewPath) to get the type generated by compiling the view.
You can find the model type by checking the generic argument of the compiled type's base type (which should be WebViewPage<TModel>)
There's no way to get the model given only an instance of a System.Web.Mvc.RazorView. It's available inside the RenderView method which is passed a ViewContext but from the outside you can't access it. But if you are inside a view you could use the Model property.

Resources