|
[Date Index]
[Thread Index]
[Author Index]
UpValues for expressions headed by a string
- To: mathgroup at smc.vnet.net
- Subject: [mg62896] UpValues for expressions headed by a string
- From: "Trevor Baca" <trevorbaca at Gmail.com>
- Date: Thu, 8 Dec 2005 00:04:25 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
This is a question about the limitations of using a string as the head
of an expression.
As an example we'll create two expressions (or data types): a foo
(which does what we want) and a bar (which doesn't).
If we define foo with
fooQ[f_] := MatchQ[f, foo[_Integer]]
and then define wiggle on foo with
foo /: wiggle[f_foo?fooQ] := First[f] + 10
then the results are exactly as expected:
wiggle[foo[17]]
27
But, if we (note the doublequotes and) define bar as headed with a
string in
barQ[b_] := MatchQ[b, "bar"[_Integer]]
and then attempt to define wiggle on bar with
"bar" /: wiggle[b_"bar"?barQ] := First[b] - 5
we get the error
TagSetDelayed::"sym": "Argument "bar" at position 1 is expected to be
a symbol.
which makes sense because we're essentially trying to assign an upvalue
to the *string* "bar" (rather than to the *symbol* foo).
First question: is it possible to assign an upvalue to a string?
Second question: why doesn't unprotecting String with
Unprotect[String];
"bar" /: wiggle[b_"bar"?barQ] := First[b] - 5;
Protect[String]
work?
Last, is it, in general, a bad idea to create data types headed with a
string, either because of situations like the one given here or for
some other, additional reasons?
Trevor.
Prev by Date:
Re: Types in Mathematica thread
Next by Date:
Re: Roman Maeder's Classes.m
Previous by thread:
Fwd: Getting good results with NDSolve?
Next by thread:
Re: UpValues for expressions headed by a string
|