Check if input to macro is a vector
up vote
0
down vote
favorite
I would like to check if the input to a macro is an vector and raise an exception if it is not. Currently, I have come up with this. Is there an idiomatic way of doing this?
macro doarray(arr)
if in(:head, fieldnames(typeof(arr))) && arr.head == :vect
println("Do Something")
else
throw(ArgumentError("$(arr) should be a vector"))
end
end
julia-lang
add a comment |
up vote
0
down vote
favorite
I would like to check if the input to a macro is an vector and raise an exception if it is not. Currently, I have come up with this. Is there an idiomatic way of doing this?
macro doarray(arr)
if in(:head, fieldnames(typeof(arr))) && arr.head == :vect
println("Do Something")
else
throw(ArgumentError("$(arr) should be a vector"))
end
end
julia-lang
2
If you want to allow only[A, B, C, ...]
like syntax then you have to look for:vect
in head as you write as far as I know. However, you can construct a vector also usingvcat
like this[1;2;3]
and this pattern will not cover this (butvcat
also can produce something else than a vector so the case is messy here).
– Bogumił Kamiński
Nov 22 at 17:52
2
Also take a look at Macrotools.jl. You can match against[a__]
.
– 张实唯
Nov 23 at 2:08
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I would like to check if the input to a macro is an vector and raise an exception if it is not. Currently, I have come up with this. Is there an idiomatic way of doing this?
macro doarray(arr)
if in(:head, fieldnames(typeof(arr))) && arr.head == :vect
println("Do Something")
else
throw(ArgumentError("$(arr) should be a vector"))
end
end
julia-lang
I would like to check if the input to a macro is an vector and raise an exception if it is not. Currently, I have come up with this. Is there an idiomatic way of doing this?
macro doarray(arr)
if in(:head, fieldnames(typeof(arr))) && arr.head == :vect
println("Do Something")
else
throw(ArgumentError("$(arr) should be a vector"))
end
end
julia-lang
julia-lang
asked Nov 22 at 17:33
RAbraham
2,49722542
2,49722542
2
If you want to allow only[A, B, C, ...]
like syntax then you have to look for:vect
in head as you write as far as I know. However, you can construct a vector also usingvcat
like this[1;2;3]
and this pattern will not cover this (butvcat
also can produce something else than a vector so the case is messy here).
– Bogumił Kamiński
Nov 22 at 17:52
2
Also take a look at Macrotools.jl. You can match against[a__]
.
– 张实唯
Nov 23 at 2:08
add a comment |
2
If you want to allow only[A, B, C, ...]
like syntax then you have to look for:vect
in head as you write as far as I know. However, you can construct a vector also usingvcat
like this[1;2;3]
and this pattern will not cover this (butvcat
also can produce something else than a vector so the case is messy here).
– Bogumił Kamiński
Nov 22 at 17:52
2
Also take a look at Macrotools.jl. You can match against[a__]
.
– 张实唯
Nov 23 at 2:08
2
2
If you want to allow only
[A, B, C, ...]
like syntax then you have to look for :vect
in head as you write as far as I know. However, you can construct a vector also using vcat
like this [1;2;3]
and this pattern will not cover this (but vcat
also can produce something else than a vector so the case is messy here).– Bogumił Kamiński
Nov 22 at 17:52
If you want to allow only
[A, B, C, ...]
like syntax then you have to look for :vect
in head as you write as far as I know. However, you can construct a vector also using vcat
like this [1;2;3]
and this pattern will not cover this (but vcat
also can produce something else than a vector so the case is messy here).– Bogumił Kamiński
Nov 22 at 17:52
2
2
Also take a look at Macrotools.jl. You can match against
[a__]
.– 张实唯
Nov 23 at 2:08
Also take a look at Macrotools.jl. You can match against
[a__]
.– 张实唯
Nov 23 at 2:08
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435917%2fcheck-if-input-to-macro-is-a-vector%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435917%2fcheck-if-input-to-macro-is-a-vector%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
If you want to allow only
[A, B, C, ...]
like syntax then you have to look for:vect
in head as you write as far as I know. However, you can construct a vector also usingvcat
like this[1;2;3]
and this pattern will not cover this (butvcat
also can produce something else than a vector so the case is messy here).– Bogumił Kamiński
Nov 22 at 17:52
2
Also take a look at Macrotools.jl. You can match against
[a__]
.– 张实唯
Nov 23 at 2:08