Why is String::find not a method on &str? - string

I recently noticed that String::find is actually a method on an owned String.
But I can't see why it wouldn't just be a method on &str instead, making it useful in more cases (and still being just as useful for String). Am I missing a reason for why it's like this, or is it just a historical accident?

Apparently, the documentation confused you. This method is listed under this section:
So it is not even implemented for String, but indeed just for &str.

Actually it's only available for String because it Derefs to str:
Methods from Deref<Target=str>
You won't find it in the source for String, but in the source for str.

Actually... you are wrong: it is not a String method.
What you are looking at is str::find.
It just so happens that the Rust documentation automatically includes on the String page the methods brought in by the fact that String implements Deref<Target=str> as can be seen here.
Why does the documentation includes the methods that can be called on the target of Deref?
Because you can actually call them directly on a String object, since the compiler will automatically follow Deref if it does not find the method you are calling, recursively.

Related

Where is checksumValid() defined?

I am working from a compliance perspective. I am struggling with the simplest thing, how to find the source for various functions that are called by rustup.
For example, the rustup source code references checksumValid, but I can't find it defined anywhere. I searched stackoverflow, I searched Google, etc.
I'd appreciate help finding checksumValid, but more importantly, what is wrong with my approach?
pub enum Notification<'a> {
...
NoUpdateHash(&'a Path),
ChecksumValid(&'a str),
SignatureValid(&'a str, &'a PgpPublicKey),
...
ChecksumValid(&'a str) here is not actually a function but rather one of many possible values of an enum Notification defined in this line. While the () is usually associated with functions in rust it can denote other things as well. In this case it denotes one possible value of the enum that contains some data that is of type &str with a lifetime of 'a. This is the definition of this value, in this line right here.
You might want to read this to understand the syntax.

How do I use Option::unwrap_or_default when it contains a reference?

I have a candle_series of the type Vec<Candle> that gets the last element and I try to use unwrap_or_default:
self.candle_series.last().unwrap_or_default()
But then I get this error:
method not found in `std::option::Option<&market::Candle>
How can I get the behaviour of unwrap_or_default on the struct instead of the reference?
My current workaround is this but it seems incorrect. If it is correct, please let me know:
self.candle_series.last().unwrap_or(&Candle::default())
How can I get the behaviour of unwrap_or_default on the struct instead of the reference?
You'd need to either copy or move the reference (e.g. use Option::cloned, assuming your structure is Clone) so that your option is an Option<Candle>, and unwrap_or_default can do its job.
My current workaround is this but it seems incorrect.
Looks fine to me, though of course it might be shorter to use e.g. Candle::new() if you have such an initializer.

Right way to type as "Any"

Let's say I'm using gdscript static typing and for one function parameter I don't know in advance what I'm going to get. That's what typing.Any is for in python. How do I do it with gdscript?
It seems that Variant is not a valid type and I'm not sure about using Object for that purpose (since it could be a built-in)
edit
Leaving the type blank obviously works, but the docs has a specific section called typed-or-dynamic-stick-to-one-style, and since we're already kinda short on good practices using gdscript I'd rather find another way
Any idea?
As of Godot 3.2, GDScript does not feature a Variant or any type hint yet. However, you can still use the Object type hint if you expect a variable to hold any object (or null, as Object is nullable by design). Object may not hold primitive types like int or bool though.
Therefore, you should just leave out the type hint for now.

squeak(smalltallk) how to 'inject' string into string

I'm writing a class named "MyObject".
one of the class methods is:
addTo: aCodeString assertType: aTypeCollection
when the method is called with aCodeString, I want to add (in runtime) a new method to "MyObject" class which aCodeString is it's source code and inject type checking code into the source code.
for example, if I call addTo: assertType: like that:
a := MyObject new.
a addTo: 'foo: a boo:b baz: c
^(a*b+c)'
assertType: #(SmallInteger SmallInteger SmallInteger).
I expect that I could write later:
answer := (a foo: 2 boo: 5 baz: 10).
and get 20 in answer.
and if I write:
a foo: 'someString' boo: 5 baz: 10.
I get the proper message because 'someString' is not a SmallInteger.
I know how to write the type checking code, and I know that to add the method to the class in runtime I can use 'compile' method from Behavior class.
the problem is that I want to add the type checking code inside the source code.
I'm not really familiar with all of squeak classes so I'm not sure if I rather edit the aCodeString as a string inside addTo: assertType: and then use compile: (and I don't know how to do so), or that there is a way to inject code to an existing method in Behavior class or other squeak class.
so basically, what I'm asking is how can I inject string into an existing string or to inject code into an existing method.
There are many ways you could achieve such type checking...
The one you propose is to modify the source code (a String) so as to insert additional pre-condition type checks.
The key point with this approach is that you will have to insert the type checking at the right place. That means somehow parsing the original source (or at least the selector and arguments) so as to find its exact span (and the argument names).
See method initPattern:return: in Parser and its senders. You will find quite low level (not most beautiful) code that feed the block (passed thru return: keyword) with sap an Array of 3 objects: the method selector, the method arguments and the method precedence (a code telling if the method is connected to unary, binary or keyword message). From there, you'll get enough material for achieving source code manipulation (insert a string into another with copyReplace:from:to:with:).
Do not hesitate to write small snippets of code and execute in the Debugger (select code to debug, then use debug it menu or ALT+Shift+D). Also use the inspectors extensively to gain more insight on how things work!
Another solution is to parse the whole Abstract Syntax Tree (AST) of the source code, and manipulate that AST to insert the type checks. Normally, the Parser builds the AST, so observe how it works. From the modified AST, you can then generate new CompiledMethod (the bytecode instructions) and install it in methodDictionary - see the source code of compile: and follow the message sent until you discover generateMethodFromNode:trailer:. This is a bit more involved, and has a bad side effect that the source code is now not in phase with generated code, which might become a problem once you want to debug the method (fortunately, Squeak can used decompiled code in place of source code!).
Last, you can also arrange to have an alternate compiler and parser for some of your classes (see compilerClass and/or parserClass). The alternate TypeHintParser would accept modified syntax with the type hints in source code (once upon a time, it was implemented with type hints following the args inside angle brackets foo: x <Integer> bar: y <Number>). And the alternate TypeHintCompiler would arrange to compile preconditions automatically given those type hints. Since you will then be very advanced in Squeak, you will also create special mapping between source code index and bytecodes so as to have sane debugger and even special Decompiler class that could recognize the precondition type checks and transform them back to type hints just in case.
My advice would be to start with the first approach that you are proposing.
EDIT
I forgot to say, there is yet another way, but it is currently available in Pharo rather than Squeak: Pharo compiler (named OpalCompiler) does reify the bytecode instructions as objects (class names beginning with IR) in the generation phase. So it is also possible to directly manipulate the bytecode instructions by proper hacking at this stage... I'm pretty sure that we can find examples of usage. Probably the most advanced technic.

Types for strings escaped/encoded differently

Recently I am dealing with escaping/encoding issues. I have a bunch of APIs that receive and return Strings encoded/escaped differently. In order to clean up the mess I'd like to introduce new types XmlEscapedString, HtmlEscapedString, UrlEncodedString, etc. and use them instead of Strings.
The problem is that the compiler cannot check the encoding/escaping and I'll have runtime errors.
I can also provide "conversion" functions that escape/encode input as necessary. Does it make sense ?
The compiler can enforce that you pass the types through your encoding/decoding functions; this should be enough, provided you get things right at the boundaries (if you have a correctly encoded XmlEscapedString and convert it to a UrlEncodedString, the result is always going to be correctly encoded, no?). You could use constructors or conversion methods that check the escaping initially, though you might pay a performance penalty for doing so.
(Theoretically it might be possible to check a string's escaping at compile time using type-level programming, but this would be exceedingly difficult and only work on literals anyway, when it sounds like the problem is Strings coming in from other APIs).
My own compromise position would probably be to use tagged types (using Scalaz tags) and have the conversion from untagged String to tagged string perform the checking, i.e.:
import scalaz._, Scalaz._
sealed trait XmlEscaped
def xmlEscape(rawString: String): String ## XmlEscaped = {
//perform escaping, guaranteed to return a correctly-escaped String
Tag[String, XmlEscaped](escapedString)
}
def castToXmlEscaped(escapedStringFromJavaApi: String) = {
require(...) //confirm that string is properly escaped
Tag[String, XmlEscaped](escapedStringFromJavaApi)
}
def someMethodThatRequiresAnEscapedString(string: String ## XmlEscaped)
Then we use castToXmlEscaped for Strings that are already supposed to be XML-escaped, so we check there, but we only have to check once; the rest of the time we pass it around as a String ## XmlEscaped, and the compiler will enforce that we never pass a non-escaped string to a method that expects one.

Resources