Confused about Trait Bounding in functions Rust - function

first post on here so apologies if I don't give enough information to get a clear picture.
I'm attempting to write a basic VST in Rust, the example I'm following has used a deprecated gtk method to actually build out the app and so I'm having to rewrite it. The thing I'm confused about is trait bounding (I think it's trait bounding) in functions.
How the function is written in the example:
fn run_main<Module: AudioModule>() {
//Stuff going on in here using Module::
}
What is going on here? Why am I doing this? Is there another way to achieve the same thing here?
I need to rewrite this function and pass an &app argument into it but I'm not allowed to do that because the AudioModule and gtk_app aren't related in anyway.
I tried to do something like this:
fn build_ui(app: &Application) {
//Doing all my UI stuff
fn defining_module<Module: AudioModule>() {
//Doing all my Module:: stuff
}
}
But honestly I haven't got a clue what I'm doing. It's throwing a load of errors at me. And I'm at a loss. I'd really appreciate any help.

Related

Can anyone explain .split(''); in Razor?

I sitting here with a ton of code (that works) which I have to look through, and some of it happens to be in Razor. Not too hard to understand, yet not something I've tried before.
My problem is that the function .split(''); (notice the '') comes up several times and I don't understand neither the purpose nor what it does, cause I feel like it would just give an error to split on nothing.
I can't really test anything as the code is way too large for me to be able to just run to check such a minor thing, and there are no real places to test Razor unless I get enough understanding to just make a testpage from scratch.
The most simple example I can find is this:
function openbysetting(settingcookie)
{
var settingsarray = settingcookie.split('');
for (var i = 0; i < settingsarray.length; i++)
{
if (settingsarray[i] == 'f')
{
....
}
};
}
Mind you, settingcookie isn't defined or mentioned anywhere else in this entire code, so I don't even understand why a regular split function would be required.
Edit: Never mind, I'm told it's a javascript thing. Just ignore the previous sentence.
As for a result, I'd expect it to just simply not work, but it seems to be doing fine.
C# just gives an error if you do it, and this is a C# based language after all.
Hope someone can clarify. Thank you.
Edit: Okay, I understand now. It just splits between every single character. Thanks for the help.

Has there been any known improvements of Class <--> Json Serialization?

Dart has a known error where a class you create with public variables can not be passed into native JSON template calls. I was not sure if there has been any improved headway on this.
Example;
class Test{
String a;
int b;
}
would be nice to translate into:
{"a":"","b":0}
It seems when looking around the internet and forums for dart, this was an issue, and with a language that works so closely with Javascript, a terribly large oversight. I havent seen anything yet, though it seems you might be able to scrape a the public variables if you are using mirrors, which is seemingly time intensive.
Here is what a sample is that I am working on.
class UserGroup{
String a;
int b;
UserGroup.fromMap(Map m){ ... }
}
I receive from a Service call, a map which is cast to a UserGroup.
This will then produce a List which are binded and passed into a sub WebComponent which will access a Map. Something like this.
<service-call result="{{rowData}}"></service-call>
<grid-creator rows="{{rowData}}"></grid-creator>
and inside of grid creator:
<dom-module id="grid-creator>
<template>
<div>
<template is="dom-repeat" for="{{rows}}" as="row">
<div id="{{row.id}}">{{row.name}}</div>
</template>
</div>
</template>
</dom-module>
The issue had has is that the grid-creator is dynamic, accepting a row of maps and parsing accordingly. Even if I were to say List<UserGroup> rows it doesnt understand it as row and will throw an error like:
type 'UserGroup' is not a subtype of type 'Map' of 'row'
That means that Dart classes nativly cannot interact with template repeaters at all, unless the class itself is a subtype of map. which wouldnt make sense to me.
That being said, a lot of the notes are from September 2015 and prior. I was curious where this has progressed.
You could say: Well, why not just have a toJson method which turns a map, then parse and intercept the class and convert it before it it hits the template.
I mean, i guess that could be a thing if i do an observer on the rowData and then have a second variable which is populated and subsequently refresh the grid-creator but that that isnt really the point. It sort of removes the Object from being passed around and at that point, I should just remove the class component and just use maps. If im just using maps, then why the heck would i use dart? /endrant
Anyways, has there been improvements in this area?
One of the threads i was reading about serialization: https://github.com/dart-lang/sdk/issues/16630
Edit: I was trying to look into the concept of having the class UserGroup extend JsProxy, but i dont think that would resolve the issue i was having

Flash AS Event Properties: Possible to trace them with a for loop?

I was wondering if it was possible to use a for (or for each) loop to trace the properties of an event to the output window. I know I can trace the event in one go, like this:
function myFunct (evt:IOErrorEvent):void
{
trace(evt);
}
Unfortunately this gets a little crazy to read in some situations, such as a long URL path, so I would like to reformat it a bit to show each property on its own line, something like this:
function URLLoader_IOError (evt:IOErrorEvent):void
{
for each(var prop in evt)
{
trace(prop)
}
}
Of course, this example isn't showing anything in the output window. Am I missing something in the function or is this just not doable?
Thanks!
I suggest you find the named properties you want to trace and trace those specifically. Properties that would be useful are errorId, text and type. Possibly eventPhase as well.
As your code stands, you will be trying to convert objects to string representations. What is trace supposed to do with the currentTarget property, for example? And do you really care about the bubbles property of IOErrorEvent? Or 'constructor'?
Alternatively, you can do a lot of testing in your loop to determine what kind of data type you're dealing with, and convert some of its properties to strings for tracing, but at the end of the day you'll still have to use the debugger to examine objects in depth.

AS3 override addChild(), test for type

this works for me, but does it look correct in terms of as3 best practices. is it correct to return null if it fails?
override public function addChild(child:DisplayObject):DisplayObject {
if(child is Screen) {
super.addChild(child);
return child;
}
return null;
}
I don't see why not. I've never actually used a return value of addChild in practice. You'd need some way of knowing if the child had been added so you could then use:
if (!addChild(child))
{
//couldn't be added
}
instead of
if (child is Screen)
{
addChild(child);
}
else
{
// couldn't be added
}
Or you could instead throw an error in your override and catch that if it's not an instance of Screen.
I don't think it's a best practice issue at all if it works for you.
I see a couple of options and none feels completely "right".
The best solution, as I see it, is not possible in Actionscript (I think this is allowed in other languages). That is, declare that you will accept only instances of Screen:
override public function addChild(child:Screen):Screen {
return super.addChild(child);
}
Since that's not possible, the other options I can think of would be:
1) Throwing an Error.
I'm generally not very fond of throwing Errors, but a pro here is that it matches the behaviour of the extended class. If you pass a non valid display object to addChild, it will throw. On the other hand, this is valid given that the method signature tells you that you have to pass a DisplayObject. In this case, your method is "lying", in a way, because it says it accepts DisplayObjects but it really only accepts instances of Screen. So you are relying in documentation (hopefully) rather on the type system to tell the user code how it's supposed to use your function.
2) Adding an addScreen method that calls addChild.
public function addScreen(child:Screen):Screen {
return super.addChild(child) as Screen;
}
This is somewhat more typesafe, but you lose some of the advatanges of polymorphism (and perhaps it's not possible / feasible in your code). Maybe if we had method overloading... but we don't so, again, I guess it's a tradeoff.
3) Runtime type checking and returning null on failure.
This is what your code does. It could be just fine. What I don't like about it is what I mentioned above: that your method is kind of "lying". I don't expect a method that takes a DisplayObject to fail because I passed a DisplayObject. But well, sometimes, this is not a big deal.
I'm afraid I can't really give a definite answer on this. If possible, I think I'd probably go with option 2), but all these options seem equally valid. At some point you have to settle for the one that makes more sense for you and your project, I guess.
There is no good way to do, what you want to. If you want the desired behaviour, you are to use composition instead of inheritance. What you're trying to do is violating the Liskov substitution principle.

