Make the compiler tell me that two variables can’t have the same value - swift

I have a list of objects that need to be loaded into my app. But the load order matters. I’ve created a var which is a part of each object that indicates the load order where the lowest number gets loaded in first. Then I’ve set the default to be 9999. I can manually set this value on each object and override the default. Currently this is where I’m at.
Should I add more objects which need to be loaded in a specific order in the future, I want to make sure there are no conflicts. It seems to me like I could do that by making each load order number on the objects unique, (i.e. no duplicate numbers in all the load order variables).
Is there a way I can make Xcode throw an error or warning if it detects that anything conforming to a protocol has the same value on a variable from that protocol as any of the other conforming objects?

Well, you could list all the objects in an array literal in the order you want them loaded, and forgo the load order property entirely:
let objectsToLoad = [
ObjectDescriptor("hello"),
ObjectDescriptor("world"),
]
But if you have objects defined in disparate places and don't want a single unified array literal that lists them all, then I don't think you can get help from the compiler (or the linker).
In a few months, when Swift will have macros, you'll probably be able to get help from the compiler or the linker. The strategy is to use a macro to wrap each load-order constant in a macro that also generates some type definition whose name includes the constant, e.g. enum _LoadOrder_1 {}, enum _LoadOrder_357 {}, etc. Then, if you have a duplicate, either the compiler or the linker will fail due to the multiple definitions for a single identifier.

Related

Passing An Object Instance By Value In Dart

I have an object that was created with GetxController and it has a list field. I want to select a specific item in that list and change some parameters. But I should be able to cancel this changing scenario. Because when I select an item and change it, also change that class list item.
In Dart, objects pass by reference. But I need to pass it by value for canceling change. There's any way to pass an object instance by value?
In this kind of scenario, you should try to use immutable types.
There are good Dart libraries for that, even if the language doesn't have native support for it... with truly immutable data types, you essentially get pass-by-value semantics. They also make it easy to make copies of your data with small modifications on the returned value (while the original obviously remains unmodified).
Example packages you could use:
built_collection
built_value
freezed
No.
But you can just make a copy yourself and pass that. And depending on whether the change was confirmed you can eiter copy it back into your list or not.

Is it possible to get the type of a variable while computing completion suggestions?

I'm thinking of creating a VSCode extension to help people use a library I am creating. I've looked in 50 places and can only see documentation on getting the plain text of the document in which the completion suggestions are triggered.
My library exposes a function with two parameters, both objects with the same keys, but the first one will be defined already and the 2nd one will be passed in as an object literal. For example
const obj1 = {a:1, b:2};
doLibraryThing(obj1, {a:[], b:[]});
I want to provide code completion, or a snippet, or anything that can detect that doLibraryThing is being called, and know what properties were defined in obj1, even though usually it will be imported from another file; and then use those properties to generate the suggestion with the same properties, {a:[], b:[]}.
Is this possible?

When does Inspector in unity assign variable?

Say i have a public list of integers then of course I will see the list in unity inspector and i can assign multiple values inside it. My question is when the list will actually assign the values to the variables in the game?? Does it assign values in "OnEnable()", "OnAwake()", "OnStart()".
During deserialization
Which occurs before any method of that script's code is called.
If you want to run code at that point in time, you need an ISerializationCallbackReceiver. Note that the intended use of this interface is to serialize/deserialize certain complex Types (such as dictionaries) for use in the Inspector; I have not attempted to use this in a runtime capacity even though the interface does appear to be in UnityEngine not UnityEditor.

Using Conditional Syntax (Overrides) in BitBake

