I have a code, and I want to make sure that a class definion is indeed loaded.
Lets say that the class name is foo. I can do the following:
if { [ catch { foo new } ] } { source "path_to_code" }
Is there a way to do it without catch?
You can use the info command:
if {![info object isa class foo]} {source "path_to_code"}
Related
I am trying to know which class has called a specific function. I've been looking through the docs for this, but without success. I already know how to get the name of a class, but that is something different of what I'm looking for. I found already something related for java but for dart I haven't. Maybe I'm missing something.
Let's say for example that I have a print function like so:
class A {
void printSomethingAndTellWhereYouDidIt() {
// Here I would also include the class where this function is
// being called. For instance:
print('you called the function at: ...');
//This dot-dot-dot is where maybe should go what I'm looking for.
}
}
class B {
A a = A();
void test() {
a.printSomethingAndTellWhereYouDidIt();
}
}
The output should be something like:
you called the function at: B
Please let me know if there are ways to achieve this. The idea behind is to then use this with a logger, for instance the logging package. Thank you in advance.
You can use StackTrace.current to obtain a stack trace at any time, which is the object that's printed when an exception occurs. This contains the line numbers of the chain of invocations leading up to the call, which should provide the information you need.
class A {
void printSomethingAndTellWhereYouDidIt() {
print(StackTrace.current);
}
}
class B {
A a = A();
void test() {
a.printSomethingAndTellWhereYouDidIt();
}
}
If you are doing this for debugging purposes, you can also set a breakpoint in printSomethingAndTellWhereYouDidIt to check where it was called from.
Let's say I'm writing a swift module, and I want to name a type with a name which already exists. If the name is from another module, it's easy. I can just use the module name as a namespace:
import Foundation
class MyClass {
class Notification : Foundation.Notification { ... }
}
My question is, is there any way to do the same with types in the same module? For example, I would like to be able to do something like this:
class Notification { ... }
class MyClass {
class Notification : Module.Notification { ... }
}
Where Module.Notification is a reference to the type declared above. Is such a thing possible?
You need to use the actual name of your module:
class Notification { ... }
class MyClass {
class Notification : MyAmazingTwitterApp.Notification { ... }
}
If you're working in Xcode, this defaults to your target name. There's a build setting "Product Module Name", under "Packaging" that lets you change this.
If you're using the Swift build system, this is of course specified in your manifest file, via the PackageDescription.
Is it possible to build an enum inside a Rust macro using fields that are defined as macro parameters? I've tried this:
macro_rules! build {
($($case:ty),*) => { enum Test { $($case),* } };
}
fn main() {
build!{ Foo(i32), Bar(i32, i32) };
}
But it fails with error: expected ident, found 'Foo(i32)'
Note that if the fields are defined inside the enum, there is no problem:
macro_rules! build {
($($case:ty),*) => { enum Test { Foo(i32), Bar(i32, i32) } };
}
fn main() {
build!{ Foo(i32), Bar(i32, i32) };
}
It also works if my macro only accepts simple fields:
macro_rules! build {
($($case:ident),*) => { enum Test { $($case),* } };
}
fn main() {
build!{ Foo, Bar };
}
But I've been unable to get it to work in the general case.
It's absolutely possible, but you're conflating totally unrelated concepts.
Something like $case:ty does not mean $case is something which looks like a type, it means $case is literally a type. Enums are not made up of a sequence of types; they're made up of a sequence of variants which are an identifier followed (optionally) by a tuple structure body, a record structure body, or a tag value.
The parser doesn't care if the type you give it happens to coincidentally look like a valid variant, it's simply not expecting a type, and will refuse to parse one in that position.
What you need is to use something like $case:variant. Unfortunately for you, no such matcher exists. The only way to do something like this is to manually parse it using a recursive incremental parser and that is so out of scope of an SO question it's not funny. If you want to learn more, try the chapter on incremental TT munchers in the Little Book of Rust Macros as a starting point.
However, you don't appear to actually do anything with the cases. You're just blindly substituting them. In that case, you can just cheat and not bother with trying to match anything coherent:
macro_rules! build {
($($body:tt)*) => {
as_item! {
enum Test { $($body)* }
}
};
}
macro_rules! as_item {
($i:item) => { $i };
}
fn main() {
build!{ Foo, Bar };
}
(Incidentally, that as_item! thing is explained in the section on AST coercion (a.k.a. "the reparse trick").)
This just grabs everything provided as input to build!, and shoves it into the body of an enum without caring what it looks like.
If you were trying to do something meaningful with the variants, well, you're going to have to be more specific about what you're actually trying to accomplish, as the best advice of how to proceed varies wildly depending on the answer.
I have 2 modules with class name has A-Class and B-Class. I want to make sure B-Class runs first and with A-Class when i run Puppet apply site.pp. But getting error as 'Syntax error at 'mongos'; expected '}' at require B-Class line, below is the code.
node 'HOST-1'{
class { 'a_class':
require b_class,
first => "abcd",
log_data_path => "/log/serv.1",
}
class { 'b_Class':
build_id => "php_2.4",
}
}
Is it the right way to use require? Else what is the better way to do.
TIA
This is also right. But better way to do it is
class { 'a_class':
first => "abcd",
log_data_path => "/log/serv.1",
require => Class['a_class']
}
Almost right, but require is a parameter in its own right.
require => Class['a_class']
In this scenario, you could also use chaining arrows.
class { 'b_class': ... }
->
class { 'a_class': ... }
This will have the same effect.
In both Python and Java we have import to eliminate the repetition of fully-qualified package/module names throughout code. Is there any equivalent in Perl/Moose? I think it would really make Moose nicer to use if we didn't have to repeat MyApp::Model::Item. Instead, I'd like to [somehow declare] MyApp::Model::Item; and later on, simply refer to Item. I can think of all of these use-cases where class names are used...
extends 'Item';
with 'ItemRole';
Item->new(name => 'thing');
method foo(Item $xyz) { ... }, with MooseX::Method::Signatures
$var->isa('Item');
try { ... } catch (DatabaseError $e) { ... }, with TryCatch
$Item::SOME_PACKAGE_GLOBAL_VARIABLE
If there is no such thing yet, any idea on how I might start to cleanly implement this? I can see that it would be tricky to deal with cases where the classname is used as a string.
This all works with aliased
use aliased 'MyApp::Model::Item';
use aliased 'MyApp::ItemRole';
use aliased 'MyApp::Exception::DatabaseError';
extends Item;
with ItemRole;
Item->new(name => 'thing');
method foo (Item $xyz) { ... }
$var->isa(Item);
try { ... } catch(DatabaseError $e) { ... }
This doesn't:
$Item::SOME_PACKAGE_GLOBAL_VAR
Needing something like that seems to be quite rare, but I suppose it could be made to work with the namespace::alias module.