Friday, 1 December 2017

java - Why List is not acceptable as List?





Consider below method
doSomething(List) which accepts
List as
parameter.



private void
doSomething(List list) {
// do
something
}


Now
consider below code snippet which tries to call doSomething()
where I try to pass List to
doSomething()



List
objectList;
List
stringList;

doSomething(stringList); // compilation error
incompatible types
doSomething(objectList); // works fine



Even below code
throws compilation error



objectList = stringList; //
compilation error incompatible
types


My question is
why List can not be passed to a method which
accepts List?



Answer




This generic question in Java may look
confusing to any one who is not very familiar with Generics as in first glance it looks
like String is object so List can be used where
List is required but this is not true. It will
result in compilation error.



It does make sense
if you go one step further because List
can store anything including
String, Integer etc but
List can only store
Strings.



Also have a
look at: href="https://stackoverflow.com/questions/21692193/why-not-inherit-from-listt">Why
not inherit from List?



sublimetext2 - Regex in Sublime Text tmLanguage file doesn't use multiline

itemprop="text">

I'm trying to create a custom syntax
language file to highlight and help with creating new documents in Sublime Text 2. I
have come pretty far, but I'm stuck at a specific problem regarding Regex searches in
the tmLanguage file. I simply want to be able to match a regex over multiple lines
within a YAML document that I then convert to PList to use in Sublime Text as a package.
It won't work.




href="https://regex101.com/r/iH3gL0/1" rel="nofollow">This is my
regex:



/(foo[^.#]*bar)/


And
this is how it looks inside the tmLanguage YAML
document:



patterns:
-
include: '#test'


repository:
test:

comment: Tester pattern
name: constant.numeric.xdoc
match:
(foo[^.#]*bar)


If I
build this YAML to a tmLanguage file and use it as a package in Sublime Text, I create a
document that uses this custom syntax, try it out and the following
happens:




This WILL
match:



foo 12345
bar


This WILL NOT
match:



foo
12345
bar



In
a Regex tester,
they should and will both match, but in my tmLanguage file it does not
work.



I also already tried to add modifiers to
my regex in the tmLanguage file, but the following either don't work or break the
document entirely:



match:
(/foo[^.#]*bar/gm)
match: /(/foo[^.#]*bar/)/gm
match:
/foo[^.#]*bar/gm
match:
foo[^.#]*bar





Note: My Regex rule works in the tester, this problem occurs in the tmLanguage
file in Sublime Text 2
only.




Any help is
greatly appreciated.



EDIT: The reason I use a
match instead of begin/end clauses is because I want to use capture groups to give them
different names. If someone has a solution with begin and end clauses where you can
still name 'foo', '12345' and 'bar' differently, that's fine by me
too.



Answer





I found that this is impossible to
do. This is directly from the href="http://manual.macromates.com/en/language_grammars" rel="nofollow">TextMate
Manual, which is the text editor Sublime Text is based
on.







<...>



Note that the regular
expressions are matched against only a single
line of the
document at a time
. That means it is not possible to use
a
pattern that matches multiple lines
. The reason for this is
technical:

being able to restart the parser at an arbitrary line
and having to
re-parse only the minimal number of lines affected by an edit.
In most
situations it is possible to use the begin/end model to overcome
this

limitation.




My
situation is one of the few in which a begin/end model cannot overcome the limitation.
Unfortunate.


php - Parse error: syntax error, unexpected T_STATIC






class Employee 
{

public static $favSport = "Football";

public static function
watchTV()
{
echo "Watching ".static::$favSport;

}
}


class Executive extends Employee

{
public static $favSport =
"Polo";
}

echo
Executive::watchTV();




Parse error: syntax error, unexpected T_STATIC on line
7





Why
do I get parse error & and how to fix it? Thanks!



Answer




The parse error
here:



echo "Watching
".static::$favSport;


is
because rel="nofollow noreferrer">late static bindings were introduced in PHP v5.3.
Your php version (<5.3) doesn't recognize
static::$favSport.




There
isn't any way I can think of to fix it for PHP older than 5.3, other than with object
inheritance (which isn't really a fix per se since it doesn't have
anything to do with static)...



plot explanation - Taken - "I can tell you I don't have money" - Movies & TV

I'm watching href="http://www.imdb.com/title/tt0936501/" rel="nofollow
noreferrer">Taken (2008) again, and I'm at the
infamous "particular set of skills" speech. I just realized he says "...I can tell you I
don't have money...".




His wife's new
husband is portrayed as an obvious millionaire and this guy just blew the chance that he
could get his daughter back by offering them the husband's
money.



Can anyone think of any sort of logic or
reasoning behind this?

c++ - Should i specify volatile keyword for every object that shares its memory between different threads

itemprop="text">

I just read href="https://www.securecoding.cert.org/confluence/display/cplusplus/CON01-CPP.+Do+not+use+volatile+as+a+synchronization+primitive"
rel="nofollow">Do not use volatile as a synchronization primitive article
on CERT site and noticed that a compiler can theoretically optimize the following code
in the way that it'll store a flag variable in the registers
instead of modifying actual memory shared between different
threads:



bool flag = false;//Not
declaring as {{volatile}} is wrong. But even by declaring {{volatile}} this code is
still erroneous
void test() {
while (!flag) {

Sleep(1000); // sleeps for 1000 milliseconds
}
}
void
Wakeup() {
flag = true;
}
void debit(int
amount){
test();
account_balance -= amount;//We think it is safe
to go inside the critical
section
}


Am
I right?



Is it true that I need to use
volatile keyword for every object in my program that shares its
memory between different threads? Not because it does some kind of synchronization for
me (I need to use mutexes or any other synchronization primitives to accomplish such
task anyway) but just because of the fact that a compiler can possibly optimize my code
and store all shared variables in the registers so other threads will never get updated
values?



Answer




It is actually very simple, but confusing at
the same time. On a high level, there are two optimization entities at play when you
write C++ code - compiler and CPU. And within compiler, there are two major optimization
techniue in regards to variable access - omitting variable access even if written in the
code and moving other instructions around this particular variable
access.



In particular, following example
demonstrates those two techniques:



int
k;
bool flag;



void foo()
{
flag = true;
int i = k;
k++;
k =
i;
flag =
false;
}


In
the code provided, compiler is free to skip first modification of flag - leaving only
final assignment to false; and completely remove any modifications to k. If you make k
volatile, you will require compiler to preserve all access to k = it will be
incremented, and than original value put back. If you make flag volatile as well, both
assignments first to true, than two false will remain in the code. However, reordering
would still be possible, and the effective code might look
like



void foo() {
flag
= true;
flag = false;
int i = k;
k++;
k =
i;
}


This
will have unpleasant effect if another thread would be expecting flag to indicate if k
is being modified now.



One of the way to achive
the desired effect would be to define both variables as atomic.
This would prevent compiler from both optimizations, ensuring code executed will be the
same as code written. Note that atomic is, in effect, a volatile+ - it does all the
volatile does + more.



Another thing to notice
is that compiler optimizations are, indeed, a very powerful and desired tool. One should
not impede them just for the fun of it, so atomicity should be used only when it is
required.


php - What is the difference between public, private, and protected?

itemprop="text">

When and why should I use
public, private, and
protected functions and variables inside a class? What is the
difference between
them?



Examples:



//
Public
public $variable;

public function doSomething()
{
// ...
}

// Private
private
$variable;
private function doSomething() {
//
...
}


// Protected
protected
$variable;
protected function doSomething() {
//
...
}

class="post-text" itemprop="text">
class="normal">Answer



You
use:





  • public
    scope to make that property/method available from anywhere, other classes and instances
    of the
    object.


  • private
    scope when you want your property/method to be visible in its own class
    only.


  • protected
    scope when you want to make your property/method visible in all classes that extend
    current class including the parent
    class.




More:
(For comprehensive information)





What does in XML mean?

itemprop="text">

I often find this strange
CDATA tag in XML
files:




            stuff]]>


I have
observed that this CDATA tag always comes at the beginning, and
then followed by some stuff.



But sometimes it is
used, sometimes it is not. I assume it is to mark that some
stuff
is the "data" that will be inserted after that. But what kind of data
is some stuff? Isn't anything I write in XML tags some sort of
data?



Answer




href="http://www.w3.org/TR/REC-xml/#sec-cdata-sect" rel="noreferrer">CDATA
stands for rel="noreferrer">Character Data and it means that the data in between these
strings includes data that could be interpreted as XML markup, but
should not be.




The key differences
between CDATA and comments
are:





This means given
these three snippets of XML from one well-formed
document:



            MyParamEntity "Has been
expanded">


/>






/>

Within this
Character Data block I can
use double dashes as much as I want (along with
<, &, ', and ")
*and* %MyParamEntity; will be expanded to the
text
"Has been expanded" ... however, I can't use
the CEND sequence.
If I need to use CEND I must escape one of the
brackets or the greater-than
sign using concatenated CDATA
sections.

]]>


/>

An example of
escaped CENDs



]]]]>]]>


CEND
]]]>]]>



php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...