Reading a book on Yocto. Got to the following page, which says:
BitBake provides a very easy-to-use way to write conditional metadata.
It is done by a mechanism called overrides.
The OVERRIDES variable contains values separated by colons (:), and
each value is an item we want to satisfy conditions. So, if we have a
variable that is conditional on arm, and arm is in OVERRIDES, then the
version of the variable that is specific to arm is used rather than
the non-conditional version, as shown:
OVERRIDES = "architecture:os:machine"
TEST = "defaultvalue"
TEST_os = "osspecificvalue"
TEST_other = "othercondvalue"
In this example, TEST will be osspecificvalue due to the condition
of os being in OVERRIDES.
I'm unclear from this explanation how did TEST become equal to osspecificvalue. Would someone be able to explain it?
Bitbake implements it's own dictionary data structure based on Python's MutableMapping in lib/bb/data_smart.py. The goal is to create a dictionary with more flexibility in that each value in the "key,value" pair can be overridden based on specific identifiers.
If you look at how the variables in this dictionary are set, you will see that the datastore allows "overrides" of variables based on a list of override identifiers. These identifiers are expected to be appended with an underscore, like in your example of "TEST_os".
In the case you are referencing, "other" identifier is not in the list of OVERRIDES, so this "smart dictionary" does not override the value of TEST with "othercondvalue". However, because the "os" identifier is in the list of OVERRIDES, the value of TEST is indeed overridden with the value "osspecificvalue".
I would highly recommend reading through the DataSmart class as this is a very simplified explanation, but hopefully it helps.
Also, see the BitBake manual entry for OVERRIDES for more information.

Copying EGenericType instances with ECoreUtil.Copier

I am working with EMF models and need to make copies of these. My models consist of interlinked instances of three meta-models, one of which is Ecore itself. So far, so good.
However, when it comes to copying these models (which I do using EcoreUtil.Copier.copyAll following the usual protocol), some parts of my model are not copied. Specifically, the Ecore instance contains a number of instances of EGenericType (because there are references and attributes and these are automatically set up with EGenericType instances to show their types). The result of copying contains everything, but these instances of EGenericType.
I have searched high and low and looked into the EMF source code, too, but couldn't figure out what the problem is. I have looked at the source of EcoreUtil.Copier and it checks for each structural feature whether it is changeable and not derived to decide whether to actually copy it. This condition is true for the reference to EGenericType, so this should be copied as a containment reference.
Interestingly, the result of the copy does contain copied instances of EGenericType in all the right places in the object graph. However, these are not mapped in the copier, so don't seem to have been created by a call to EcoreUtil.Copier.copy along the way, but implicitly.
Any ideas when these are created and how I might get them to show up in the copier map?
Many thanks,
Steffen
OK, so I have dug deeper into this with the debugger and think I now understand what's going on:
Essentially, ETypedElement (which is what contains types and generic types) is a bit loose with its contract: neither eType nor eGenericType are marked as derived, but depending on the situation, they one of them will be derived from the other.
Specifically, if you set the eType of an ETypedElement, this implicitly creates a new eGenericType. Similarly, if you set the eGenericType, this will implicitly set the eType to be the erasure of this generic type.
Unfortunately, this behaviour confuses EcoreUtil.Copier when an ETypedElement has had its eType set explicitly. In this situation, the following happens:
EcoreUtil.Copier.copy creates a new instance of the ETypedElement and then starts copying all of its features.
When it gets to the eType feature, it doesn't copy it at this point, because eType is not a containment reference (for obvious reasons).
Next it comes to eGenericType, which is a containment reference. However, the first thing it does is to check whether this is set in the original ETypedElement. For eGenericType and eType, this check has been customised to make sure only one of the two actually returns true. As a result, for our ETypedElement, isSetEType() returns true and isSetEGenericType() returns true. Therefore, copyContainment() decides there is nothing to copy and moves on.
By the time copy() or copyAll() returns, neither the eType nor the eGenericType have been set for the newly created object. We now call copyReferences().
This will eventually attempt to copy the eType reference (remember that this is marked as not a containment reference). isSetType() returns true, so copyReference() goes on and copies the type information across. The setter for eType in the copied object then creates the new instance of EGenericType, but EcoreUtil.Copier never gets to see it.
Thus, if I want to get the original EGenericType instance and its copy to show up in the copy map, I will need to sub-class EcoreUtil.Copier and override copyReference() or copyContainment() to handle this special case.