Giter VIP home page Giter VIP logo

Comments (7)

edwardcwang avatar edwardcwang commented on July 22, 2024

It seems like the return type of your poly() is AnyVal, which suggests a possible bug in the poly() function.

from chisel-bootcamp.

NeutrinoLiu avatar NeutrinoLiu commented on July 22, 2024

yes that makes sense, Ive found the problem: in the former scala numeric exercise, I used "if(select==0)else if(select==1)else if(select==2)" instead of "if(select==0)else if(select==1)else ..." to do the conditional branch. it works now. but it seems not a problem of the return type of poly() ? To be honest I suppose "if(select==0)else if(select==1)else if(select==2)" and "if(select==0)else if(select==1)else ..." have almost no difference in this case, isn't it? even it fixes the problem, I still don't know why...moreover, in the former use of the function expect, expect(IO, val) is often the case.

from chisel-bootcamp.

edwardcwang avatar edwardcwang commented on July 22, 2024

If you define a function that returns in some branches, not others, Scala will infer the result type to be AnyVal. For example:

def funcTest(sel: Int) = {
    if (sel == 5) {
        10
    }
}

is equivalent to:

def funcTest(sel: Int): AnyVal = {
    if (sel == 5) {
        10
    }
}

from chisel-bootcamp.

NeutrinoLiu avatar NeutrinoLiu commented on July 22, 2024

emm, I see. so that means:
a) the type AnyVal != Val
b) if the conditions listed are not complete the function will be defined as AnyVal defaultly, instead of a normal Val.
correct?

from chisel-bootcamp.

edwardcwang avatar edwardcwang commented on July 22, 2024

a) Val is not a Scala type and lowercase val is a keyword and cannot be used as a variable name
b) The return type if none of the if conditions are triggered is Unit, and Scala will determine the result type to be the common supertype of the branch (Int) and Unit, which is AnyVal. Note that AnyVal is much less useful and should generally be avoided. If you need to sometimes return a type, Scala has the Option type.

In other words:

def funcTest(sel: Int): AnyVal = {
    if (sel == 5) {
        10
    }
}

is identical to:

def funcTest(sel: Int): AnyVal = {
    if (sel == 5) {
        10
    } else {
        Unit
    }
}

from chisel-bootcamp.

NeutrinoLiu avatar NeutrinoLiu commented on July 22, 2024

I believe this time I get it~thats really helpful!
thanks for your patience!

from chisel-bootcamp.

edwardcwang avatar edwardcwang commented on July 22, 2024

Glad to hear it!

from chisel-bootcamp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.