Eval formula in AS3?

I'm playing around a bit with ActionScript. What I want is that I can display a mathematical function from a string.
E.g. in my working python script I do something like that:
formula = 'x**2 + 3*x'
for x in range( 0, 100 ):
y = eval( formula )
graph.display( x, y )
I want to port this to ActionScript, but it seems like there is no more eval since version 3. How can I compute my function values anyway?
Something that might also work in your case, is using Javascript eval instead. You can use something like:
var result = ExternalInterface.call(myEvalFunctionInJS,formula)
to evaluate math functions.
This is an somewhat easy and useful workaround as javascript is quite close to actionscript.
If you put the ExternalInterface call inside an loop, it may become sluggish. To avoid that, you can write the loop in javascript. (You can even write the entire javascript inside as3, so that you do not need to touch the actual html page.)
edit:
Here's an link for that.
http://www.actionscript.org/resources/articles/745/2/JavaScript-and-VBScript-Injection-in-ActionScript-3/Page2.html
You will have to write an eval yourself. You will have to parse the string and invoke the right operators.
Here's a link to get you started.
The Tamarin project has a ECMAScript parser written in ES4. Try this as well.
"You can even write the entire javascript inside as3, so that you do not need to touch the actual html page." Do you have links / tutorials? – okoman
Both AS and JS are based on the same ECMAScript standard. So, if you pass a string of AS3 to a container, and use JS's eval on this string, it should work just fine.
Just noticed this question and realized I answered almost the exact same thing here: https://stackoverflow.com/a/11460839/1449525
To paraphrase myself, you can definitely use D.eval, AS3Eval, or ExternalInterface (as seen in the currently chosen answer) assuming you're running in a web page. However, all it seems like you really need is something like this simple MathParser (More info about the MathParser)
Here's how you'd use the MathParser:
package {
import bkde.as3.parsers.*;
import flash.display.Sprite;
public class MathTest extends Sprite {
public function MathTest() {
var parser:MathParser = new MathParser([]);
var compiledObj:CompiledObject = parser.doCompile("(10/3)*4+10");
var answer:Number = parser.doEval(compiledObj.PolishArray, []);
var xyParser:MathParser = new MathParser(["x", "y"]);
var xyCompiledObj:CompiledObject = xyParser.doCompile("(x/3)*y+10");
var xyAnswer:Number = xyParser.doEval(xyCompiledObj.PolishArray, [10, 4]);
}
}
}
I'm sure ExternalInterface stuff works just fine, but I have personal reservations about the cross language communication (especially in terms of efficiency and security) as well as just the awkward nature of it. I feel like a wholly-contained, same-language solution is typically preferable in most situations.
A bit late, but for reference, the D.eval library does what you are asking for:
http://www.riaone.com/products/deval/
It is free and works great for me, but doesn't come with source. I found this question looking for an alternative built-in or source-available solution.
There is also a seemingly abandoned project to port Tamarin to Flash itself:
http://eval.hurlant.com/
Would be awesome if more progress was made, but seems like a curiosity for now.