What is type and what is type constructor in scala -


i'm bit confused understanding of type in scala means.

in documents read list[int] type , list type constructor.

but keyword type means when write following?

val ls: _root_.scala.collection.immutable.list.type = scala.collection.immutable.list 

and how type related type can defined field in trait example.

what keyword type means when write following

type member defined on object in scala.

suppose have class , companion object:

class foo(a: string)  object foo {   def apply(a: string) = new foo } 

now suppose want write method accepts object foo input. what's type?

if write this:

def somemethod(fooobj: foo) = fooobj.apply("x") // doesn't compile 

it's not going compile. foo refers type of instance of class (i.e. 1 returned new foo("x") or foo("x")). that's why objects have type member can refer own type:

def somemethod(fooobj: foo.type) = fooobj.apply("x") // compiles! 

in specific example list.type type of companion object of list. here's couple of examples hope clarify means:

val listobj: list.type = list val anemptylist: list[int] = listobj.empty[int] // list() val alistofintegers: list[int] = listobj.range(1, 4) // list[(1, 2, 3) 

and how type related type can defined field in trait example.

and how type related type can defined field in trait example.

the type keyword defines type member. .type is type member. conceptually it's every object in scala has type member named type, this:

object foo {   type type = foo } 

obviously won't compile, gives idea of may like.


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -