Apply method used in type [Scala] -


this question has answer here:

i skimming scala code, , find apply method used in type.

example:
type common = {   def apply: {val func: {} => {val a: a}; val c: c} => {val b: b} } 

what above code mean?

as understand, means common refers types includes apply method. but, questions kind of apply method mean? should inputs of apply method be?

also,

type common = {   def apply({val func: {} => {val a: a}; val c: c} => {val b: b}) } 

what difference between 2 common type?

this known structural type. means describe type structure instead of (only) name. type foo{val a: string} means "something has type foo has val a: string". {val a: string} same anyref{val a: string}. {} means anyref{}, means same anyref.

of course can use structural types in structural types, common does. common subtype of anyref has apply method takes no arguments returns function complicated structural types type arguments. decipher have recursively apply rules first paragraph.

how use common type? recommend not to, but...

scala> :paste // entering paste mode (ctrl-d finish)  class a; class b; class c  type common = {   def apply: {val func: {} => {val a: a}; val c: c} => {val b: b} }  class hasa { val = new }  class hasb { val b = new b }  class hasc {   val func = (a: anyref) => new hasa   val c = new c }  class mycommon { def apply = (h: any) => new hasb }  // exiting paste mode, interpreting.   scala> def common(c: common) = c common: (c: common)common  scala> common(new mycommon) res0: common = mycommon@3652a0d8  scala> res0.apply(new hasc) res1: anyref{val b: b} = hasb@3925c40e  scala> res1.b res2: b = b@1ba053df 

invocations of methods of structural types might incur runtime overhead, implemented reflection.


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -