Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equal Sign and Type Mismatch

Tags:

syntax

scala

When working with the standard widget toolkit (SWT), I usually use something like this to define my GridLayout:

layout.marginTop = layout.marginBottom = 
    layout.marginLeft = layout.marginRight =
        layout.horizontalSpacing = layout.verticalSpacing = 20

It works in java but not in scala. It gives me type mismatch; Found: Unit Required: Int.

So how can this solve it?

like image 915
f4lco Avatar asked Jan 19 '23 17:01

f4lco


1 Answers

You cannot do this in one line in scala because the result type of an assignment expression (e.g. a = b) is Unit. You'd have to have 6 separate calls:

layout.marginTop = 20
layout.marginBottom = 20 
... etc

Why is the result type of an assignment Unit and nmot the assigned value? I believe this was chosen for performance reasons as outlined in this question.

There is a related question on assignment which points out that at declaration site, it is possible via:

val a, b, c = X
like image 74
oxbow_lakes Avatar answered Jan 29 '23 11:01

oxbow_lakes