How can I use a std::variant with non-trivial user objects (constructed at a later time), and having the visitor use an auto lambda? - c++17

I have code that is something like this:
using variant_t = std::variant<MyObj1, MyObj2, MyObj3>;
auto foo(){
variant_t var;
if (condition1){
var = MyObj1{"A String"};
// Other stuff
} else if (condition2) {
var = MyObj2{123, 12345};
// Other stuff
} else if (condition3) {
var = MyObj3{SomeObject};
// Other stuff
} else {
throw std::runtime_error{};
}
return var;
}
int main(){
auto var = foo();
std::visit([&](auto& v){v.call_shared_function_name();}, var);
}
Assuming all of the MyObj's are non-trivial, is there a way to get this to work?
I know std::monostate will allow you to initialize the variant in this way and actually populate it later. But if I do so, I can't have the clean auto& lambda in the visitor and will have to create a visitor/lambda for each type.

I know this might looks silly, or not that clean, but you actually need to initialize with one of those objects:
variant_t var = MyObj1{"A String"};
And it compiles fine, if you can't, please let me know and i'll remove the answer

Related

Equivalent of tuples in Dart [duplicate]

Is there a way to return several values in a function return statement (other than returning an object) like we can do in Go (or some other languages)?
For example, in Go we can do:
func vals() (int, int) {
return 3, 7
}
Can this be done in Dart? Something like this:
int, String foo() {
return 42, "foobar";
}
Dart doesn't support multiple return values.
You can return an array,
List foo() {
return [42, "foobar"];
}
or if you want the values be typed use a Tuple class like the package https://pub.dartlang.org/packages/tuple provides.
See also either for a way to return a value or an error.
I'd like to add that one of the main use-cases for multiple return values in Go is error handling which Dart handle's in its own way with Exceptions and failed promises.
Of course this leaves a few other use-cases, so let's see how code looks when using explicit tuples:
import 'package:tuple/tuple.dart';
Tuple2<int, String> demo() {
return new Tuple2(42, "life is good");
}
void main() {
final result = demo();
if (result.item1 > 20) {
print(result.item2);
}
}
Not quite as concise, but it's clean and expressive code. What I like most about it is that it doesn't need to change much once your quick experimental project really takes off and you start adding features and need to add more structure to keep on top of things.
class FormatResult {
bool changed;
String result;
FormatResult(this.changed, this.result);
}
FormatResult powerFormatter(String text) {
bool changed = false;
String result = text;
// secret implementation magic
// ...
return new FormatResult(changed, result);
}
void main() {
String draftCode = "print('Hello World.');";
final reformatted = powerFormatter(draftCode);
if (reformatted.changed) {
// some expensive operation involving servers in the cloud.
}
}
So, yes, it's not much of an improvement over Java, but it works, it is clear, and reasonably efficient for building UIs. And I really like how I can quickly hack things together (sometimes starting on DartPad in a break at work) and then add structure later when I know that the project will live on and grow.
Create a class:
import 'dart:core';
class Tuple<T1, T2> {
final T1 item1;
final T2 item2;
Tuple({
this.item1,
this.item2,
});
factory Tuple.fromJson(Map<String, dynamic> json) {
return Tuple(
item1: json['item1'],
item2: json['item2'],
);
}
}
Call it however you want!
Tuple<double, double>(i1, i2);
or
Tuple<double, double>.fromJson(jsonData);
You can create a class to return multiple values
Ej:
class NewClass {
final int number;
final String text;
NewClass(this.number, this.text);
}
Function that generates the values:
NewClass buildValues() {
return NewClass(42, 'foobar');
}
Print:
void printValues() {
print('${this.buildValues().number} ${this.buildValues().text}');
// 42 foobar
}
The proper way to return multiple values would be to store those values in a class, whether your own custom class or a Tuple. However, defining a separate class for every function is very inconvenient, and using Tuples can be error-prone since the members won't have meaningful names.
Another (admittedly gross and not very Dart-istic) approach is try to mimic the output-parameter approach typically used by C and C++. For example:
class OutputParameter<T> {
T value;
OutputParameter(this.value);
}
void foo(
OutputParameter<int> intOut,
OutputParameter<String>? optionalStringOut,
) {
intOut.value = 42;
optionalStringOut?.value = 'foobar';
}
void main() {
var theInt = OutputParameter(0);
var theString = OutputParameter('');
foo(theInt, theString);
print(theInt.value); // Prints: 42
print(theString.value); // Prints: foobar
}
It certainly can be a bit inconvenient for callers to have to use variable.value everywhere, but in some cases it might be worth the trade-off.
you can use dartz package for Returning multiple data types
https://www.youtube.com/watch?v=8yMXUC4W1cc&t=110s
Dart is finalizing records, a fancier tuple essentially.
Should be in a stable release a month from the time of writing.
I'll try to update, it's already available with experiments flags.
you can use Set<Object> for returning multiple values,
Set<object> foo() {
return {'my string',0}
}
print(foo().first) //prints 'my string'
print(foo().last) //prints 0
In this type of situation in Dart, an easy solution could return a list then accessing the returned list as per your requirement. You can access the specific value by the index or the whole list by a simple for loop.
List func() {
return [false, 30, "Ashraful"];
}
void main() {
final list = func();
// to access specific list item
var item = list[2];
// to check runtime type
print(item.runtimeType);
// to access the whole list
for(int i=0; i<list.length; i++) {
print(list[i]);
}
}

How do functions with multiple parameters of the same name work?

I've been looking through the code for a Flash game (link). However, I'm having trouble understanding how some of these functions work, especially because some of them have function definitions that I would think to fail to get past the compiler.
The following is some code from TodCommon.as that appears to conflict with itself (or at the very least uses bad naming conventions).
private static var gFlashingColor:Color = new Color();
final public static function ClampFloat(ClampInt:Number, ClampInt:Number, ClampInt:Number) : Number
{
if(ClampInt <= ClampInt)
{
return ClampInt;
}
if(ClampInt >= ClampInt)
{
return ClampInt;
}
return ClampInt;
}
final public static function ClampInt(gFlashingColor:int, gFlashingColor:int, gFlashingColor:int) : int
{
if(gFlashingColor <= gFlashingColor)
{
return gFlashingColor;
}
if(gFlashingColor >= gFlashingColor)
{
return gFlashingColor;
}
return gFlashingColor;
}
Also in the code is the weirdest syntax for a for-each loop that I've ever seen (this example also features a package name as a parameter name)
public function CountPlantByType(com.popcap.flash.framework.resources.fonts:int) : int
{
var _loc_3:CPlant = null;
var _loc_2:int = 0;
var _loc_4:int = 0;
var _loc_5:* = this.mPlants;
while(<to complete>)
{
_loc_3 = __nextvalue;
if(_loc_3.mSeedType != com.popcap.flash.framework.resources.fonts)
{
break;
}
_loc_2++;
}
return _loc_2;
}
Those are just a few examples of things that I think look super weird and am having trouble understanding. But these functions all work and are used extensively throughout the code. Can someone explain how the ClampFloat and ClampInt functions work, or why it's legal to use a package name as a parameter? Thanks
Resolved. Turns out the program I used to extract these files from the SWF also corrupted them in the process. Using JPEXS Free Flash Decompiler instead of ActionScriptExtractor fixed the code syntax.

How to define functions within a function in typescript?

I know basic Javascript, but am confronted with a problem in a Typescript file. I'm using Ionic framework to test a page where a user can theoretically "swipe" like they're on Tinder, just for fun.
I have all the JS written, because I'm moving this over from Codepen, but I can't seem to get past Typescript's syntax.
The Javascript:
var tinderContainer = document.querySelector('.tinder');
var allCards = document.querySelectorAll('.tinder--card');
var nope = document.getElementById('nope');
var love = document.getElementById('love');
function initCards(card, index) {
var newCards = document.querySelectorAll('.tinder--card:not(.removed)');
newCards.forEach(function (card, index) {
card.style.zIndex = allCards.length - index;
}
}
The Typescript (that I put together using Google and SOF answers):
export class TestPage {
constructor(public navCtrl: NavController) {
}
tinderContainer = document.querySelector('ion-content');
allCards = document.querySelector('.tinder--card');
nope = document.getElementById('nope');
love = document.getElementById('love');
declare initCards(card,index) {
newCards = document.querySelectorAll('.tinder--card:not(.removed)');
newCards.forEach((card,index)) {
card.style.zIndex = allCards.length - index;
}
}
}
some hints are:
Use let newCards in you function as you have to declare your variable.
Your forEach should be something like this.
newCards.forEach((card, index) => {
...
});
but in order to use syntax like card.style.zIndex and allCards.length you will have to declare variable types..
For unknown models you can use something like card['style']['zIndex']
Also you have to use this to access class properties, like this.allCards

NSClassFromString not working

Here is an issue I have with this case of code:
let entityTypeDico = ["entityTypeOne": arrayOne, "entityTypeOneTwo": arrayTwo,
"entityTypeThree": arrayThree, "entityTypeFour": arrayFour];
for (entityName, arrayItem) in entityTypeDico {
for x in arrayItem {
/*do something with x and arrayItem
I need to use the class of entityName */
//To get it I tried to use some code like:
NSClassFromString(entityName) // But it does not work.
}
}
entityTypeOne, ..... entityTypeFour are names of CoreData entities,
arrayOne, ..... arrayFour are some kind of arrays (not so important for the question).
What exact code do I need to use to get hold of the type of each entity inside the loop?
Your second for loop doesnt make sense. Can you try?
for (entityName, arrayItem) in entityTypeDico {
NSClassFromString("YourProjectName.\(entityName)")
}
Edit 1
Currently I am working on a project where I used NSClassFromString. And that is how I used it in my code.
In my main class I use:
let carClass = NSClassFromString("MyProjectName.\(self.selectedBrand)\(self.selectedModel)") as! NSObject.Type
_ = carClass.init()
where selectedBrand and selectedModel are picked by the user.
In the car class, I have an init function
override init() {
super.init()
// My codes here like initializing webview
}
Hope it helps
Here what I ended up by finding after a number of experiments.
It works and does what I want. I hope it will be useful to some other people too.
let entityTypeDico = ["entityTypeOne": arrayOne, "entityTypeOneTwo": arrayTwo,
"entityTypeThree": arrayThree, "entityTypeFour": arrayFour];
// "entityTypeXYZ" is a class name (subclass of NSManagedObject)
// arrayXYZ is some array (not so important for the question)
for (entityName, arrayItem) in entityTypeDico {
for x in arrayItem {
/*do something with x and arrayItem
I need to use the class of entityName */
//To get it I tried to use some code like:
NSClassFromString(entityName) // But it did not work.
// Here is what finally worked for me:
((NSClassFromString("\(entityName)")) as! NSManagedObject.Type)
}
}

How to call Type Methods within an instance method

Apple has a nice explanation of Type (Class) Methods, however, their example looks like this:
class SomeClass {
class func someTypeMethod() {
// type method implementation goes here
}
}
SomeClass.typeMethod()
I see this exact same example parroted everywhere. My problem is, I need to call my Type Method from within an instance of my class, and that doesn't seem to compute.
I MUST be doing something wrong, but I noticed that Apple does not yet support Class Properties. I'm wondering if I'm wasting my time.
I tried this in a playground:
class ClassA
{
class func staticMethod() -> String { return "STATIC" }
func dynamicMethod() -> String { return "DYNAMIC" }
func callBoth() -> ( dynamicRet:String, staticRet:String )
{
var dynamicRet:String = self.dynamicMethod()
var staticRet:String = ""
// staticRet = self.class.staticMethod() // Nope
// staticRet = class.staticMethod() // No way, Jose
// staticRet = ClassA.staticMethod(self) // Uh-uh
// staticRet = ClassA.staticMethod(ClassA()) // Nah
// staticRet = self.staticMethod() // You is lame
// staticRet = .staticMethod() // You're kidding, right?
// staticRet = this.staticMethod() // What, are you making this crap up?
// staticRet = staticMethod() // FAIL
return ( dynamicRet:dynamicRet, staticRet:staticRet )
}
}
let instance:ClassA = ClassA()
let test:( dynamicRet:String, staticRet:String ) = instance.callBoth()
Does anyone have a clue for me?
var staticRet:String = ClassA.staticMethod()
This works. It doesn't take any parameters so you don't need to pass in any. You can also get ClassA dynamically like this:
Swift 2
var staticRet:String = self.dynamicType.staticMethod()
Swift 3
var staticRet:String = type(of:self).staticMethod()
In Swift 3 you can use:
let me = type(of: self)
me.staticMethod()
I wanted to add one more twist to this old question.
In today's Swift (I think it switched at 4, but I am not sure), we have replaced type(of: self) with Self (Note the capital "S").

Resources